Skip to content

Commit

Permalink
fix(otel): also respect sampling on parentless spans
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Oct 5, 2023
1 parent 26911d5 commit a775255
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
7 changes: 1 addition & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,7 @@ func NewClient(options ClientOptions) (*Client, error) {
switch options.Instrumenter {
case "":
options.Instrumenter = "sentry"
case "sentry":
// noop
case "otel":
// sampling is performed by the OpenTelemetry SDK
options.TracesSampleRate = 1.0
options.TracesSampler = nil
case "sentry", "otel": // noop
default:
return nil, fmt.Errorf("invalid value for TracesInstrumenter (supported are 'sentry' and 'otel'): %q", options.Instrumenter)
}
Expand Down
17 changes: 11 additions & 6 deletions otel/span_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (ssp *sentrySpanProcessor) OnStart(parent context.Context, s otelSdkTrace.R

sentrySpanMap.Set(otelSpanID, span)
} else {
traceParentContext := getTraceParentContext(parent)
sampled := getSampled(parent, s)
transaction := sentry.StartTransaction(
parent,
s.Name(),
sentry.WithSpanSampled(traceParentContext.Sampled),
sentry.WithSpanSampled(sampled),
)
transaction.SpanID = sentry.SpanID(otelSpanID)
transaction.TraceID = sentry.TraceID(otelTraceID)
Expand Down Expand Up @@ -112,12 +112,17 @@ func flushSpanProcessor(ctx context.Context) error {
return nil
}

func getTraceParentContext(ctx context.Context) sentry.TraceParentContext {
func getSampled(ctx context.Context, s otelSdkTrace.ReadWriteSpan) sentry.Sampled {
traceParentContext, ok := ctx.Value(sentryTraceParentContextKey{}).(sentry.TraceParentContext)
if !ok {
traceParentContext.Sampled = sentry.SampledUndefined
if ok {
return traceParentContext.Sampled
}
return traceParentContext

if s.SpanContext().IsSampled() {
return sentry.SampledTrue
}

return sentry.SampledFalse
}

func updateTransactionWithOtelData(transaction *sentry.Span, s otelSdkTrace.ReadOnlySpan) {
Expand Down
2 changes: 1 addition & 1 deletion otel/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func emptyContextWithSentry() context.Context {
Environment: "testing",
Release: "1.2.3",
EnableTracing: true,
TracesSampleRate: 1.0,
TracesSampleRate: 0.0, // we want to ensure otel's sampling decision (AlwaysSample) is used instead
Transport: &TransportMock{},
})
hub := sentry.NewHub(client, sentry.NewScope())
Expand Down

0 comments on commit a775255

Please sign in to comment.