@@ -2,13 +2,15 @@ import type { TransactionContext } from '@sentry/types';
2
2
import { isThenable } from '@sentry/utils' ;
3
3
4
4
import { getCurrentHub } from '../hub' ;
5
+ import { hasTracingEnabled } from '../utils/hasTracingEnabled' ;
5
6
import type { Span } from './span' ;
6
7
7
8
/**
8
9
* Wraps a function with a transaction/span and finishes the span after the function is done.
9
10
*
10
- * Note that if you have not enabled tracing extensions via `addTracingExtensions`, this function
11
- * will not generate spans, and the `span` returned from the callback may be undefined.
11
+ * Note that if you have not enabled tracing extensions via `addTracingExtensions`
12
+ * or you didn't set `tracesSampleRate`, this function will not generate spans
13
+ * and the `span` returned from the callback will be undefined.
12
14
*
13
15
* This function is meant to be used internally and may break at any time. Use at your own risk.
14
16
*
@@ -31,7 +33,15 @@ export function trace<T>(
31
33
const scope = hub . getScope ( ) ;
32
34
33
35
const parentSpan = scope . getSpan ( ) ;
34
- const activeSpan = parentSpan ? parentSpan . startChild ( ctx ) : hub . startTransaction ( ctx ) ;
36
+
37
+ function getActiveSpan ( ) : Span | undefined {
38
+ if ( ! hasTracingEnabled ( ) ) {
39
+ return undefined ;
40
+ }
41
+ return parentSpan ? parentSpan . startChild ( ctx ) : hub . startTransaction ( ctx ) ;
42
+ }
43
+
44
+ const activeSpan = getActiveSpan ( ) ;
35
45
scope . setSpan ( activeSpan ) ;
36
46
37
47
function finishAndSetSpan ( ) : void {
0 commit comments