Skip to content

Commit 952d866

Browse files
authored
fix(otel): Always set baggage regardless of active transaction (#6819)
1 parent 8672efc commit 952d866

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

packages/opentelemetry-node/src/propagator.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,23 @@ export class SentryPropagator extends W3CBaggagePropagator {
2828
return;
2929
}
3030

31+
let baggage = propagation.getBaggage(context) || propagation.createBaggage({});
32+
3133
const span = SENTRY_SPAN_PROCESSOR_MAP.get(spanContext.spanId);
3234
if (span) {
3335
setter.set(carrier, SENTRY_TRACE_HEADER, span.toTraceparent());
3436

3537
if (span.transaction) {
3638
const dynamicSamplingContext = span.transaction.getDynamicSamplingContext();
37-
38-
const baggage = propagation.getBaggage(context) || propagation.createBaggage({});
39-
const baggageWithSentryInfo = Object.entries(dynamicSamplingContext).reduce<Baggage>(
40-
(b, [dscKey, dscValue]) => {
41-
if (dscValue) {
42-
return b.setEntry(`${SENTRY_BAGGAGE_KEY_PREFIX}${dscKey}`, { value: dscValue });
43-
}
44-
return b;
45-
},
46-
baggage,
47-
);
48-
super.inject(propagation.setBaggage(context, baggageWithSentryInfo), carrier, setter);
39+
baggage = Object.entries(dynamicSamplingContext).reduce<Baggage>((b, [dscKey, dscValue]) => {
40+
if (dscValue) {
41+
return b.setEntry(`${SENTRY_BAGGAGE_KEY_PREFIX}${dscKey}`, { value: dscValue });
42+
}
43+
return b;
44+
}, baggage);
4945
}
5046
}
47+
super.inject(propagation.setBaggage(context, baggage), carrier, setter);
5148
}
5249

5350
/**

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const SENTRY_SPAN_PROCESSOR_MAP: Map<SentrySpan['spanId'], SentrySpan> =
2323
export class SentrySpanProcessor implements OtelSpanProcessor {
2424
public constructor() {
2525
addGlobalEventProcessor(event => {
26-
const otelSpan = trace.getActiveSpan() as OtelSpan;
26+
const otelSpan = trace && trace.getActiveSpan && (trace.getActiveSpan() as OtelSpan | undefined);
2727
if (!otelSpan) {
2828
return event;
2929
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ describe('SentryPropagator', () => {
166166
);
167167
});
168168

169+
it('should create baggage without active transaction', () => {
170+
const spanContext = {
171+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
172+
spanId: '6e0c63257de34c92',
173+
traceFlags: TraceFlags.SAMPLED,
174+
};
175+
const context = trace.setSpanContext(ROOT_CONTEXT, spanContext);
176+
const baggage = propagation.createBaggage({ foo: { value: 'bar' } });
177+
propagator.inject(propagation.setBaggage(context, baggage), carrier, defaultTextMapSetter);
178+
expect(carrier[SENTRY_BAGGAGE_HEADER]).toBe('foo=bar');
179+
});
180+
169181
it('should NOT set baggage and sentry-trace header if instrumentation is supressed', () => {
170182
const spanContext = {
171183
traceId: 'd4cda95b652f4a1592b449d5929fda1b',

0 commit comments

Comments
 (0)