@@ -6,13 +6,16 @@ import {
6
6
initAndBind ,
7
7
Integrations as CoreIntegrations ,
8
8
} from '@sentry/core' ;
9
- import type { SessionStatus , StackParser } from '@sentry/types' ;
9
+ import type { DynamicSamplingContext , PropagationContext , SessionStatus , StackParser } from '@sentry/types' ;
10
10
import {
11
+ baggageHeaderToDynamicSamplingContext ,
11
12
createStackParser ,
13
+ extractTraceparentData ,
12
14
GLOBAL_OBJ ,
13
15
logger ,
14
16
nodeStackLineParser ,
15
17
stackParserFromStackParserOptions ,
18
+ uuid4 ,
16
19
} from '@sentry/utils' ;
17
20
18
21
import { setNodeAsyncContextStrategy } from './async' ;
@@ -129,8 +132,9 @@ export function init(options: NodeOptions = {}): void {
129
132
options . dsn = process . env . SENTRY_DSN ;
130
133
}
131
134
132
- if ( options . tracesSampleRate === undefined && process . env . SENTRY_TRACES_SAMPLE_RATE ) {
133
- const tracesSampleRate = parseFloat ( process . env . SENTRY_TRACES_SAMPLE_RATE ) ;
135
+ const sentryTracesSampleRate = process . env . SENTRY_TRACES_SAMPLE_RATE ;
136
+ if ( options . tracesSampleRate === undefined && sentryTracesSampleRate ) {
137
+ const tracesSampleRate = parseFloat ( sentryTracesSampleRate ) ;
134
138
if ( isFinite ( tracesSampleRate ) ) {
135
139
options . tracesSampleRate = tracesSampleRate ;
136
140
}
@@ -171,6 +175,8 @@ export function init(options: NodeOptions = {}): void {
171
175
if ( options . autoSessionTracking ) {
172
176
startSessionTracking ( ) ;
173
177
}
178
+
179
+ updateScopeFromEnvVariables ( ) ;
174
180
}
175
181
176
182
/**
@@ -285,3 +291,35 @@ function startSessionTracking(): void {
285
291
if ( session && ! terminalStates . includes ( session . status ) ) hub . endSession ( ) ;
286
292
} ) ;
287
293
}
294
+
295
+ /**
296
+ * Update scope and propagation context based on environmental variables.
297
+ *
298
+ * See https://github.com/getsentry/rfcs/blob/main/text/0071-continue-trace-over-process-boundaries.md
299
+ * for more details.
300
+ */
301
+ function updateScopeFromEnvVariables ( ) : void {
302
+ const sentryUseEnvironment = process . env . SENTRY_USE_ENVIRONMENT ;
303
+ if (
304
+ ! [ 'false' , 'False' , 'FALSE' , 'n' , 'no' , 'No' , 'NO' , 'off' , 'Off' , 'OFF' , '0' ] . includes (
305
+ sentryUseEnvironment as string ,
306
+ )
307
+ ) {
308
+ const sentryTraceEnv = process . env . SENTRY_TRACE || '' ;
309
+ const baggageEnv = process . env . SENTRY_BAGGAGE ;
310
+
311
+ const traceparentData = extractTraceparentData ( sentryTraceEnv ) || { } ;
312
+ const { traceId, parentSpanId, parentSampled } = traceparentData ;
313
+ const propagationContext : PropagationContext = {
314
+ traceId : traceId || uuid4 ( ) ,
315
+ spanId : uuid4 ( ) . substring ( 16 ) ,
316
+ parentSpanId,
317
+ sampled : parentSampled === undefined ? false : parentSampled ,
318
+ } ;
319
+ const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext ( baggageEnv ) ;
320
+ if ( dynamicSamplingContext ) {
321
+ propagationContext . dsc = dynamicSamplingContext as DynamicSamplingContext ;
322
+ }
323
+ getCurrentHub ( ) . getScope ( ) . setPropagationContext ( propagationContext ) ;
324
+ }
325
+ }
0 commit comments