@@ -17,6 +17,39 @@ import { FastifyOtelInstrumentation } from './fastify-otel/index';
17
17
import type { FastifyInstance , FastifyReply , FastifyRequest } from './types' ;
18
18
import { FastifyInstrumentationV3 } from './v3/instrumentation' ;
19
19
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
+ *
46
+ * @param error Captured Fastify error
47
+ * @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath)
48
+ * @param reply Fastify reply (or any object containing at least statusCode)
49
+ */
50
+ shouldHandleDiagnosticsChannelError : ( error : Error , request : FastifyRequest , reply : FastifyReply ) => boolean ;
51
+ }
52
+
20
53
interface FastifyHandlerOptions {
21
54
/**
22
55
* Callback method deciding whether error should be captured and sent to Sentry
@@ -36,19 +69,6 @@ interface FastifyHandlerOptions {
36
69
* });
37
70
* ```
38
71
*
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
- * ```
52
72
*
53
73
* If using TypeScript, you can cast the request and reply to get full type safety.
54
74
*
@@ -105,7 +125,7 @@ function handleFastifyError(
105
125
106
126
export const instrumentFastify = generateInstrumentOnce (
107
127
INTEGRATION_NAME ,
108
- ( options : Partial < FastifyHandlerOptions > = { } ) => {
128
+ ( options : Partial < FastifyIntegrationOptions > = { } ) => {
109
129
const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( ) ;
110
130
const plugin = fastifyOtelInstrumentationInstance . plugin ( ) ;
111
131
@@ -140,23 +160,23 @@ export const instrumentFastify = generateInstrumentOnce(
140
160
error ,
141
161
request ,
142
162
reply ,
143
- options ?. shouldHandleError || defaultShouldHandleError ,
163
+ options ?. shouldHandleDiagnosticsChannelError || defaultShouldHandleError ,
144
164
'diagnostics-channel' ,
145
165
) ;
146
166
} ) ;
147
167
148
168
// Returning this as unknown not to deal with the internal types of the FastifyOtelInstrumentation
149
- return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyHandlerOptions > ;
169
+ return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyIntegrationOptions > ;
150
170
} ,
151
171
) ;
152
172
153
- const _fastifyIntegration = ( ( { shouldHandleError } ) => {
173
+ const _fastifyIntegration = ( ( { shouldHandleDiagnosticsChannelError } : Partial < FastifyIntegrationOptions > ) => {
154
174
return {
155
175
name : INTEGRATION_NAME ,
156
176
setupOnce ( ) {
157
177
instrumentFastifyV3 ( ) ;
158
178
instrumentFastify ( {
159
- shouldHandleError ,
179
+ shouldHandleDiagnosticsChannelError ,
160
180
} ) ;
161
181
} ,
162
182
} ;
@@ -178,7 +198,7 @@ const _fastifyIntegration = (({ shouldHandleError }) => {
178
198
* })
179
199
* ```
180
200
*/
181
- export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyHandlerOptions > = { } ) =>
201
+ export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyIntegrationOptions > = { } ) =>
182
202
_fastifyIntegration ( options ) ,
183
203
) ;
184
204
0 commit comments