Skip to content

Commit 66d2e77

Browse files
committed
ref(core): Ensure non-sampled spans are NonRecordingSpans
1 parent e8e84ea commit 66d2e77

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

packages/core/src/tracing/idleSpan.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
spanToJSON,
1616
} from '../utils/spanUtils';
1717
import { SentryNonRecordingSpan } from './sentryNonRecordingSpan';
18+
import { recordDroppedRootSpan } from './sentrySpan';
1819
import { SPAN_STATUS_ERROR } from './spanstatus';
1920
import { startInactiveSpan } from './trace';
2021

@@ -124,6 +125,14 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
124125
beforeSpanEnd(span);
125126
}
126127

128+
// If the span is non-recording, nothing more to do here...
129+
// This is the case if tracing is enabled but this specific span was not sampled
130+
// We make sure to record this as dropped in this case
131+
if (thisArg instanceof SentryNonRecordingSpan) {
132+
recordDroppedRootSpan(thisArg);
133+
return;
134+
}
135+
127136
// Just ensuring that this keeps working, even if we ever have more arguments here
128137
const [definedEndTimestamp, ...rest] = args;
129138
const timestamp = definedEndTimestamp || timestampInSeconds();

packages/core/src/tracing/sentrySpan.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
getRootSpan,
3535
getSpanDescendants,
3636
getStatusMessage,
37+
spanIsSampled,
3738
spanTimeInputToSeconds,
3839
spanToJSON,
3940
spanToTransactionTraceContext,
@@ -333,17 +334,9 @@ export class SentrySpan implements Span {
333334
}
334335

335336
const { scope: capturedSpanScope, isolationScope: capturedSpanIsolationScope } = getCapturedScopesOnSpan(this);
336-
const scope = capturedSpanScope || getCurrentScope();
337-
const client = scope.getClient() || getClient();
338337

339338
if (this._sampled !== true) {
340-
// At this point if `sampled !== true` we want to discard the transaction.
341-
DEBUG_BUILD && logger.log('[Tracing] Discarding transaction because its trace was not chosen to be sampled.');
342-
343-
if (client) {
344-
client.recordDroppedEvent('sample_rate', 'transaction');
345-
}
346-
339+
recordDroppedRootSpan(this);
347340
return undefined;
348341
}
349342

@@ -442,3 +435,18 @@ function sendSpanEnvelope(envelope: SpanEnvelope): void {
442435
// eslint-disable-next-line @typescript-eslint/no-floating-promises
443436
client.sendEnvelope(envelope);
444437
}
438+
439+
/** Record a dropped root span. */
440+
export function recordDroppedRootSpan(span: Span): void {
441+
const { scope: capturedSpanScope } = getCapturedScopesOnSpan(span);
442+
const scope = capturedSpanScope || getCurrentScope();
443+
const client = scope.getClient() || getClient();
444+
445+
if (!spanIsSampled(span)) {
446+
DEBUG_BUILD && logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
447+
448+
if (client) {
449+
client.recordDroppedEvent('sample_rate', 'transaction');
450+
}
451+
}
452+
}

packages/core/src/tracing/trace.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,13 @@ function getAcs(): AsyncContextStrategy {
370370
return getAsyncContextStrategy(carrier);
371371
}
372372

373-
function _startRootSpan(spanArguments: SentrySpanArguments, scope: Scope, parentSampled?: boolean): SentrySpan {
373+
function _startRootSpan(
374+
spanArguments: SentrySpanArguments,
375+
scope: Scope,
376+
parentSampled?: boolean,
377+
): SentrySpan | SentryNonRecordingSpan {
374378
const client = getClient();
375-
const options: Partial<ClientOptions> = (client && client.getOptions()) || {};
379+
const options: Partial<ClientOptions> = client?.getOptions() || {};
376380

377381
const { name = '', attributes } = spanArguments;
378382
const [sampled, sampleRate] = scope.getScopeData().sdkProcessingMetadata[SUPPRESS_TRACING_KEY]
@@ -387,14 +391,17 @@ function _startRootSpan(spanArguments: SentrySpanArguments, scope: Scope, parent
387391
},
388392
});
389393

390-
const rootSpan = new SentrySpan({
391-
...spanArguments,
392-
attributes: {
393-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
394-
...spanArguments.attributes,
395-
},
396-
sampled,
397-
});
394+
const rootSpan = sampled
395+
? new SentrySpan({
396+
...spanArguments,
397+
attributes: {
398+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
399+
...spanArguments.attributes,
400+
},
401+
sampled,
402+
})
403+
: new SentryNonRecordingSpan({ traceId: spanArguments.traceId });
404+
398405
if (sampleRate !== undefined) {
399406
rootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, sampleRate);
400407
}

0 commit comments

Comments
 (0)