@@ -17,6 +17,41 @@ import { FastifyOtelInstrumentation } from './fastify-otel/index';
1717import type { FastifyInstance , FastifyReply , FastifyRequest } from './types' ;
1818import { FastifyInstrumentationV3 } from './v3/instrumentation' ;
1919
20+ /**
21+ * Options for the Fastify integration.
22+ *
23+ * `shouldHandleDiagnosticsChannelError` - Callback method deciding whether error should be captured and sent to Sentry
24+ * This is used on Fastify v5 where Sentry handles errors in the diagnostics channel.
25+ * Fastify v3 and v4 use `setupFastifyErrorHandler` instead.
26+ *
27+ * @example
28+ *
29+ * ```javascript
30+ * Sentry.init({
31+ * integrations: [
32+ * Sentry.fastifyIntegration({
33+ * shouldHandleDiagnosticsChannelError(_error, _request, reply) {
34+ * return reply.statusCode >= 500;
35+ * },
36+ * });
37+ * },
38+ * });
39+ * ```
40+ *
41+ */
42+ interface FastifyIntegrationOptions {
43+ /**
44+ * Callback method deciding whether error should be captured and sent to Sentry
45+ * This is used on Fastify v5 where Sentry handles errors in the diagnostics channel.
46+ * Fastify v3 and v4 use `setupFastifyErrorHandler` instead.
47+ *
48+ * @param error Captured Fastify error
49+ * @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath)
50+ * @param reply Fastify reply (or any object containing at least statusCode)
51+ */
52+ shouldHandleDiagnosticsChannelError : ( error : Error , request : FastifyRequest , reply : FastifyReply ) => boolean ;
53+ }
54+
2055interface FastifyHandlerOptions {
2156 /**
2257 * Callback method deciding whether error should be captured and sent to Sentry
@@ -36,19 +71,6 @@ interface FastifyHandlerOptions {
3671 * });
3772 * ```
3873 *
39- * or if you use Fastify v5 you can set options in the Sentry.init call:
40- *
41- * ```javascript
42- * Sentry.init({
43- * integrations: [
44- * Sentry.fastifyIntegration({
45- * shouldHandleError(_error, _request, reply) {
46- * return reply.statusCode >= 500;
47- * },
48- * });
49- * },
50- * });
51- * ```
5274 *
5375 * If using TypeScript, you can cast the request and reply to get full type safety.
5476 *
@@ -105,7 +127,7 @@ function handleFastifyError(
105127
106128export const instrumentFastify = generateInstrumentOnce (
107129 INTEGRATION_NAME ,
108- ( options : Partial < FastifyHandlerOptions > = { } ) => {
130+ ( options : Partial < FastifyIntegrationOptions > = { } ) => {
109131 const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( ) ;
110132 const plugin = fastifyOtelInstrumentationInstance . plugin ( ) ;
111133
@@ -140,23 +162,23 @@ export const instrumentFastify = generateInstrumentOnce(
140162 error ,
141163 request ,
142164 reply ,
143- options ?. shouldHandleError || defaultShouldHandleError ,
165+ options ?. shouldHandleDiagnosticsChannelError || defaultShouldHandleError ,
144166 'diagnostics-channel' ,
145167 ) ;
146168 } ) ;
147169
148170 // Returning this as unknown not to deal with the internal types of the FastifyOtelInstrumentation
149- return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyHandlerOptions > ;
171+ return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyIntegrationOptions > ;
150172 } ,
151173) ;
152174
153- const _fastifyIntegration = ( ( { shouldHandleError } ) => {
175+ const _fastifyIntegration = ( ( { shouldHandleDiagnosticsChannelError } : Partial < FastifyIntegrationOptions > ) => {
154176 return {
155177 name : INTEGRATION_NAME ,
156178 setupOnce ( ) {
157179 instrumentFastifyV3 ( ) ;
158180 instrumentFastify ( {
159- shouldHandleError ,
181+ shouldHandleDiagnosticsChannelError ,
160182 } ) ;
161183 } ,
162184 } ;
@@ -178,7 +200,7 @@ const _fastifyIntegration = (({ shouldHandleError }) => {
178200 * })
179201 * ```
180202 */
181- export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyHandlerOptions > = { } ) =>
203+ export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyIntegrationOptions > = { } ) =>
182204 _fastifyIntegration ( options ) ,
183205) ;
184206
0 commit comments