Replies: 2 comments 9 replies
-
Your opentelemetry-go/trace/trace.go Lines 54 to 58 in f4ec95d opentelemetry-go/trace/trace.go Lines 77 to 81 in f4ec95d This means the opentelemetry-go/trace/trace.go Lines 215 to 219 in f4ec95d opentelemetry-go/trace/trace.go Lines 242 to 245 in f4ec95d opentelemetry-go/trace/trace.go Lines 263 to 266 in f4ec95d Because of this the parent based sampler will use the root sampler to make a sampling decision for you child span. opentelemetry-go/sdk/trace/sampling.go Lines 265 to 281 in f4ec95d This means your child span will be sampled at the rate you configured for ratio based root sampler. To always sample the child span, make sure the parent span context contains valid trace and span identifiers and that the |
Beta Was this translation helpful? Give feedback.
-
I think the problem is that the I was able to get your example to work with the following: package main
import (
"context"
"log"
"go.opentelemetry.io/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
const (
sampleRatio = 0
shouldSample = true
)
func main() {
sampler := sdktrace.ParentBased(sdktrace.TraceIDRatioBased(sampleRatio))
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sampler))
otel.SetTracerProvider(tp)
tracer := otel.Tracer("")
spanContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{1}, // TODO: randomly generate
SpanID: [8]byte{1}, // TODO: randomly generate
})
ctx := trace.ContextWithSpanContext(context.Background(),
spanContext.WithTraceFlags(spanContext.TraceFlags().WithSampled(true)))
ctx, span := tracer.Start(ctx, "span")
log.Printf("Span IsSampled: %#v", span.SpanContext().IsSampled())
defer span.End()
_, span2 := tracer.Start(ctx, "span2")
log.Printf("Span2 IsSampled: %#v", span.SpanContext().IsSampled())
defer span2.End()
} Of course, manually having to set those TraceID and SpanID is quite cumbersome. A nicer way of generating that SpanContext with custom flags would be interesting. |
Beta Was this translation helpful? Give feedback.
-
I am trying to force sampling by a request header.
For a sampler, I'm using
ParentBased
withTraceIDRatioBased
:To start a new span (upon HTTP request), I call
Start
:So in order to force a sampling, I want to check the request headers, and based on my logic decide if I should sample that request (regardless and on top of the ratio based sampling). To do that, I am wrapping the request context like so:
I do get a context with span config that is sampled, but when
Start
is called, the span context that is returned is not sampled anymore, or at least it'sspan.SpanContext().IsSampled()
returnsfalse
What am I missing?
Is there another way to force sampling and achieve my requirement? Seems to me it's a simple and widely used use case.
Beta Was this translation helpful? Give feedback.
All reactions