Skip to content

Commit 235620f

Browse files
committed
ref: rewrite to wrapSamplingDecision
1 parent 46bb0c7 commit 235620f

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

packages/opentelemetry/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ export { SentryPropagator } from './propagator';
3838
export { SentrySpanProcessor } from './spanProcessor';
3939
export {
4040
SentrySampler,
41-
sentrySamplerNoDecision,
42-
sentrySamplerNotSampled,
43-
sentrySamplerSampled,
41+
wrapSamplingDecision,
4442
} from './sampler';
4543

4644
export { openTelemetrySetupCheck } from './utils/setupCheck';

packages/opentelemetry/src/sampler.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class SentrySampler implements Sampler {
4141
const parentContext = parentSpan?.spanContext();
4242

4343
if (!hasTracingEnabled(options)) {
44-
return sentrySamplerNoDecision({ context, spanAttributes });
44+
return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
4545
}
4646

4747
// If we have a http.client span that has no local parent, we never want to sample it
@@ -51,7 +51,7 @@ export class SentrySampler implements Sampler {
5151
spanAttributes[SEMATTRS_HTTP_METHOD] &&
5252
(!parentSpan || parentContext?.isRemote)
5353
) {
54-
return sentrySamplerNoDecision({ context, spanAttributes });
54+
return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
5555
}
5656

5757
const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
@@ -68,7 +68,7 @@ export class SentrySampler implements Sampler {
6868
mutableSamplingDecision,
6969
);
7070
if (!mutableSamplingDecision.decision) {
71-
return sentrySamplerNoDecision({ context, spanAttributes });
71+
return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
7272
}
7373

7474
const [sampled, sampleRate] = sampleSpan(options, {
@@ -90,19 +90,19 @@ export class SentrySampler implements Sampler {
9090
DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
9191

9292
return {
93-
...sentrySamplerNotSampled({ context, spanAttributes }),
93+
...wrapSamplingDecision({ decision: SamplingDecision.NOT_RECORD, context, spanAttributes }),
9494
attributes,
9595
};
9696
}
9797

9898
if (!sampled) {
9999
return {
100-
...sentrySamplerNotSampled({ context, spanAttributes }),
100+
...wrapSamplingDecision({ decision: SamplingDecision.NOT_RECORD, context, spanAttributes }),
101101
attributes,
102102
};
103103
}
104104
return {
105-
...sentrySamplerSampled({ context, spanAttributes }),
105+
...wrapSamplingDecision({ decision: SamplingDecision.RECORD_AND_SAMPLED, context, spanAttributes }),
106106
attributes,
107107
};
108108
}
@@ -143,40 +143,28 @@ function getParentSampled(parentSpan: Span, traceId: string, spanName: string):
143143
}
144144

145145
/**
146-
* Returns a SamplingResult that indicates that a span was not sampled, but no definite decision was made yet.
147-
* This indicates to downstream SDKs that they may make their own decision.
146+
* Wrap a sampling decision with data that Sentry needs to work properly with it.
147+
* If you pass `decision: undefined`, it will be treated as `NOT_RECORDING`, but in contrast to passing `NOT_RECORDING`
148+
* it will not propagate this decision to downstream Sentry SDKs.
148149
*/
149-
export function sentrySamplerNoDecision({
150+
export function wrapSamplingDecision({
151+
decision,
150152
context,
151153
spanAttributes,
152-
}: { context: Context; spanAttributes: SpanAttributes }): SamplingResult {
154+
}: { decision: SamplingDecision | undefined; context: Context; spanAttributes: SpanAttributes }): SamplingResult {
153155
const traceState = getBaseTraceState(context, spanAttributes);
154156

155-
return { decision: SamplingDecision.NOT_RECORD, traceState };
156-
}
157-
158-
/**
159-
* Returns a SamplingResult that indicates that a span was not sampled.
160-
*/
161-
export function sentrySamplerNotSampled({
162-
context,
163-
spanAttributes,
164-
}: { context: Context; spanAttributes: SpanAttributes }): SamplingResult {
165-
const traceState = getBaseTraceState(context, spanAttributes).set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1');
166-
167-
return { decision: SamplingDecision.NOT_RECORD, traceState };
168-
}
157+
// If the decision is undefined, we treat it as NOT_RECORDING, but we don't propagate this decision to downstream SDKs
158+
// Which is done by not setting `SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING` traceState
159+
if (decision == undefined) {
160+
return { decision: SamplingDecision.NOT_RECORD, traceState };
161+
}
169162

170-
/**
171-
* Returns a SamplingResult that indicates that a span was sampled.
172-
*/
173-
export function sentrySamplerSampled({
174-
context,
175-
spanAttributes,
176-
}: { context: Context; spanAttributes: SpanAttributes }): SamplingResult {
177-
const traceState = getBaseTraceState(context, spanAttributes);
163+
if (decision === SamplingDecision.NOT_RECORD) {
164+
return { decision, traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1') };
165+
}
178166

179-
return { decision: SamplingDecision.RECORD_AND_SAMPLED, traceState };
167+
return { decision, traceState };
180168
}
181169

182170
function getBaseTraceState(context: Context, spanAttributes: SpanAttributes): TraceStateInterface {

0 commit comments

Comments
 (0)