11import type { Context , Span , SpanContext , SpanOptions , Tracer } from '@opentelemetry/api' ;
22import { context , SpanStatusCode , trace , TraceFlags } from '@opentelemetry/api' ;
3- import { suppressTracing } from '@opentelemetry/core' ;
3+ import { isTracingSuppressed , suppressTracing } from '@opentelemetry/core' ;
44import type {
55 Client ,
66 continueTrace as baseContinueTrace ,
@@ -17,6 +17,7 @@ import {
1717 getRootSpan ,
1818 getTraceContextFromScope ,
1919 handleCallbackErrors ,
20+ hasSpansEnabled ,
2021 SDK_VERSION ,
2122 SEMANTIC_ATTRIBUTE_SENTRY_OP ,
2223 spanToJSON ,
@@ -53,6 +54,34 @@ export function startSpan<T>(options: OpenTelemetrySpanContext, callback: (span:
5354
5455 const spanOptions = getSpanOptions ( options ) ;
5556
57+ // If spans are not enabled, ensure we suppress tracing for the span creation
58+ // but preserve the original context for the callback execution
59+ // This ensures that we don't create spans when tracing is disabled which
60+ // would otherwise be a problem for users that don't enable tracing but use
61+ // custom OpenTelemetry setups.
62+ if ( ! hasSpansEnabled ( ) ) {
63+ const suppressedCtx = isTracingSuppressed ( ctx ) ? ctx : suppressTracing ( ctx ) ;
64+
65+ return context . with ( suppressedCtx , ( ) => {
66+ return tracer . startActiveSpan ( name , spanOptions , suppressedCtx , span => {
67+ // Restore the original context for the callback execution
68+ // so that custom OpenTelemetry spans maintain the correct context
69+ return context . with ( ctx , ( ) => {
70+ return handleCallbackErrors (
71+ ( ) => callback ( span ) ,
72+ ( ) => {
73+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
74+ if ( spanToJSON ( span ) . status === undefined ) {
75+ span . setStatus ( { code : SpanStatusCode . ERROR } ) ;
76+ }
77+ } ,
78+ ( ) => span . end ( ) ,
79+ ) ;
80+ } ) ;
81+ } ) ;
82+ } ) ;
83+ }
84+
5685 return tracer . startActiveSpan ( name , spanOptions , ctx , span => {
5786 return handleCallbackErrors (
5887 ( ) => callback ( span ) ,
@@ -96,6 +125,33 @@ export function startSpanManual<T>(
96125
97126 const spanOptions = getSpanOptions ( options ) ;
98127
128+ if ( ! hasSpansEnabled ( ) ) {
129+ // If spans are not enabled, ensure we suppress tracing for the span creation
130+ // but preserve the original context for the callback execution
131+ // This ensures that we don't create spans when tracing is disabled which
132+ // would otherwise be a problem for users that don't enable tracing but use
133+ // custom OpenTelemetry setups.
134+ const suppressedCtx = isTracingSuppressed ( ctx ) ? ctx : suppressTracing ( ctx ) ;
135+
136+ return context . with ( suppressedCtx , ( ) => {
137+ return tracer . startActiveSpan ( name , spanOptions , suppressedCtx , span => {
138+ // Restore the original context for the callback execution
139+ // so that custom OpenTelemetry spans maintain the correct context
140+ return context . with ( ctx , ( ) => {
141+ return handleCallbackErrors (
142+ ( ) => callback ( span , ( ) => span . end ( ) ) ,
143+ ( ) => {
144+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
145+ if ( spanToJSON ( span ) . status === undefined ) {
146+ span . setStatus ( { code : SpanStatusCode . ERROR } ) ;
147+ }
148+ } ,
149+ ) ;
150+ } ) ;
151+ } ) ;
152+ } ) ;
153+ }
154+
99155 return tracer . startActiveSpan ( name , spanOptions , ctx , span => {
100156 return handleCallbackErrors (
101157 ( ) => callback ( span , ( ) => span . end ( ) ) ,
@@ -134,9 +190,12 @@ export function startInactiveSpan(options: OpenTelemetrySpanContext): Span {
134190
135191 const spanOptions = getSpanOptions ( options ) ;
136192
137- const span = tracer . startSpan ( name , spanOptions , ctx ) ;
193+ if ( ! hasSpansEnabled ( ) ) {
194+ const suppressedCtx = isTracingSuppressed ( ctx ) ? ctx : suppressTracing ( ctx ) ;
195+ return tracer . startSpan ( name , spanOptions , suppressedCtx ) ;
196+ }
138197
139- return span ;
198+ return tracer . startSpan ( name , spanOptions , ctx ) ;
140199 } ) ;
141200}
142201
0 commit comments