Skip to content

Commit aadd855

Browse files
authored
fix(otel): Set trace context via Otel Span instead of Sentry span (#6724)
1 parent e26e835 commit aadd855

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,22 @@ export const SENTRY_SPAN_PROCESSOR_MAP: Map<SentrySpan['spanId'], SentrySpan> =
2222
export class SentrySpanProcessor implements OtelSpanProcessor {
2323
public constructor() {
2424
addGlobalEventProcessor(event => {
25-
const otelSpan = trace.getActiveSpan();
25+
const otelSpan = trace.getActiveSpan() as OtelSpan;
2626
if (!otelSpan) {
2727
return event;
2828
}
2929

30-
const otelSpanId = otelSpan.spanContext().spanId;
31-
const sentrySpan = SENTRY_SPAN_PROCESSOR_MAP.get(otelSpanId);
32-
33-
if (!sentrySpan) {
34-
return event;
35-
}
30+
const otelSpanContext = otelSpan.spanContext();
3631

3732
// If event has already set `trace` context, use that one.
38-
// This happens in the case of transaction events.
39-
event.contexts = { trace: sentrySpan.getTraceContext(), ...event.contexts };
40-
const transactionName = sentrySpan.transaction && sentrySpan.transaction.name;
41-
if (transactionName) {
42-
event.tags = { transaction: transactionName, ...event.tags };
43-
}
33+
event.contexts = {
34+
trace: {
35+
trace_id: otelSpanContext.traceId,
36+
span_id: otelSpanContext.spanId,
37+
parent_span_id: otelSpan.parentSpanId,
38+
},
39+
...event.contexts,
40+
};
4441

4542
return event;
4643
});

packages/opentelemetry-node/test/spanprocessor.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,6 @@ describe('SentrySpanProcessor', () => {
744744
expect(sentryEvent).toBeDefined();
745745
expect(sentryEvent.exception).toBeDefined();
746746
expect(sentryEvent.contexts.trace).toEqual({
747-
description: otelSpan.name,
748747
parent_span_id: otelSpan.parentSpanId,
749748
span_id: otelSpan.spanContext().spanId,
750749
trace_id: otelSpan.spanContext().traceId,

0 commit comments

Comments
 (0)