1
- import type {
2
- ClientOptions ,
3
- Hub ,
4
- Scope ,
5
- Span ,
6
- SpanTimeInput ,
7
- StartSpanOptions ,
8
- TransactionContext ,
9
- } from '@sentry/types' ;
1
+ import type { ClientOptions , Scope , Span , SpanTimeInput , StartSpanOptions , TransactionContext } from '@sentry/types' ;
10
2
11
3
import { propagationContextFromHeaders } from '@sentry/utils' ;
12
4
import type { AsyncContextStrategy } from '../asyncContext' ;
@@ -17,6 +9,8 @@ import { getAsyncContextStrategy, getCurrentHub } from '../hub';
17
9
import { handleCallbackErrors } from '../utils/handleCallbackErrors' ;
18
10
import { hasTracingEnabled } from '../utils/hasTracingEnabled' ;
19
11
import {
12
+ _getSpanForScope ,
13
+ _setSpanForScope ,
20
14
addChildSpanToSpan ,
21
15
getActiveSpan ,
22
16
spanIsSampled ,
@@ -34,13 +28,12 @@ import { setCapturedScopesOnSpan } from './utils';
34
28
/**
35
29
* Wraps a function with a transaction/span and finishes the span after the function is done.
36
30
* The created span is the active span and will be used as parent by other spans created inside the function
37
- * and can be accessed via `Sentry.getSpan ()`, as long as the function is executed while the scope is active.
31
+ * and can be accessed via `Sentry.getActiveSpan ()`, as long as the function is executed while the scope is active.
38
32
*
39
33
* If you want to create a span that is not set as active, use {@link startInactiveSpan}.
40
34
*
41
- * Note that if you have not enabled tracing extensions via `addTracingExtensions`
42
- * or you didn't set `tracesSampleRate`, this function will not generate spans
43
- * and the `span` returned from the callback will be undefined.
35
+ * You'll always get a span passed to the callback,
36
+ * it may just be a non-recording span if the span is not sampled or if tracing is disabled.
44
37
*/
45
38
export function startSpan < T > ( context : StartSpanOptions , callback : ( span : Span ) => T ) : T {
46
39
const acs = getAcs ( ) ;
@@ -51,23 +44,19 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span) =
51
44
const spanContext = normalizeContext ( context ) ;
52
45
53
46
return withScope ( context . scope , scope => {
54
- // eslint-disable-next-line deprecation/deprecation
55
- const hub = getCurrentHub ( ) ;
56
- // eslint-disable-next-line deprecation/deprecation
57
- const parentSpan = scope . getSpan ( ) as SentrySpan | undefined ;
47
+ const parentSpan = _getSpanForScope ( scope ) as SentrySpan | undefined ;
58
48
59
49
const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
60
50
const activeSpan = shouldSkipSpan
61
51
? new SentryNonRecordingSpan ( )
62
- : createChildSpanOrTransaction ( hub , {
52
+ : createChildSpanOrTransaction ( {
63
53
parentSpan,
64
54
spanContext,
65
55
forceTransaction : context . forceTransaction ,
66
56
scope,
67
57
} ) ;
68
58
69
- // eslint-disable-next-line deprecation/deprecation
70
- scope . setSpan ( activeSpan ) ;
59
+ _setSpanForScope ( scope , activeSpan ) ;
71
60
72
61
return handleCallbackErrors (
73
62
( ) => callback ( activeSpan ) ,
@@ -90,9 +79,8 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span) =
90
79
* The created span is the active span and will be used as parent by other spans created inside the function
91
80
* and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active.
92
81
*
93
- * Note that if you have not enabled tracing extensions via `addTracingExtensions`
94
- * or you didn't set `tracesSampleRate`, this function will not generate spans
95
- * and the `span` returned from the callback will be undefined.
82
+ * You'll always get a span passed to the callback,
83
+ * it may just be a non-recording span if the span is not sampled or if tracing is disabled.
96
84
*/
97
85
export function startSpanManual < T > ( context : StartSpanOptions , callback : ( span : Span , finish : ( ) => void ) => T ) : T {
98
86
const acs = getAcs ( ) ;
@@ -103,23 +91,19 @@ export function startSpanManual<T>(context: StartSpanOptions, callback: (span: S
103
91
const spanContext = normalizeContext ( context ) ;
104
92
105
93
return withScope ( context . scope , scope => {
106
- // eslint-disable-next-line deprecation/deprecation
107
- const hub = getCurrentHub ( ) ;
108
- // eslint-disable-next-line deprecation/deprecation
109
- const parentSpan = scope . getSpan ( ) as SentrySpan | undefined ;
94
+ const parentSpan = _getSpanForScope ( scope ) as SentrySpan | undefined ;
110
95
111
96
const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
112
97
const activeSpan = shouldSkipSpan
113
98
? new SentryNonRecordingSpan ( )
114
- : createChildSpanOrTransaction ( hub , {
99
+ : createChildSpanOrTransaction ( {
115
100
parentSpan,
116
101
spanContext,
117
102
forceTransaction : context . forceTransaction ,
118
103
scope,
119
104
} ) ;
120
105
121
- // eslint-disable-next-line deprecation/deprecation
122
- scope . setSpan ( activeSpan ) ;
106
+ _setSpanForScope ( scope , activeSpan ) ;
123
107
124
108
function finishAndSetSpan ( ) : void {
125
109
activeSpan . end ( ) ;
@@ -140,13 +124,12 @@ export function startSpanManual<T>(context: StartSpanOptions, callback: (span: S
140
124
141
125
/**
142
126
* Creates a span. This span is not set as active, so will not get automatic instrumentation spans
143
- * as children or be able to be accessed via `Sentry.getSpan ()`.
127
+ * as children or be able to be accessed via `Sentry.getActiveSpan ()`.
144
128
*
145
129
* If you want to create a span that is set as active, use {@link startSpan}.
146
130
*
147
- * Note that if you have not enabled tracing extensions via `addTracingExtensions`
148
- * or you didn't set `tracesSampleRate` or `tracesSampler`, this function will not generate spans
149
- * and the `span` returned from the callback will be undefined.
131
+ * This function will always return a span,
132
+ * it may just be a non-recording span if the span is not sampled or if tracing is disabled.
150
133
*/
151
134
export function startInactiveSpan ( context : StartSpanOptions ) : Span {
152
135
const acs = getAcs ( ) ;
@@ -155,11 +138,8 @@ export function startInactiveSpan(context: StartSpanOptions): Span {
155
138
}
156
139
157
140
const spanContext = normalizeContext ( context ) ;
158
- // eslint-disable-next-line deprecation/deprecation
159
- const hub = getCurrentHub ( ) ;
160
141
const parentSpan = context . scope
161
- ? // eslint-disable-next-line deprecation/deprecation
162
- ( context . scope . getSpan ( ) as SentrySpan | undefined )
142
+ ? ( _getSpanForScope ( context . scope ) as SentrySpan | undefined )
163
143
: ( getActiveSpan ( ) as SentrySpan | undefined ) ;
164
144
165
145
const shouldSkipSpan = context . onlyIfParent && ! parentSpan ;
@@ -170,7 +150,7 @@ export function startInactiveSpan(context: StartSpanOptions): Span {
170
150
171
151
const scope = context . scope || getCurrentScope ( ) ;
172
152
173
- return createChildSpanOrTransaction ( hub , {
153
+ return createChildSpanOrTransaction ( {
174
154
parentSpan,
175
155
spanContext,
176
156
forceTransaction : context . forceTransaction ,
@@ -219,26 +199,22 @@ export function withActiveSpan<T>(span: Span | null, callback: (scope: Scope) =>
219
199
}
220
200
221
201
return withScope ( scope => {
222
- // eslint-disable-next-line deprecation/deprecation
223
- scope . setSpan ( span || undefined ) ;
202
+ _setSpanForScope ( scope , span || undefined ) ;
224
203
return callback ( scope ) ;
225
204
} ) ;
226
205
}
227
206
228
- function createChildSpanOrTransaction (
229
- hub : Hub ,
230
- {
231
- parentSpan,
232
- spanContext,
233
- forceTransaction,
234
- scope,
235
- } : {
236
- parentSpan : SentrySpan | undefined ;
237
- spanContext : TransactionContext ;
238
- forceTransaction ?: boolean ;
239
- scope : Scope ;
240
- } ,
241
- ) : Span {
207
+ function createChildSpanOrTransaction ( {
208
+ parentSpan,
209
+ spanContext,
210
+ forceTransaction,
211
+ scope,
212
+ } : {
213
+ parentSpan : SentrySpan | undefined ;
214
+ spanContext : TransactionContext ;
215
+ forceTransaction ?: boolean ;
216
+ scope : Scope ;
217
+ } ) : Span {
242
218
if ( ! hasTracingEnabled ( ) ) {
243
219
return new SentryNonRecordingSpan ( ) ;
244
220
}
0 commit comments