Skip to content

Commit 7b423e8

Browse files
committed
ref: Use getInjectionData for otel-specific getTraceData
1 parent ad67471 commit 7b423e8

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

packages/opentelemetry/src/asyncContextStrategy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from '.
1212
import type { CurrentScopes } from './types';
1313
import { getScopesFromContext } from './utils/contextData';
1414
import { getActiveSpan } from './utils/getActiveSpan';
15+
import { getTraceData } from './utils/getTraceData';
1516
import { suppressTracing } from './utils/suppressTracing';
1617

1718
/**
@@ -103,7 +104,7 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
103104
startInactiveSpan,
104105
getActiveSpan,
105106
suppressTracing,
106-
// getTraceData,
107+
getTraceData,
107108
// The types here don't fully align, because our own `Span` type is narrower
108109
// than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
109110
withActiveSpan: withActiveSpan as typeof defaultWithActiveSpan,

packages/opentelemetry/src/propagator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ export class SentryPropagator extends W3CBaggagePropagator {
186186
}
187187
}
188188

189-
function getInjectionData(context: Context): {
189+
/**
190+
* Get propagation injection data for the given context.
191+
*/
192+
export function getInjectionData(context: Context): {
190193
dynamicSamplingContext: Partial<DynamicSamplingContext> | undefined;
191194
traceId: string | undefined;
192195
spanId: string | undefined;
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
import * as api from '@opentelemetry/api';
22
import { getCapturedScopesOnSpan } from '@sentry/core';
33
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';
56
import { getContextFromScope } from './contextData';
67

78
/**
89
* Otel-specific implementation of `getTraceData`.
910
* @see `@sentry/core` version of `getTraceData` for more information
1011
*/
1112
export function getTraceData({ span }: { span?: Span } = {}): SerializedTraceData {
12-
const headersObject: Record<string, string> = {};
13+
let ctx = api.context.active();
1314

1415
if (span) {
1516
const { scope } = getCapturedScopesOnSpan(span);
1617
// 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);
2219
}
2320

24-
if (!headersObject['sentry-trace']) {
25-
return {};
26-
}
21+
const { traceId, spanId, sampled, dynamicSamplingContext } = getInjectionData(ctx);
2722

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+
};
3227
}

0 commit comments

Comments
 (0)