Replies: 3 comments 1 reply
|
Currently the OpenTelemetry specification does not allow this type of sampling override option, it would provide the instrumentation author a mechanism that could drastically conflict with what an operator would want regarding sampling. The |
|
To force 100% sampling via a request header, you need to implement a custom type ForceTraceSampler struct {
fallback trace.Sampler
}
func (s ForceTraceSampler) ShouldSample(p trace.SamplingParameters) trace.SamplingResult {
// Check if the force-trace attribute was set on the span
for _, attr := range p.Attributes {
if attr.Key == "force.trace" && attr.Value.AsBool() {
return trace.SamplingResult{
Decision: trace.RecordAndSample,
Tracestate: p.ParentContext.TraceState(),
}
}
}
return s.fallback.ShouldSample(p)
}
func (s ForceTraceSampler) Description() string {
return "ForceTraceSampler"
}Then at the trace start point, inject the attribute based on your header: forceTrace := len(reqCtx.Request.Header.Peek("X-Force-Trace")) > 0
attrs := []attribute.KeyValue{}
if forceTrace {
attrs = append(attrs, attribute.Bool("force.trace", true))
}
ctxWithSpan, span := tr.Start(ctx, name,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attrs...),
)And wire it in when building your TracerProvider: tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(
ForceTraceSampler{
fallback: sdktrace.TraceIDRatioBased(0.001),
},
),
)On the efficiency question from @atombender — the linear attribute scan is generally fine since type forceTraceKey struct{}
// set in middleware
ctx = context.WithValue(ctx, forceTraceKey{}, true)
// check in sampler
func (s ForceTraceSampler) ShouldSample(p trace.SamplingParameters) trace.SamplingResult {
if p.ParentContext.Value(forceTraceKey{}) == true {
return trace.SamplingResult{Decision: trace.RecordAndSample}
}
return s.fallback.ShouldSample(p)
}This avoids the attribute scan entirely and is safe across goroutines since |
|
In OpenTelemetry, the sampler is consulted only when a span is created. After that, the sampling decision cannot be changed for that span. If you want to force 100% sampling based on a request header such as
There isn't a built-in span option that overrides the sampler after |
Uh oh!
There was an error while loading. Please reload this page.
Hi!
I have a service that starts tracing with a given ratio:
So I want to override this Sampler with a
trace.AwlaysSamplerlike I did it with opencensus.trace.WithRecord()enables recording but doesn't affect the sampling decision.How to achieve 100% sampling by a flag?
All reactions