Skip to content

fix(otel): Always set baggage regardless of active transaction #6819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snuck this change in as it seems that getActiveSpan is not defined for some versions - will investigate more but at least this unhandled errors in production.

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