|
| 1 | +import { errorMonitor } from 'node:events'; |
1 | 2 | import http from 'node:http'; |
2 | 3 | import https from 'node:https'; |
3 | | -import type { IntegrationFn } from '@sentry/core'; |
| 4 | +import type { HttpIncomingMessage, HttpServerResponse, IntegrationFn, Span } from '@sentry/core'; |
4 | 5 | import { defineIntegration, getHttpServerSubscriptions, HTTP_ON_SERVER_REQUEST } from '@sentry/core'; |
5 | 6 |
|
6 | 7 | const INTEGRATION_NAME = 'BunHttpServer' as const; |
@@ -43,6 +44,31 @@ interface BunHttpServerOptions { |
43 | 44 | * @default 'medium' |
44 | 45 | */ |
45 | 46 | maxRequestBodySize?: 'none' | 'small' | 'medium' | 'always'; |
| 47 | + |
| 48 | + /** |
| 49 | + * Do not capture spans for incoming HTTP requests to URLs where the given callback returns `true`. |
| 50 | + * |
| 51 | + * The `urlPath` param consists of the URL path and query string (if any) of the incoming request. |
| 52 | + */ |
| 53 | + ignoreIncomingRequests?: (urlPath: string, request: HttpIncomingMessage) => boolean; |
| 54 | + |
| 55 | + /** |
| 56 | + * Whether to automatically ignore common static asset requests like favicon.ico, robots.txt, etc. |
| 57 | + * |
| 58 | + * @default true |
| 59 | + */ |
| 60 | + ignoreStaticAssets?: boolean; |
| 61 | + |
| 62 | + /** |
| 63 | + * A hook that can be used to mutate the span for incoming requests. |
| 64 | + * This is triggered after the span is created, but before it is recorded. |
| 65 | + */ |
| 66 | + onSpanCreated?: (span: Span, request: HttpIncomingMessage, response: HttpServerResponse) => void; |
| 67 | + |
| 68 | + /** |
| 69 | + * A hook that can be used to mutate the span one last time when the response is finished. |
| 70 | + */ |
| 71 | + onSpanEnd?: (span: Span, request: HttpIncomingMessage, response: HttpServerResponse) => void; |
46 | 72 | } |
47 | 73 |
|
48 | 74 | let hasPatched = false; |
@@ -95,7 +121,12 @@ export function instrumentBunHttpServer(options: BunHttpServerOptions = {}): voi |
95 | 121 | return; |
96 | 122 | } |
97 | 123 |
|
98 | | - const { [HTTP_ON_SERVER_REQUEST]: onServerRequest } = getHttpServerSubscriptions(options); |
| 124 | + const { [HTTP_ON_SERVER_REQUEST]: onServerRequest } = getHttpServerSubscriptions({ |
| 125 | + ...options, |
| 126 | + // Pass the real `errorMonitor` symbol so core observes `'error'` events without consuming |
| 127 | + // them — otherwise it would swallow errors before they reach user-supplied `'error'` handlers. |
| 128 | + errorMonitor, |
| 129 | + }); |
99 | 130 |
|
100 | 131 | // Track which servers we have already handed to core, so we instrument each server exactly once. |
101 | 132 | // After core instruments a server it installs its own `emit` on the instance, which shadows this |
|
0 commit comments