1+ import '@sentry/tracing' ;
2+
13import * as Sentry from '@sentry/node' ;
24import { FastifyReply , FastifyRequest } from 'fastify' ;
35
@@ -9,38 +11,48 @@ import { kafkactlWebhook } from './kafka-control-plane/kafka-control-plane';
911import { sentryOptionsWebhook } from './sentry-options/sentry-options' ;
1012import { webpackWebhook } from './webpack/webpack' ;
1113
14+ type WebhookHandler = (
15+ request : FastifyRequest < any > ,
16+ reply : FastifyReply
17+ ) => Promise < void > ;
18+
1219// Error handling wrapper function
1320export async function handleRoute (
14- handler ,
21+ handler : WebhookHandler ,
1522 request : FastifyRequest ,
16- reply : FastifyReply
23+ reply : FastifyReply ,
24+ name : string
1725) : Promise < void > {
26+ const tx = Sentry . startTransaction ( {
27+ op : 'webhooks' ,
28+ name : 'webhooks.' + name ,
29+ } ) ;
1830 try {
1931 await handler ( request , reply ) ;
2032 } catch ( err ) {
21- console . error ( err ) ;
2233 Sentry . captureException ( err ) ;
2334 reply . code ( 400 ) . send ( 'Bad Request' ) ;
24- return ;
35+ tx . setHttpStatus ( 400 ) ;
2536 }
37+ tx . finish ( ) ;
2638}
2739
2840// Function that maps routes to their respective handlers
2941export async function routeHandlers ( server : Fastify , _options ) : Promise < void > {
3042 server . post ( '/metrics/bootstrap-dev-env/webhook' , ( request , reply ) =>
31- handleRoute ( bootstrapWebhook , request , reply )
43+ handleRoute ( bootstrapWebhook , request , reply , 'bootstrap-dev-env' )
3244 ) ;
3345 server . post ( '/metrics/gocd/webhook' , ( request , reply ) =>
34- handleRoute ( gocdWebhook , request , reply )
46+ handleRoute ( gocdWebhook , request , reply , 'gocd' )
3547 ) ;
3648 server . post ( '/metrics/kafka-control-plane/webhook' , ( request , reply ) =>
37- handleRoute ( kafkactlWebhook , request , reply )
49+ handleRoute ( kafkactlWebhook , request , reply , 'kafka-control-plane' )
3850 ) ;
3951 server . post ( '/metrics/sentry-options/webhook' , ( request , reply ) =>
40- handleRoute ( sentryOptionsWebhook , request , reply )
52+ handleRoute ( sentryOptionsWebhook , request , reply , 'sentry-options' )
4153 ) ;
4254 server . post ( '/metrics/webpack/webhook' , ( request , reply ) =>
43- handleRoute ( webpackWebhook , request , reply )
55+ handleRoute ( webpackWebhook , request , reply , 'webpack' )
4456 ) ;
4557
4658 // Default handler for invalid routes
0 commit comments