@@ -27,6 +27,7 @@ interface FastifyHandlerOptions {
27
27
*
28
28
* @example
29
29
*
30
+ *
30
31
* ```javascript
31
32
* setupFastifyErrorHandler(app, {
32
33
* shouldHandleError(_error, _request, reply) {
@@ -35,6 +36,20 @@ interface FastifyHandlerOptions {
35
36
* });
36
37
* ```
37
38
*
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
+ *
38
53
* If using TypeScript, you can cast the request and reply to get full type safety.
39
54
*
40
55
* ```typescript
@@ -88,51 +103,65 @@ function handleFastifyError(
88
103
}
89
104
}
90
105
91
- export const instrumentFastify = generateInstrumentOnce ( INTEGRATION_NAME , ( ) => {
92
- const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( ) ;
93
- const plugin = fastifyOtelInstrumentationInstance . plugin ( ) ;
94
- const options = fastifyOtelInstrumentationInstance . getConfig ( ) ;
95
- const shouldHandleError = ( options as FastifyHandlerOptions ) ?. shouldHandleError || defaultShouldHandleError ;
96
-
97
- // This message handler works for Fastify versions 3, 4 and 5
98
- diagnosticsChannel . subscribe ( 'fastify.initialization' , message => {
99
- const fastifyInstance = ( message as { fastify ?: FastifyInstance } ) . fastify ;
100
-
101
- fastifyInstance ?. register ( plugin ) . after ( err => {
102
- if ( err ) {
103
- DEBUG_BUILD && debug . error ( 'Failed to setup Fastify instrumentation' , err ) ;
104
- } else {
105
- instrumentClient ( ) ;
106
-
107
- if ( fastifyInstance ) {
108
- instrumentOnRequest ( fastifyInstance ) ;
106
+ export const instrumentFastify = generateInstrumentOnce (
107
+ INTEGRATION_NAME ,
108
+ (
109
+ options : Partial < FastifyHandlerOptions > = {
110
+ shouldHandleError : defaultShouldHandleError ,
111
+ } ,
112
+ ) => {
113
+ const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( ) ;
114
+ const plugin = fastifyOtelInstrumentationInstance . plugin ( ) ;
115
+
116
+ // This message handler works for Fastify versions 3, 4 and 5
117
+ diagnosticsChannel . subscribe ( 'fastify.initialization' , message => {
118
+ const fastifyInstance = ( message as { fastify ?: FastifyInstance } ) . fastify ;
119
+
120
+ fastifyInstance ?. register ( plugin ) . after ( err => {
121
+ if ( err ) {
122
+ DEBUG_BUILD && debug . error ( 'Failed to setup Fastify instrumentation' , err ) ;
123
+ } else {
124
+ instrumentClient ( ) ;
125
+
126
+ if ( fastifyInstance ) {
127
+ instrumentOnRequest ( fastifyInstance ) ;
128
+ }
109
129
}
110
- }
130
+ } ) ;
111
131
} ) ;
112
- } ) ;
113
-
114
- // This diagnostics channel only works on Fastify version 5
115
- // For versions 3 and 4, we use `setupFastifyErrorHandler` instead
116
- diagnosticsChannel . subscribe ( 'tracing:fastify.request.handler:error' , message => {
117
- const { error, request, reply } = message as {
118
- error : Error ;
119
- request : FastifyRequest & { opentelemetry ?: ( ) => { span ?: Span } } ;
120
- reply : FastifyReply ;
121
- } ;
122
132
123
- handleFastifyError . call ( handleFastifyError , error , request , reply , shouldHandleError , 'diagnostics-channel' ) ;
124
- } ) ;
133
+ // This diagnostics channel only works on Fastify version 5
134
+ // For versions 3 and 4, we use `setupFastifyErrorHandler` instead
135
+ diagnosticsChannel . subscribe ( 'tracing:fastify.request.handler:error' , message => {
136
+ const { error, request, reply } = message as {
137
+ error : Error ;
138
+ request : FastifyRequest & { opentelemetry ?: ( ) => { span ?: Span } } ;
139
+ reply : FastifyReply ;
140
+ } ;
141
+
142
+ handleFastifyError . call (
143
+ handleFastifyError ,
144
+ error ,
145
+ request ,
146
+ reply ,
147
+ options . shouldHandleError ,
148
+ 'diagnostics-channel' ,
149
+ ) ;
150
+ } ) ;
125
151
126
- // Returning this as unknown not to deal with the internal types of the FastifyOtelInstrumentation
127
- return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyHandlerOptions > ;
128
- } ) ;
152
+ // Returning this as unknown not to deal with the internal types of the FastifyOtelInstrumentation
153
+ return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyHandlerOptions > ;
154
+ } ,
155
+ ) ;
129
156
130
- const _fastifyIntegration = ( ( ) => {
157
+ const _fastifyIntegration = ( ( { shouldHandleError } ) => {
131
158
return {
132
159
name : INTEGRATION_NAME ,
133
160
setupOnce ( ) {
134
161
instrumentFastifyV3 ( ) ;
135
- instrumentFastify ( ) ;
162
+ instrumentFastify ( {
163
+ shouldHandleError,
164
+ } ) ;
136
165
} ,
137
166
} ;
138
167
} ) satisfies IntegrationFn ;
@@ -153,7 +182,9 @@ const _fastifyIntegration = (() => {
153
182
* })
154
183
* ```
155
184
*/
156
- export const fastifyIntegration = defineIntegration ( _fastifyIntegration ) ;
185
+ export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyHandlerOptions > = { } ) =>
186
+ _fastifyIntegration ( options ) ,
187
+ ) ;
157
188
158
189
/**
159
190
* Default function to determine if an error should be sent to Sentry
0 commit comments