@@ -6,13 +6,16 @@ import {
66 initAndBind ,
77 Integrations as CoreIntegrations ,
88} from '@sentry/core' ;
9- import type { SessionStatus , StackParser } from '@sentry/types' ;
9+ import type { DynamicSamplingContext , PropagationContext , SessionStatus , StackParser } from '@sentry/types' ;
1010import {
11+ baggageHeaderToDynamicSamplingContext ,
1112 createStackParser ,
13+ extractTraceparentData ,
1214 GLOBAL_OBJ ,
1315 logger ,
1416 nodeStackLineParser ,
1517 stackParserFromStackParserOptions ,
18+ uuid4 ,
1619} from '@sentry/utils' ;
1720
1821import { setNodeAsyncContextStrategy } from './async' ;
@@ -129,8 +132,9 @@ export function init(options: NodeOptions = {}): void {
129132 options . dsn = process . env . SENTRY_DSN ;
130133 }
131134
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 ) ;
134138 if ( isFinite ( tracesSampleRate ) ) {
135139 options . tracesSampleRate = tracesSampleRate ;
136140 }
@@ -171,6 +175,8 @@ export function init(options: NodeOptions = {}): void {
171175 if ( options . autoSessionTracking ) {
172176 startSessionTracking ( ) ;
173177 }
178+
179+ updateScopeFromEnvVariables ( ) ;
174180}
175181
176182/**
@@ -285,3 +291,35 @@ function startSessionTracking(): void {
285291 if ( session && ! terminalStates . includes ( session . status ) ) hub . endSession ( ) ;
286292 } ) ;
287293}
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