@@ -3,41 +3,51 @@ import type { Client, Scope, SerializedTraceData, Span } from '@sentry/core';
33import {
44 dynamicSamplingContextToSentryBaggageHeader ,
55 generateSentryTraceHeader ,
6- getActiveSpan ,
76 getCapturedScopesOnSpan ,
8- getCurrentScope ,
97 scopeToTraceparentHeader ,
108 spanToTraceparentHeader ,
119} from '@sentry/core' ;
1210import { getInjectionData } from '../propagator' ;
13- import { getContextFromScope } from './contextData' ;
11+ import { getContextFromScope , getScopesFromContext } from './contextData' ;
1412
1513/**
1614 * Otel-specific implementation of `getTraceData`.
1715 * @see `@sentry/core` version of `getTraceData` for more information
1816 */
19- export function getTraceData (
20- options : { span ?: Span ; scope ?: Scope ; client ?: Client ; propagateTraceparent ?: boolean } = { } ,
21- ) : SerializedTraceData {
22- const span = options . span || getActiveSpan ( ) ;
23- const scope = options . scope || ( span && getCapturedScopesOnSpan ( span ) . scope ) || getCurrentScope ( ) ;
17+ export function getTraceData ( options : { span ?: Span ; scope ?: Scope ; client ?: Client ; propagateTraceparent ?: boolean } = { } ) : SerializedTraceData {
18+ const { client, propagateTraceparent } = options ;
19+ let { span, scope } = options ;
2420
25- let ctx = getContextFromScope ( scope ) ?? api . context . active ( ) ;
21+ let ctx = ( scope && getContextFromScope ( scope ) ) ?? api . context . active ( ) ;
2622
2723 if ( span ) {
24+ const { scope } = getCapturedScopesOnSpan ( span ) ;
2825 // fall back to current context if for whatever reason we can't find the one of the span
29- ctx = getContextFromScope ( scope ) || api . trace . setSpan ( api . context . active ( ) , span ) ;
26+ ctx = ( scope && getContextFromScope ( scope ) ) || api . trace . setSpan ( api . context . active ( ) , span ) ;
27+ } else {
28+ span = api . trace . getSpan ( ctx ) ;
3029 }
3130
32- const { traceId, spanId, sampled, dynamicSamplingContext } = getInjectionData ( ctx , { scope, client : options . client } ) ;
31+ if ( ! scope ) {
32+ const scopes = getScopesFromContext ( ctx ) ;
33+ if ( scopes ) {
34+ scope = scopes . scope ;
35+ }
36+ }
37+
38+ const { traceId, spanId, sampled, dynamicSamplingContext } = getInjectionData ( ctx , { scope, client } ) ;
3339
3440 const traceData : SerializedTraceData = {
3541 'sentry-trace' : generateSentryTraceHeader ( traceId , spanId , sampled ) ,
3642 baggage : dynamicSamplingContextToSentryBaggageHeader ( dynamicSamplingContext ) ,
3743 } ;
3844
39- if ( options . propagateTraceparent ) {
40- traceData . traceparent = span ? spanToTraceparentHeader ( span ) : scopeToTraceparentHeader ( scope ) ;
45+ if ( propagateTraceparent ) {
46+ if ( span ) {
47+ traceData . traceparent = spanToTraceparentHeader ( span ) ;
48+ } else if ( scope ) {
49+ traceData . traceparent = scopeToTraceparentHeader ( scope ) ;
50+ }
4151 }
4252
4353 return traceData ;
0 commit comments