Skip to content

Commit 34f79d7

Browse files
committed
WIP
1 parent ee058e6 commit 34f79d7

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

packages/core/src/asyncContext.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Hub, Integration } from '@sentry/types';
22
import type { Scope } from '@sentry/types';
33
import { GLOBAL_OBJ } from '@sentry/utils';
4-
import type { startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from './tracing/trace';
4+
import type { startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan } from './tracing/trace';
55
import type { getActiveSpan } from './utils/spanUtils';
66

77
/**
@@ -62,6 +62,9 @@ export interface AsyncContextStrategy {
6262

6363
/** Make a span the active span in the context of the callback. */
6464
withActiveSpan?: typeof withActiveSpan;
65+
66+
/** Suppress tracing in the given callback, ensuring no spans are generated inside of it. */
67+
suppressTracing?: typeof suppressTracing;
6568
}
6669

6770
/**

packages/core/src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const DEFAULT_ENVIRONMENT = 'production';
2+
3+
export const SUPPRESS_TRACING_KEY = '__SENTRY_SUPPRESS_TRACING__';

packages/core/src/tracing/trace.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ClientOptions, Scope, SentrySpanArguments, Span, SpanTimeInput, St
33
import { propagationContextFromHeaders } from '@sentry/utils';
44
import type { AsyncContextStrategy } from '../asyncContext';
55
import { getMainCarrier } from '../asyncContext';
6+
import { SUPPRESS_TRACING_KEY } from '../constants';
67
import { getClient, getCurrentScope, getIsolationScope, withScope } from '../currentScopes';
78

89
import { getAsyncContextStrategy } from '../hub';
@@ -204,6 +205,20 @@ export function withActiveSpan<T>(span: Span | null, callback: (scope: Scope) =>
204205
});
205206
}
206207

208+
/** Suppress tracing in the given callback, ensuring no spans are generated inside of it. */
209+
export function suppressTracing<T>(callback: () => T): T {
210+
const acs = getAcs();
211+
212+
if (acs.suppressTracing) {
213+
return acs.suppressTracing(callback);
214+
}
215+
216+
return withScope(scope => {
217+
scope.setSDKProcessingMetadata({ [SUPPRESS_TRACING_KEY]: true });
218+
return callback();
219+
});
220+
}
221+
207222
function createChildSpanOrTransaction({
208223
parentSpan,
209224
spanContext,
@@ -237,6 +252,7 @@ function createChildSpanOrTransaction({
237252
parentSpanId,
238253
...spanContext,
239254
},
255+
scope,
240256
parentSampled,
241257
);
242258

@@ -258,6 +274,7 @@ function createChildSpanOrTransaction({
258274
parentSpanId,
259275
...spanContext,
260276
},
277+
scope,
261278
parentSampled,
262279
);
263280

@@ -296,20 +313,22 @@ function getAcs(): AsyncContextStrategy {
296313
return getAsyncContextStrategy(carrier);
297314
}
298315

299-
function _startRootSpan(spanArguments: SentrySpanArguments, parentSampled?: boolean): SentrySpan {
316+
function _startRootSpan(spanArguments: SentrySpanArguments, scope: Scope, parentSampled?: boolean): SentrySpan {
300317
const client = getClient();
301318
const options: Partial<ClientOptions> = (client && client.getOptions()) || {};
302319

303320
const { name = '', attributes } = spanArguments;
304-
const [sampled, sampleRate] = sampleSpan(options, {
305-
name,
306-
parentSampled,
307-
attributes,
308-
transactionContext: {
309-
name,
310-
parentSampled,
311-
},
312-
});
321+
const [sampled, sampleRate] = scope.getScopeData().sdkProcessingMetadata[SUPPRESS_TRACING_KEY]
322+
? [false]
323+
: sampleSpan(options, {
324+
name,
325+
parentSampled,
326+
attributes,
327+
transactionContext: {
328+
name,
329+
parentSampled,
330+
},
331+
});
313332

314333
const transaction = new SentrySpan({
315334
...spanArguments,

0 commit comments

Comments
 (0)