Skip to content

Commit

Permalink
fix(otel): Always set baggage regardless of active transaction (#6819)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad authored Jan 17, 2023
1 parent 8672efc commit 952d866
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
21 changes: 9 additions & 12 deletions packages/opentelemetry-node/src/propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,23 @@ export class SentryPropagator extends W3CBaggagePropagator {
return;
}

let baggage = propagation.getBaggage(context) || propagation.createBaggage({});

const span = SENTRY_SPAN_PROCESSOR_MAP.get(spanContext.spanId);
if (span) {
setter.set(carrier, SENTRY_TRACE_HEADER, span.toTraceparent());

if (span.transaction) {
const dynamicSamplingContext = span.transaction.getDynamicSamplingContext();

const baggage = propagation.getBaggage(context) || propagation.createBaggage({});
const baggageWithSentryInfo = Object.entries(dynamicSamplingContext).reduce<Baggage>(
(b, [dscKey, dscValue]) => {
if (dscValue) {
return b.setEntry(`${SENTRY_BAGGAGE_KEY_PREFIX}${dscKey}`, { value: dscValue });
}
return b;
},
baggage,
);
super.inject(propagation.setBaggage(context, baggageWithSentryInfo), carrier, setter);
baggage = Object.entries(dynamicSamplingContext).reduce<Baggage>((b, [dscKey, dscValue]) => {
if (dscValue) {
return b.setEntry(`${SENTRY_BAGGAGE_KEY_PREFIX}${dscKey}`, { value: dscValue });
}
return b;
}, baggage);
}
}
super.inject(propagation.setBaggage(context, baggage), carrier, setter);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-node/src/spanprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const SENTRY_SPAN_PROCESSOR_MAP: Map<SentrySpan['spanId'], SentrySpan> =
export class SentrySpanProcessor implements OtelSpanProcessor {
public constructor() {
addGlobalEventProcessor(event => {
const otelSpan = trace.getActiveSpan() as OtelSpan;
const otelSpan = trace && trace.getActiveSpan && (trace.getActiveSpan() as OtelSpan | undefined);
if (!otelSpan) {
return event;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/opentelemetry-node/test/propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ describe('SentryPropagator', () => {
);
});

it('should create baggage without active transaction', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};
const context = trace.setSpanContext(ROOT_CONTEXT, spanContext);
const baggage = propagation.createBaggage({ foo: { value: 'bar' } });
propagator.inject(propagation.setBaggage(context, baggage), carrier, defaultTextMapSetter);
expect(carrier[SENTRY_BAGGAGE_HEADER]).toBe('foo=bar');
});

it('should NOT set baggage and sentry-trace header if instrumentation is supressed', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
Expand Down

0 comments on commit 952d866

Please sign in to comment.