Skip to content

Commit ede3929

Browse files
committed
Remove version check.
1 parent afac5e6 commit ede3929

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

packages/node/src/integrations/tracing/fastify-v3/internal-types.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ export type HookHandlerDoneFunction = <TError extends Error = FastifyError>(err?
2121

2222
export type FastifyErrorCodes = any;
2323

24-
export type FastifyPlugin = any;
24+
export type FastifyPlugin = (
25+
instance: FastifyInstance,
26+
opts: any,
27+
done: HookHandlerDoneFunction,
28+
) => unknown | Promise<unknown>;
2529

2630
export interface FastifyInstance {
27-
register: (plugin: FastifyPlugin) => void;
31+
version: string;
32+
register: (
33+
plugin: FastifyPlugin,
34+
opts?: {
35+
throw?: boolean;
36+
},
37+
) => FastifyInstance;
38+
after: (listener?: (err: Error) => void) => FastifyInstance;
2839
addHook(
2940
name:
3041
| 'onRequest'

packages/node/src/integrations/tracing/fastify.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import {
66
defineIntegration,
77
getClient,
88
getIsolationScope,
9-
parseSemver,
9+
logger,
1010
spanToJSON,
1111
} from '@sentry/core';
1212
import type { IntegrationFn, Span } from '@sentry/core';
1313
import { generateInstrumentOnce } from '../../otel/instrument';
1414
import { FastifyInstrumentationV3 } from './fastify-v3/instrumentation';
1515
import type { FastifyInstance } from './fastify-v3/internal-types';
16+
import { DEBUG_BUILD } from '../../debug-build';
1617

1718
/**
1819
* Minimal request type containing properties around route information.
@@ -105,22 +106,10 @@ export const instrumentFastifyV3 = generateInstrumentOnce(
105106

106107
let fastifyOtelInstrumentationInstance: FastifyOtelInstrumentation | undefined;
107108

108-
function checkFastifyVersion(): ReturnType<typeof parseSemver> | undefined {
109-
try {
110-
// eslint-disable-next-line @typescript-eslint/no-var-requires
111-
const pkg = require('fastify/package.json') as { version?: string };
112-
return pkg?.version ? parseSemver(pkg.version) : undefined;
113-
} catch {
114-
return undefined;
115-
}
116-
}
117-
118109
export const instrumentFastify = generateInstrumentOnce(INTEGRATION_NAME, () => {
119110
// FastifyOtelInstrumentation does not have a `requestHook`
120111
// so we can't use `addFastifySpanAttributes` here for now
121-
fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation({
122-
registerOnInitialization: true,
123-
});
112+
fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation();
124113
return fastifyOtelInstrumentationInstance;
125114
});
126115

@@ -129,11 +118,7 @@ const _fastifyIntegration = (() => {
129118
name: INTEGRATION_NAME,
130119
setupOnce() {
131120
instrumentFastifyV3();
132-
133-
const fastifyVersion = checkFastifyVersion();
134-
if (fastifyVersion?.major && fastifyVersion.major >= 4) {
135-
instrumentFastify();
136-
}
121+
instrumentFastify();
137122
},
138123
};
139124
}) satisfies IntegrationFn;
@@ -228,6 +213,15 @@ export function setupFastifyErrorHandler(fastify: Fastify, options?: Partial<Fas
228213
});
229214
}
230215

216+
if (fastifyOtelInstrumentationInstance) {
217+
// Setting after callback to prevent this plugin from throwing version mismatch errors
218+
fastify.register(fastifyOtelInstrumentationInstance.plugin()).after(err => {
219+
if (err) {
220+
// eslint-disable-next-line no-console
221+
DEBUG_BUILD && logger.warn('Failed to register Fastify Otel instrumentation plugin', err);
222+
}
223+
});
224+
}
231225
// eslint-disable-next-line @typescript-eslint/no-floating-promises
232226
fastify.register(plugin);
233227
}

0 commit comments

Comments
 (0)