Skip to content

Commit 403c7ca

Browse files
committed
ensure we always have a DSC
1 parent a9b569f commit 403c7ca

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

packages/e2e-tests/test-applications/node-experimental-fastify-app/tests/propagation.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ test('Propagates trace for outgoing http requests', async ({ baseURL }) => {
4141

4242
// data is passed through from the inbound request, to verify we have the correct headers set
4343
const inboundHeaderSentryTrace = data.headers?.['sentry-trace'];
44+
const inboundHeaderBaggage = data.headers?.['baggage'];
45+
4446
expect(inboundHeaderSentryTrace).toEqual(`${traceId}-${outgoingHttpSpanId}-1`);
45-
// Baggage and DSC are not set
46-
expect(data.headers?.['baggage']).toEqual(undefined);
47+
expect(inboundHeaderBaggage).toBeDefined();
48+
49+
const baggage = (inboundHeaderBaggage || '').split(',');
50+
expect(baggage).toEqual(
51+
expect.arrayContaining([
52+
'sentry-environment=qa',
53+
`sentry-trace_id=${traceId}`,
54+
expect.stringMatching(/sentry-public_key=/),
55+
]),
56+
);
4757

4858
expect(outboundTransaction).toEqual(
4959
expect.objectContaining({

packages/node-experimental/src/opentelemetry/propagator.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import type { Baggage, Context, SpanContext, TextMapGetter, TextMapSetter } from '@opentelemetry/api';
22
import { propagation, trace, TraceFlags } from '@opentelemetry/api';
33
import { isTracingSuppressed, W3CBaggagePropagator } from '@opentelemetry/core';
4-
import type { PropagationContext } from '@sentry/types';
4+
import { getDynamicSamplingContextFromClient } from '@sentry/core';
5+
import type { DynamicSamplingContext, PropagationContext } from '@sentry/types';
56
import { generateSentryTraceHeader, SENTRY_BAGGAGE_KEY_PREFIX, tracingContextFromHeaders } from '@sentry/utils';
67

8+
import { getCurrentHub } from '../sdk/hub';
79
import { SENTRY_BAGGAGE_HEADER, SENTRY_PROPAGATION_CONTEXT_CONTEXT_KEY, SENTRY_TRACE_HEADER } from './../constants';
10+
import { getSpanScope } from './spanData';
811

912
/**
1013
* Injects and extracts `sentry-trace` and `baggage` headers from carriers.
@@ -23,7 +26,10 @@ export class SentryPropagator extends W3CBaggagePropagator {
2326
const propagationContext = context.getValue(SENTRY_PROPAGATION_CONTEXT_CONTEXT_KEY) as
2427
| PropagationContext
2528
| undefined;
26-
const dynamicSamplingContext = propagationContext?.dsc;
29+
30+
const { spanId, traceId, sampled } = getSentryTraceData(context, propagationContext);
31+
32+
const dynamicSamplingContext = propagationContext ? getDsc(context, propagationContext, traceId) : undefined;
2733

2834
if (dynamicSamplingContext) {
2935
baggage = Object.entries(dynamicSamplingContext).reduce<Baggage>((b, [dscKey, dscValue]) => {
@@ -34,8 +40,6 @@ export class SentryPropagator extends W3CBaggagePropagator {
3440
}, baggage);
3541
}
3642

37-
const { spanId, traceId, sampled } = getSentryTraceData(context, propagationContext);
38-
3943
setter.set(carrier, SENTRY_TRACE_HEADER, generateSentryTraceHeader(traceId, spanId, sampled));
4044

4145
super.inject(propagation.setBaggage(context, baggage), carrier, setter);
@@ -78,6 +82,28 @@ export class SentryPropagator extends W3CBaggagePropagator {
7882
}
7983
}
8084

85+
function getDsc(
86+
context: Context,
87+
propagationContext: PropagationContext,
88+
traceId: string | undefined,
89+
): DynamicSamplingContext | undefined {
90+
// If we have a DSC on the propagation context, we just use it
91+
if (propagationContext.dsc) {
92+
return propagationContext.dsc;
93+
}
94+
95+
// Else, we try to generate a new one
96+
const client = getCurrentHub().getClient();
97+
const activeSpan = trace.getSpan(context);
98+
const scope = activeSpan ? getSpanScope(activeSpan) : undefined;
99+
100+
if (client) {
101+
return getDynamicSamplingContextFromClient(traceId || propagationContext.traceId, client, scope);
102+
}
103+
104+
return undefined;
105+
}
106+
81107
function getSentryTraceData(
82108
context: Context,
83109
propagationContext: PropagationContext | undefined,

0 commit comments

Comments
 (0)