@@ -6,14 +6,15 @@ 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' ;
1515
1616import type { FastifyInstance } from './fastify-v3/internal-types' ;
17+ import { DEBUG_BUILD } from '../../debug-build' ;
1718/**
1819 * Minimal request type containing properties around route information.
1920 * Works for Fastify 3, 4 and 5.
@@ -42,22 +43,10 @@ export const instrumentFastifyV3 = generateInstrumentOnce(
4243
4344let fastifyOtelInstrumentationInstance : FastifyOtelInstrumentation | undefined ;
4445
45- function checkFastifyVersion ( ) : ReturnType < typeof parseSemver > | undefined {
46- try {
47- // eslint-disable-next-line @typescript-eslint/no-var-requires
48- const pkg = require ( 'fastify/package.json' ) as { version ?: string } ;
49- return pkg ?. version ? parseSemver ( pkg . version ) : undefined ;
50- } catch {
51- return undefined ;
52- }
53- }
54-
5546export const instrumentFastify = generateInstrumentOnce ( INTEGRATION_NAME , ( ) => {
5647 // FastifyOtelInstrumentation does not have a `requestHook`
5748 // so we can't use `addFastifySpanAttributes` here for now
58- fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( {
59- registerOnInitialization : true ,
60- } ) ;
49+ fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( ) ;
6150 return fastifyOtelInstrumentationInstance ;
6251} ) ;
6352
@@ -66,11 +55,7 @@ const _fastifyIntegration = (() => {
6655 name : INTEGRATION_NAME ,
6756 setupOnce ( ) {
6857 instrumentFastifyV3 ( ) ;
69-
70- const fastifyVersion = checkFastifyVersion ( ) ;
71- if ( fastifyVersion ?. major && fastifyVersion . major >= 4 ) {
72- instrumentFastify ( ) ;
73- }
58+ instrumentFastify ( ) ;
7459 } ,
7560 } ;
7661} ) satisfies IntegrationFn ;
@@ -151,6 +136,15 @@ export function setupFastifyErrorHandler(fastify: FastifyInstance): void {
151136 } ) ;
152137 }
153138
139+ if ( fastifyOtelInstrumentationInstance ) {
140+ // Setting after callback to prevent this plugin from throwing version mismatch errors
141+ fastify . register ( fastifyOtelInstrumentationInstance . plugin ( ) ) . after ( err => {
142+ if ( err ) {
143+ // eslint-disable-next-line no-console
144+ DEBUG_BUILD && logger . warn ( 'Failed to register Fastify Otel instrumentation plugin' , err ) ;
145+ }
146+ } ) ;
147+ }
154148 // eslint-disable-next-line @typescript-eslint/no-floating-promises
155149 fastify . register ( plugin ) ;
156150}
0 commit comments