Skip to content

fix(node): Fix preloading of instrumentation #17403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cursor/BUGBOT.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ Do not flag the issues below if they appear in tests.
- If there's no direct span that's wrapping the captured exception, apply a proper `type` value, following the same naming
convention as the `SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN` value.
- When calling `startSpan`, check if error cases are handled. If flag that it might make sense to try/catch and call `captureException`.
- When calling `generateInstrumentationOnce`, the passed in name MUST match the name of the integration that uses it. If there are more than one instrumentations, they need to follow the pattern `${INSTRUMENTATION_NAME}.some-suffix`.
6 changes: 3 additions & 3 deletions packages/nestjs/src/integrations/nest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { SentryNestInstrumentation } from './sentry-nest-instrumentation';

const INTEGRATION_NAME = 'Nest';

const instrumentNestCore = generateInstrumentOnce('Nest-Core', () => {
const instrumentNestCore = generateInstrumentOnce(`${INTEGRATION_NAME}.Core`, () => {
return new NestInstrumentationCore();
});

const instrumentNestCommon = generateInstrumentOnce('Nest-Common', () => {
const instrumentNestCommon = generateInstrumentOnce(`${INTEGRATION_NAME}.Common`, () => {
return new SentryNestInstrumentation();
});

const instrumentNestEvent = generateInstrumentOnce('Nest-Event', () => {
const instrumentNestEvent = generateInstrumentOnce(`${INTEGRATION_NAME}.Event`, () => {
return new SentryNestEventInstrumentation();
});

Expand Down
9 changes: 5 additions & 4 deletions packages/node/src/integrations/tracing/fastify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ interface FastifyHandlerOptions {
}

const INTEGRATION_NAME = 'Fastify';
const INTEGRATION_NAME_V5 = 'Fastify-V5';
const INTEGRATION_NAME_V3 = 'Fastify-V3';

export const instrumentFastifyV3 = generateInstrumentOnce(INTEGRATION_NAME_V3, () => new FastifyInstrumentationV3());
export const instrumentFastifyV3 = generateInstrumentOnce(
`${INTEGRATION_NAME}.v3`,
() => new FastifyInstrumentationV3(),
);

function getFastifyIntegration(): ReturnType<typeof _fastifyIntegration> | undefined {
const client = getClient();
Expand Down Expand Up @@ -135,7 +136,7 @@ function handleFastifyError(
}
}

export const instrumentFastify = generateInstrumentOnce(INTEGRATION_NAME_V5, () => {
export const instrumentFastify = generateInstrumentOnce(`${INTEGRATION_NAME}.v5`, () => {
const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation();
const plugin = fastifyOtelInstrumentationInstance.plugin();

Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/integrations/tracing/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ const cacheResponseHook: RedisResponseCustomAttributeFunction = (span: Span, red
span.updateName(truncate(spanDescription, 1024));
};

const instrumentIORedis = generateInstrumentOnce('IORedis', () => {
const instrumentIORedis = generateInstrumentOnce(`${INTEGRATION_NAME}.IORedis`, () => {
return new IORedisInstrumentation({
responseHook: cacheResponseHook,
});
});

const instrumentRedisModule = generateInstrumentOnce('Redis', () => {
const instrumentRedisModule = generateInstrumentOnce(`${INTEGRATION_NAME}.Redis`, () => {
return new RedisInstrumentation({
responseHook: cacheResponseHook,
});
Expand Down
6 changes: 5 additions & 1 deletion packages/node/src/sdk/initOtel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ function getPreloadMethods(integrationNames?: string[]): ((() => void) & { id: s
return instruments;
}

return instruments.filter(instrumentation => integrationNames.includes(instrumentation.id));
// We match exact matches of instrumentation, but also match prefixes, e.g. "Fastify.v5" will match "Fastify"
return instruments.filter(instrumentation => {
const id = instrumentation.id;
return integrationNames.some(integrationName => id === integrationName || id.startsWith(`${integrationName}.`));
});
}

/** Just exported for tests. */
Expand Down
23 changes: 21 additions & 2 deletions packages/node/test/sdk/preload.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import { debug } from '@sentry/core';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { resetGlobals } from '../helpers/mockSdkInit';

describe('preload', () => {
beforeEach(() => {
// Mock this to prevent conflicts with other tests
vi.mock('../../src/integrations/tracing', async (importOriginal: () => Promise<Record<string, unknown>>) => {
const actual = await importOriginal();
return {
...actual,
getOpenTelemetryInstrumentationToPreload: () => [
Object.assign(vi.fn(), { id: 'Http.sentry' }),
Object.assign(vi.fn(), { id: 'Http' }),
Object.assign(vi.fn(), { id: 'Express' }),
Object.assign(vi.fn(), { id: 'Graphql' }),
],
};
});
});

afterEach(() => {
vi.resetAllMocks();
debug.disable();
resetGlobals();

delete process.env.SENTRY_DEBUG;
delete process.env.SENTRY_PRELOAD_INTEGRATIONS;
Expand All @@ -29,6 +46,7 @@ describe('preload', () => {

await import('../../src/preload');

expect(logSpy).toHaveBeenCalledWith('Sentry Logger [log]:', '[Sentry] Preloaded Http.sentry instrumentation');
expect(logSpy).toHaveBeenCalledWith('Sentry Logger [log]:', '[Sentry] Preloaded Http instrumentation');
expect(logSpy).toHaveBeenCalledWith('Sentry Logger [log]:', '[Sentry] Preloaded Express instrumentation');
expect(logSpy).toHaveBeenCalledWith('Sentry Logger [log]:', '[Sentry] Preloaded Graphql instrumentation');
Expand All @@ -44,6 +62,7 @@ describe('preload', () => {

await import('../../src/preload');

expect(logSpy).toHaveBeenCalledWith('Sentry Logger [log]:', '[Sentry] Preloaded Http.sentry instrumentation');
expect(logSpy).toHaveBeenCalledWith('Sentry Logger [log]:', '[Sentry] Preloaded Http instrumentation');
expect(logSpy).toHaveBeenCalledWith('Sentry Logger [log]:', '[Sentry] Preloaded Express instrumentation');
expect(logSpy).not.toHaveBeenCalledWith('Sentry Logger [log]:', '[Sentry] Preloaded Graphql instrumentation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ReactRouterInstrumentation } from '../instrumentation/reactRouter';

const INTEGRATION_NAME = 'ReactRouterServer';

const instrumentReactRouter = generateInstrumentOnce('React-Router-Server', () => {
const instrumentReactRouter = generateInstrumentOnce(INTEGRATION_NAME, () => {
return new ReactRouterInstrumentation();
});

Expand Down
Loading