@@ -6,13 +6,14 @@ import {
66 defineIntegration ,
77 getClient ,
88 getIsolationScope ,
9- parseSemver ,
9+ logger ,
1010 spanToJSON ,
1111} from '@sentry/core' ;
1212import type { IntegrationFn , Span } from '@sentry/core' ;
1313import { generateInstrumentOnce } from '../../otel/instrument' ;
1414import { FastifyInstrumentationV3 } from './fastify-v3/instrumentation' ;
1515import 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
106107let 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-
118109export 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