|
1 | 1 | import * as api from '@opentelemetry/api'; |
2 | 2 | import { getCapturedScopesOnSpan } from '@sentry/core'; |
3 | 3 | import type { SerializedTraceData, Span } from '@sentry/types'; |
4 | | -import { dropUndefinedKeys } from '@sentry/utils'; |
| 4 | +import { dynamicSamplingContextToSentryBaggageHeader, generateSentryTraceHeader } from '@sentry/utils'; |
| 5 | +import { getInjectionData } from '../propagator'; |
5 | 6 | import { getContextFromScope } from './contextData'; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Otel-specific implementation of `getTraceData`. |
9 | 10 | * @see `@sentry/core` version of `getTraceData` for more information |
10 | 11 | */ |
11 | 12 | export function getTraceData({ span }: { span?: Span } = {}): SerializedTraceData { |
12 | | - const headersObject: Record<string, string> = {}; |
| 13 | + let ctx = api.context.active(); |
13 | 14 |
|
14 | 15 | if (span) { |
15 | 16 | const { scope } = getCapturedScopesOnSpan(span); |
16 | 17 | // fall back to current context if for whatever reason we can't find the one of the span |
17 | | - const ctx = (scope && getContextFromScope(scope)) || api.trace.setSpan(api.context.active(), span); |
18 | | - |
19 | | - api.propagation.inject(ctx, headersObject); |
20 | | - } else { |
21 | | - api.propagation.inject(api.context.active(), headersObject); |
| 18 | + ctx = (scope && getContextFromScope(scope)) || api.trace.setSpan(api.context.active(), span); |
22 | 19 | } |
23 | 20 |
|
24 | | - if (!headersObject['sentry-trace']) { |
25 | | - return {}; |
26 | | - } |
| 21 | + const { traceId, spanId, sampled, dynamicSamplingContext } = getInjectionData(ctx); |
27 | 22 |
|
28 | | - return dropUndefinedKeys({ |
29 | | - 'sentry-trace': headersObject['sentry-trace'], |
30 | | - baggage: headersObject.baggage, |
31 | | - }); |
| 23 | + return { |
| 24 | + 'sentry-trace': generateSentryTraceHeader(traceId, spanId, sampled), |
| 25 | + baggage: dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext), |
| 26 | + }; |
32 | 27 | } |
0 commit comments