Skip to content

Commit 46a527b

Browse files
authored
feat(tracing): Add enableTracing option (#7238)
1 parent c5c223c commit 46a527b

34 files changed

+203
-68
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott
66

7+
This release adds a new `enableTracing` option, which can be used instead of `tracesSampleRate` for an easier setup.
8+
Related to this, the `hasTracingEnabled` utility function was moved from `@sentry/tracing` to `@sentry/core`.
9+
The old export from `@sentry/tracing` has been deprecated and will be removed in v8.
10+
711
## 7.37.2
812

913
This release includes changes and fixes around text masking and blocking in Replay's `rrweb` dependency. See versions [1.102.0](https://github.com/getsentry/rrweb/releases/tag/1.102.0) and [1.103.0](https://github.com/getsentry/rrweb/releases/tag/1.103.0).

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"lint:eslint": "lerna run lint:eslint",
2626
"postpublish": "lerna run --stream --concurrency 1 postpublish",
2727
"test": "lerna run --ignore @sentry-internal/* test",
28+
"test:unit": "lerna run --ignore @sentry-internal/* test:unit",
2829
"test-ci-browser": "lerna run test --ignore \"@sentry/{node,opentelemetry-node,serverless,nextjs,remix,gatsby}\" --ignore @sentry-internal/*",
2930
"test-ci-node": "ts-node ./scripts/node-unit-tests.ts",
3031
"test:update-snapshots": "lerna run test:update-snapshots"

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export { SDK_VERSION } from './version';
3030
export { getIntegrationsToSetup } from './integration';
3131
export { FunctionToString, InboundFilters } from './integrations';
3232
export { prepareEvent } from './utils/prepareEvent';
33+
export { hasTracingEnabled } from './utils/hasTracingEnabled';
3334

3435
import * as Integrations from './integrations';
3536

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Options } from '@sentry/types';
2+
3+
import { getCurrentHub } from '../hub';
4+
5+
// Treeshakable guard to remove all code related to tracing
6+
declare const __SENTRY_TRACING__: boolean | undefined;
7+
8+
/**
9+
* Determines if tracing is currently enabled.
10+
*
11+
* Tracing is enabled when at least one of `tracesSampleRate` and `tracesSampler` is defined in the SDK config.
12+
*/
13+
export function hasTracingEnabled(
14+
maybeOptions?: Pick<Options, 'tracesSampleRate' | 'tracesSampler' | 'enableTracing'> | undefined,
15+
): boolean {
16+
if (typeof __SENTRY_TRACING__ === 'boolean' && !__SENTRY_TRACING__) {
17+
return false;
18+
}
19+
20+
const client = getCurrentHub().getClient();
21+
const options = maybeOptions || (client && client.getOptions());
22+
return !!options && (options.enableTracing || 'tracesSampleRate' in options || 'tracesSampler' in options);
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { hasTracingEnabled } from '../../../src';
2+
3+
describe('hasTracingEnabled', () => {
4+
const tracesSampler = () => 1;
5+
const tracesSampleRate = 1;
6+
it.each([
7+
['No options', undefined, false],
8+
['No tracesSampler or tracesSampleRate or enableTracing', {}, false],
9+
['With tracesSampler', { tracesSampler }, true],
10+
['With tracesSampleRate', { tracesSampleRate }, true],
11+
['With enableTracing=true', { enableTracing: true }, true],
12+
['With enableTracing=false', { enableTracing: false }, false],
13+
['With tracesSampler && enableTracing=false', { tracesSampler, enableTracing: false }, true],
14+
['With tracesSampleRate && enableTracing=false', { tracesSampler, enableTracing: false }, true],
15+
['With tracesSampler and tracesSampleRate', { tracesSampler, tracesSampleRate }, true],
16+
[
17+
'With tracesSampler and tracesSampleRate and enableTracing=true',
18+
{ tracesSampler, tracesSampleRate, enableTracing: true },
19+
true,
20+
],
21+
[
22+
'With tracesSampler and tracesSampleRate and enableTracing=false',
23+
{ tracesSampler, tracesSampleRate, enableTracing: false },
24+
true,
25+
],
26+
])(
27+
'%s',
28+
(_: string, input: Parameters<typeof hasTracingEnabled>[0], output: ReturnType<typeof hasTracingEnabled>) => {
29+
expect(hasTracingEnabled(input)).toBe(output);
30+
},
31+
);
32+
});

packages/gatsby/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"access": "public"
2121
},
2222
"dependencies": {
23+
"@sentry/core": "7.38.0",
2324
"@sentry/react": "7.38.0",
2425
"@sentry/tracing": "7.38.0",
2526
"@sentry/types": "7.38.0",

packages/gatsby/src/utils/integrations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { hasTracingEnabled } from '@sentry/core';
12
import * as Tracing from '@sentry/tracing';
23
import type { Integration } from '@sentry/types';
34

@@ -13,7 +14,7 @@ export type UserIntegrations = Integration[] | UserFnIntegrations;
1314
* @param options The options users have defined.
1415
*/
1516
export function getIntegrationsFromOptions(options: GatsbyOptions): UserIntegrations {
16-
const isTracingEnabled = Tracing.hasTracingEnabled(options);
17+
const isTracingEnabled = hasTracingEnabled(options);
1718
if (options.integrations === undefined) {
1819
return getIntegrationsFromArray([], isTracingEnabled);
1920
} else if (Array.isArray(options.integrations)) {

packages/nextjs/src/client/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { hasTracingEnabled } from '@sentry/core';
12
import { RewriteFrames } from '@sentry/integrations';
23
import type { BrowserOptions } from '@sentry/react';
34
import { configureScope, init as reactInit, Integrations } from '@sentry/react';
4-
import { BrowserTracing, defaultRequestInstrumentationOptions, hasTracingEnabled } from '@sentry/tracing';
5+
import { BrowserTracing, defaultRequestInstrumentationOptions } from '@sentry/tracing';
56
import type { EventProcessor } from '@sentry/types';
67

78
import { getVercelEnv } from '../common/getVercelEnv';

packages/nextjs/src/edge/utils/edgeWrapperUtils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { captureException, getCurrentHub, startTransaction } from '@sentry/core';
2-
import { hasTracingEnabled } from '@sentry/tracing';
1+
import { captureException, getCurrentHub, hasTracingEnabled, startTransaction } from '@sentry/core';
32
import type { Span } from '@sentry/types';
43
import {
54
addExceptionMechanism,

packages/nextjs/src/server/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Carrier } from '@sentry/core';
2-
import { getHubFromCarrier, getMainCarrier } from '@sentry/core';
2+
import { getHubFromCarrier, getMainCarrier, hasTracingEnabled } from '@sentry/core';
33
import { RewriteFrames } from '@sentry/integrations';
44
import type { NodeOptions } from '@sentry/node';
55
import { configureScope, getCurrentHub, init as nodeInit, Integrations } from '@sentry/node';
6-
import { hasTracingEnabled } from '@sentry/tracing';
76
import type { EventProcessor } from '@sentry/types';
87
import { escapeStringForRegex, logger } from '@sentry/utils';
98
import * as domainModule from 'domain';

0 commit comments

Comments
 (0)