Skip to content

Commit 9aaa714

Browse files
authored
Add Sentry txn observability for webhooks (#877)
* Add txn logging * Delete unused index.ts files * Fix tests * Add http status to txn
1 parent 809b9f3 commit 9aaa714

File tree

7 files changed

+22
-15
lines changed

7 files changed

+22
-15
lines changed

src/webhooks/bootstrap-dev-env/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/webhooks/gocd/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/webhooks/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('cron jobs testing', function () {
8888
}
8989
);
9090
const reply = new MockReply() as FastifyReply;
91-
await handleRoute(mockError, {} as FastifyRequest, reply);
91+
await handleRoute(mockError, {} as FastifyRequest, reply, '');
9292
expect(mockError).toHaveBeenCalled();
9393
expect(reply.statusCode).toBe(400);
9494
});

src/webhooks/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '@sentry/tracing';
2+
13
import * as Sentry from '@sentry/node';
24
import { FastifyReply, FastifyRequest } from 'fastify';
35

@@ -9,38 +11,48 @@ import { kafkactlWebhook } from './kafka-control-plane/kafka-control-plane';
911
import { sentryOptionsWebhook } from './sentry-options/sentry-options';
1012
import { webpackWebhook } from './webpack/webpack';
1113

14+
type WebhookHandler = (
15+
request: FastifyRequest<any>,
16+
reply: FastifyReply
17+
) => Promise<void>;
18+
1219
// Error handling wrapper function
1320
export 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
2941
export 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

src/webhooks/kafka-control-plane/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/webhooks/sentry-options/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/webhooks/webpack/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)