Skip to content

Commit b9e108b

Browse files
committed
fix(otel): also respect sampling on parentless spans
1 parent f23d22a commit b9e108b

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

client.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,7 @@ func NewClient(options ClientOptions) (*Client, error) {
307307
switch options.Instrumenter {
308308
case "":
309309
options.Instrumenter = "sentry"
310-
case "sentry":
311-
// noop
312-
case "otel":
313-
// sampling is performed by the OpenTelemetry SDK
314-
options.TracesSampleRate = 1.0
315-
options.TracesSampler = nil
310+
case "sentry", "otel": // noop
316311
default:
317312
return nil, fmt.Errorf("invalid value for TracesInstrumenter (supported are 'sentry' and 'otel'): %q", options.Instrumenter)
318313
}

otel/span_processor.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ func (ssp *sentrySpanProcessor) OnStart(parent context.Context, s otelSdkTrace.R
4646

4747
sentrySpanMap.Set(otelSpanID, span)
4848
} else {
49-
traceParentContext := getTraceParentContext(parent)
49+
sampled := getSampled(parent, s)
5050
transaction := sentry.StartTransaction(
5151
parent,
5252
s.Name(),
53-
sentry.WithSpanSampled(traceParentContext.Sampled),
53+
sentry.WithSpanSampled(sampled),
5454
)
5555
transaction.SpanID = sentry.SpanID(otelSpanID)
5656
transaction.TraceID = sentry.TraceID(otelTraceID)
@@ -112,12 +112,17 @@ func flushSpanProcessor(ctx context.Context) error {
112112
return nil
113113
}
114114

115-
func getTraceParentContext(ctx context.Context) sentry.TraceParentContext {
115+
func getSampled(ctx context.Context, s otelSdkTrace.ReadWriteSpan) sentry.Sampled {
116116
traceParentContext, ok := ctx.Value(sentryTraceParentContextKey{}).(sentry.TraceParentContext)
117-
if !ok {
118-
traceParentContext.Sampled = sentry.SampledUndefined
117+
if ok {
118+
return traceParentContext.Sampled
119119
}
120-
return traceParentContext
120+
121+
if s.SpanContext().IsSampled() {
122+
return sentry.SampledTrue
123+
}
124+
125+
return sentry.SampledFalse
121126
}
122127

123128
func updateTransactionWithOtelData(transaction *sentry.Span, s otelSdkTrace.ReadOnlySpan) {

otel/span_processor_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func emptyContextWithSentry() context.Context {
4141
Environment: "testing",
4242
Release: "1.2.3",
4343
EnableTracing: true,
44-
TracesSampleRate: 1.0,
44+
TracesSampleRate: 0.0, // we want to ensure otel's sampling decision (AlwaysSample) is used instead
4545
Transport: &TransportMock{},
4646
})
4747
hub := sentry.NewHub(client, sentry.NewScope())

0 commit comments

Comments
 (0)