Skip to content

Commit 045b41f

Browse files
author
Luca Forstner
committed
always apply root span sample rate to dsc
1 parent 83ac885 commit 045b41f

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

packages/core/src/tracing/dynamicSamplingContext.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,21 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<
7575
}
7676

7777
const rootSpan = getRootSpan(span);
78+
const rootSpanJson = spanToJSON(rootSpan);
79+
const rootSpanAttributes = rootSpanJson.data;
80+
const maybeSampleRate = rootSpanAttributes[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
81+
82+
function applyRootSpanSampleRateToDsc(dsc: Partial<DynamicSamplingContext>): Partial<DynamicSamplingContext> {
83+
if (maybeSampleRate != null) {
84+
dsc.sample_rate = `${maybeSampleRate}`;
85+
}
86+
return dsc;
87+
}
7888

7989
// For core implementation, we freeze the DSC onto the span as a non-enumerable property
8090
const frozenDsc = (rootSpan as SpanWithMaybeDsc)[FROZEN_DSC_FIELD];
8191
if (frozenDsc) {
82-
return frozenDsc;
92+
return applyRootSpanSampleRateToDsc(frozenDsc);
8393
}
8494

8595
// For OpenTelemetry, we freeze the DSC on the trace state
@@ -90,24 +100,17 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<
90100
const dscOnTraceState = traceStateDsc && baggageHeaderToDynamicSamplingContext(traceStateDsc);
91101

92102
if (dscOnTraceState) {
93-
return dscOnTraceState;
103+
return applyRootSpanSampleRateToDsc(dscOnTraceState);
94104
}
95105

96106
// Else, we generate it from the span
97107
const dsc = getDynamicSamplingContextFromClient(span.spanContext().traceId, client);
98-
const jsonSpan = spanToJSON(rootSpan);
99-
const attributes = jsonSpan.data;
100-
const maybeSampleRate = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
101-
102-
if (maybeSampleRate != null) {
103-
dsc.sample_rate = `${maybeSampleRate}`;
104-
}
105108

106109
// We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII
107-
const source = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
110+
const source = rootSpanAttributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
108111

109112
// after JSON conversion, txn.name becomes jsonSpan.description
110-
const name = jsonSpan.description;
113+
const name = rootSpanJson.description;
111114
if (source !== 'url' && name) {
112115
dsc.transaction = name;
113116
}
@@ -122,7 +125,7 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<
122125

123126
client.emit('createDsc', dsc, rootSpan);
124127

125-
return dsc;
128+
return applyRootSpanSampleRateToDsc(dsc);
126129
}
127130

128131
/**

0 commit comments

Comments
 (0)