Skip to content

Commit 7fcd925

Browse files
committed
move hasSpansEnabled gate into instrumentation setup
1 parent 62db407 commit 7fcd925

3 files changed

Lines changed: 46 additions & 21 deletions

File tree

packages/nextjs/src/server/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
getRootSpan,
1212
getVercelEnv,
1313
GLOBAL_OBJ,
14-
hasSpansEnabled,
1514
} from '@sentry/core';
1615
import type { NodeClient, NodeOptions } from '@sentry/node';
1716
import { getDefaultIntegrations, httpIntegration, init as nodeInit } from '@sentry/node';
@@ -143,9 +142,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
143142
customDefaultIntegrations.push(distDirRewriteFramesIntegration({ distDirName }));
144143
}
145144

146-
if (hasSpansEnabled(options)) {
147-
customDefaultIntegrations.push(nextjsUseCacheIntegration());
148-
}
145+
customDefaultIntegrations.push(nextjsUseCacheIntegration());
149146

150147
// Detect if running on OpenNext/Cloudflare and get runtime config
151148
const cloudflareConfig = getCloudflareRuntimeConfig();

packages/nextjs/src/server/useCacheInstrumentation.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getActiveSpan,
1111
getClient,
1212
hasSpanStreamingEnabled,
13+
hasSpansEnabled,
1314
SEMANTIC_ATTRIBUTE_CACHE_HIT,
1415
SEMANTIC_ATTRIBUTE_CACHE_KEY,
1516
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
@@ -279,8 +280,11 @@ export function _instrumentUseCacheHandlers(): void {
279280
export const nextjsUseCacheIntegration = defineIntegration(() => {
280281
return {
281282
name: INTEGRATION_NAME,
282-
setupOnce() {
283-
_instrumentUseCacheHandlers();
283+
setup(client) {
284+
// The resolved client options also cover tracing enabled via `SENTRY_TRACES_SAMPLE_RATE`.
285+
if (hasSpansEnabled(client.getOptions())) {
286+
_instrumentUseCacheHandlers();
287+
}
284288
},
285289
};
286290
});

packages/nextjs/test/serverSdk.test.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,48 @@ describe('Server init()', () => {
100100
expect(onUncaughtExceptionIntegration).toBeDefined();
101101
});
102102

103-
it('adds the `use cache` integration when tracing is enabled', () => {
104-
init({ tracesSampleRate: 1 });
103+
describe('`use cache` integration', () => {
104+
const CACHE_HANDLERS_INSTRUMENTED = Symbol.for('sentry.nextjs.cacheHandlersInstrumented');
105+
const DSN = 'https://public@dsn.ingest.sentry.io/1337';
105106

106-
expect(nodeInit).toHaveBeenLastCalledWith(
107-
expect.objectContaining({
108-
defaultIntegrations: expect.arrayContaining([expect.objectContaining({ name: 'NextjsUseCache' })]),
109-
}),
110-
);
111-
});
107+
function isUseCacheInstrumented(): boolean {
108+
return (globalThis as Record<symbol, unknown>)[CACHE_HANDLERS_INSTRUMENTED] === true;
109+
}
112110

113-
it('does not add the `use cache` integration when tracing is disabled', () => {
114-
init({});
111+
afterEach(() => {
112+
vi.unstubAllEnvs();
113+
Reflect.deleteProperty(globalThis, CACHE_HANDLERS_INSTRUMENTED);
114+
});
115115

116-
expect(nodeInit).toHaveBeenLastCalledWith(
117-
expect.objectContaining({
118-
defaultIntegrations: expect.not.arrayContaining([expect.objectContaining({ name: 'NextjsUseCache' })]),
119-
}),
120-
);
116+
it('adds the integration to the default integrations', () => {
117+
init({});
118+
119+
expect(nodeInit).toHaveBeenLastCalledWith(
120+
expect.objectContaining({
121+
defaultIntegrations: expect.arrayContaining([expect.objectContaining({ name: 'NextjsUseCache' })]),
122+
}),
123+
);
124+
});
125+
126+
it('instruments the cache handlers when tracing is enabled', () => {
127+
init({ dsn: DSN, tracesSampleRate: 1 });
128+
129+
expect(isUseCacheInstrumented()).toBe(true);
130+
});
131+
132+
it('instruments the cache handlers when tracing is enabled via `SENTRY_TRACES_SAMPLE_RATE`', () => {
133+
vi.stubEnv('SENTRY_TRACES_SAMPLE_RATE', '1');
134+
135+
init({ dsn: DSN });
136+
137+
expect(isUseCacheInstrumented()).toBe(true);
138+
});
139+
140+
it('does not instrument the cache handlers when tracing is disabled', () => {
141+
init({ dsn: DSN });
142+
143+
expect(isUseCacheInstrumented()).toBe(false);
144+
});
121145
});
122146

123147
it('supports passing unrelated integrations through options', () => {

0 commit comments

Comments
 (0)