@@ -7,11 +7,12 @@ import { DEBUG_BUILD } from '../debug-build';
77import { getCurrentHub } from '../hub' ;
88import { handleCallbackErrors } from '../utils/handleCallbackErrors' ;
99import { hasTracingEnabled } from '../utils/hasTracingEnabled' ;
10- import { spanIsSampled , spanTimeInputToSeconds , spanToJSON } from '../utils/spanUtils' ;
10+ import { addChildSpanToSpan , spanIsSampled , spanTimeInputToSeconds , spanToJSON } from '../utils/spanUtils' ;
1111import { getDynamicSamplingContextFromSpan } from './dynamicSamplingContext' ;
12+ import { SentryNonRecordingSpan } from './sentryNonRecordingSpan' ;
1213import type { SentrySpan } from './sentrySpan' ;
1314import { SPAN_STATUS_ERROR } from './spanstatus' ;
14- import { addChildSpanToSpan , getActiveSpan , setCapturedScopesOnSpan } from './utils' ;
15+ import { getActiveSpan , setCapturedScopesOnSpan } from './utils' ;
1516
1617/**
1718 * Wraps a function with a transaction/span and finishes the span after the function is done.
@@ -24,7 +25,7 @@ import { addChildSpanToSpan, getActiveSpan, setCapturedScopesOnSpan } from './ut
2425 * or you didn't set `tracesSampleRate`, this function will not generate spans
2526 * and the `span` returned from the callback will be undefined.
2627 */
27- export function startSpan < T > ( context : StartSpanOptions , callback : ( span : Span | undefined ) => T ) : T {
28+ export function startSpan < T > ( context : StartSpanOptions , callback : ( span : Span ) => T ) : T {
2829 const spanContext = normalizeContext ( context ) ;
2930
3031 return withScope ( context . scope , scope => {
@@ -35,18 +36,16 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span |
3536
3637 const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
3738 const activeSpan = shouldSkipSpan
38- ? undefined
39+ ? new SentryNonRecordingSpan ( )
3940 : createChildSpanOrTransaction ( hub , {
4041 parentSpan,
4142 spanContext,
4243 forceTransaction : context . forceTransaction ,
4344 scope,
4445 } ) ;
4546
46- if ( activeSpan ) {
47- // eslint-disable-next-line deprecation/deprecation
48- scope . setSpan ( activeSpan ) ;
49- }
47+ // eslint-disable-next-line deprecation/deprecation
48+ scope . setSpan ( activeSpan ) ;
5049
5150 return handleCallbackErrors (
5251 ( ) => callback ( activeSpan ) ,
@@ -75,10 +74,7 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span |
7574 * or you didn't set `tracesSampleRate`, this function will not generate spans
7675 * and the `span` returned from the callback will be undefined.
7776 */
78- export function startSpanManual < T > (
79- context : StartSpanOptions ,
80- callback : ( span : Span | undefined , finish : ( ) => void ) => T ,
81- ) : T {
77+ export function startSpanManual < T > ( context : StartSpanOptions , callback : ( span : Span , finish : ( ) => void ) => T ) : T {
8278 const spanContext = normalizeContext ( context ) ;
8379
8480 return withScope ( context . scope , scope => {
@@ -89,18 +85,16 @@ export function startSpanManual<T>(
8985
9086 const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
9187 const activeSpan = shouldSkipSpan
92- ? undefined
88+ ? new SentryNonRecordingSpan ( )
9389 : createChildSpanOrTransaction ( hub , {
9490 parentSpan,
9591 spanContext,
9692 forceTransaction : context . forceTransaction ,
9793 scope,
9894 } ) ;
9995
100- if ( activeSpan ) {
101- // eslint-disable-next-line deprecation/deprecation
102- scope . setSpan ( activeSpan ) ;
103- }
96+ // eslint-disable-next-line deprecation/deprecation
97+ scope . setSpan ( activeSpan ) ;
10498
10599 function finishAndSetSpan ( ) : void {
106100 activeSpan && activeSpan . end ( ) ;
@@ -131,11 +125,7 @@ export function startSpanManual<T>(
131125 * or you didn't set `tracesSampleRate` or `tracesSampler`, this function will not generate spans
132126 * and the `span` returned from the callback will be undefined.
133127 */
134- export function startInactiveSpan ( context : StartSpanOptions ) : Span | undefined {
135- if ( ! hasTracingEnabled ( ) ) {
136- return undefined ;
137- }
138-
128+ export function startInactiveSpan ( context : StartSpanOptions ) : Span {
139129 const spanContext = normalizeContext ( context ) ;
140130 // eslint-disable-next-line deprecation/deprecation
141131 const hub = getCurrentHub ( ) ;
@@ -147,7 +137,7 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
147137 const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
148138
149139 if ( shouldSkipSpan ) {
150- return undefined ;
140+ return new SentryNonRecordingSpan ( ) ;
151141 }
152142
153143 const scope = context . scope || getCurrentScope ( ) ;
@@ -275,14 +265,14 @@ function createChildSpanOrTransaction(
275265 forceTransaction ?: boolean ;
276266 scope : Scope ;
277267 } ,
278- ) : Span | undefined {
268+ ) : Span {
279269 if ( ! hasTracingEnabled ( ) ) {
280- return undefined ;
270+ return new SentryNonRecordingSpan ( ) ;
281271 }
282272
283273 const isolationScope = getIsolationScope ( ) ;
284274
285- let span : Span | undefined ;
275+ let span : Span ;
286276 if ( parentSpan && ! forceTransaction ) {
287277 // eslint-disable-next-line deprecation/deprecation
288278 span = parentSpan . startChild ( spanContext ) ;
0 commit comments