@@ -60,6 +60,8 @@ export const instrumentFastifyV3 = generateInstrumentOnce(INTEGRATION_NAME_V3, (
60
60
export const instrumentFastify = generateInstrumentOnce ( INTEGRATION_NAME , ( ) => {
61
61
const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( ) ;
62
62
const plugin = fastifyOtelInstrumentationInstance . plugin ( ) ;
63
+ const options = fastifyOtelInstrumentationInstance . getConfig ( ) ;
64
+ const shouldHandleError = ( options as FastifyHandlerOptions ) ?. shouldHandleError || defaultShouldHandleError ;
63
65
64
66
// This message handler works for Fastify versions 3, 4 and 5
65
67
diagnosticsChannel . subscribe ( 'fastify.initialization' , message => {
@@ -78,8 +80,22 @@ export const instrumentFastify = generateInstrumentOnce(INTEGRATION_NAME, () =>
78
80
} ) ;
79
81
} ) ;
80
82
83
+ // This diagnostics channel only works on Fastify version 5
84
+ // For versions 3 and 4, we use `setupFastifyErrorHandler` instead
85
+ diagnosticsChannel . subscribe ( 'tracing:fastify.request.handler:error' , message => {
86
+ const { error, request, reply } = message as {
87
+ error : Error ;
88
+ request : FastifyRequest & { opentelemetry ?: ( ) => { span ?: Span } } ;
89
+ reply : FastifyReply ;
90
+ } ;
91
+
92
+ if ( shouldHandleError ( error , request , reply ) ) {
93
+ captureException ( error ) ;
94
+ }
95
+ } ) ;
96
+
81
97
// Returning this as unknown not to deal with the internal types of the FastifyOtelInstrumentation
82
- return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig > ;
98
+ return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyHandlerOptions > ;
83
99
} ) ;
84
100
85
101
const _fastifyIntegration = ( ( ) => {
@@ -143,15 +159,21 @@ function defaultShouldHandleError(_error: Error, _request: FastifyRequest, reply
143
159
*/
144
160
export function setupFastifyErrorHandler ( fastify : FastifyInstance , options ?: Partial < FastifyHandlerOptions > ) : void {
145
161
const shouldHandleError = options ?. shouldHandleError || defaultShouldHandleError ;
146
-
147
162
const plugin = Object . assign (
148
163
function ( fastify : FastifyInstance , _options : unknown , done : ( ) => void ) : void {
149
- fastify . addHook ( 'onError' , async ( request , reply , error ) => {
150
- if ( shouldHandleError ( error , request , reply ) ) {
151
- captureException ( error ) ;
152
- }
153
- } ) ;
154
-
164
+ if ( fastify . version ?. startsWith ( '5.' ) ) {
165
+ // Fastify 5.x uses diagnostics channel for error handling
166
+ DEBUG_BUILD &&
167
+ logger . warn (
168
+ 'Fastify 5.x detected, using diagnostics channel for error handling.\nYou can safely remove `setupFastifyErrorHandler` call.' ,
169
+ ) ;
170
+ } else {
171
+ fastify . addHook ( 'onError' , async ( request , reply , error ) => {
172
+ if ( shouldHandleError ( error , request , reply ) ) {
173
+ captureException ( error ) ;
174
+ }
175
+ } ) ;
176
+ }
155
177
done ( ) ;
156
178
} ,
157
179
{
0 commit comments