@@ -17,6 +17,41 @@ 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
+ * 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
+
20
55
interface FastifyHandlerOptions {
21
56
/**
22
57
* Callback method deciding whether error should be captured and sent to Sentry
@@ -36,19 +71,6 @@ interface FastifyHandlerOptions {
36
71
* });
37
72
* ```
38
73
*
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
74
*
53
75
* If using TypeScript, you can cast the request and reply to get full type safety.
54
76
*
@@ -105,7 +127,7 @@ function handleFastifyError(
105
127
106
128
export const instrumentFastify = generateInstrumentOnce (
107
129
INTEGRATION_NAME ,
108
- ( options : Partial < FastifyHandlerOptions > = { } ) => {
130
+ ( options : Partial < FastifyIntegrationOptions > = { } ) => {
109
131
const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( ) ;
110
132
const plugin = fastifyOtelInstrumentationInstance . plugin ( ) ;
111
133
@@ -140,23 +162,23 @@ export const instrumentFastify = generateInstrumentOnce(
140
162
error ,
141
163
request ,
142
164
reply ,
143
- options ?. shouldHandleError || defaultShouldHandleError ,
165
+ options ?. shouldHandleDiagnosticsChannelError || defaultShouldHandleError ,
144
166
'diagnostics-channel' ,
145
167
) ;
146
168
} ) ;
147
169
148
170
// 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 > ;
150
172
} ,
151
173
) ;
152
174
153
- const _fastifyIntegration = ( ( { shouldHandleError } ) => {
175
+ const _fastifyIntegration = ( ( { shouldHandleDiagnosticsChannelError } : Partial < FastifyIntegrationOptions > ) => {
154
176
return {
155
177
name : INTEGRATION_NAME ,
156
178
setupOnce ( ) {
157
179
instrumentFastifyV3 ( ) ;
158
180
instrumentFastify ( {
159
- shouldHandleError ,
181
+ shouldHandleDiagnosticsChannelError ,
160
182
} ) ;
161
183
} ,
162
184
} ;
@@ -178,7 +200,7 @@ const _fastifyIntegration = (({ shouldHandleError }) => {
178
200
* })
179
201
* ```
180
202
*/
181
- export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyHandlerOptions > = { } ) =>
203
+ export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyIntegrationOptions > = { } ) =>
182
204
_fastifyIntegration ( options ) ,
183
205
) ;
184
206
0 commit comments