|
| 1 | +import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; |
| 2 | +import { httpIntegration as originalHttpIntegration } from '@sentry/node'; |
| 3 | +import type { IntegrationFn } from '@sentry/types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Next.js handles incoming requests itself, |
| 7 | + * but it does not handle outgoing requests. |
| 8 | + * Today, it is not possible to use the HttpInstrumentation for only outgoing requests - |
| 9 | + * until https://github.com/open-telemetry/opentelemetry-js/pull/4643 is merged & released. |
| 10 | + * So in the meanwhile, we extend the base HttpInstrumentation to not wrap incoming requests. |
| 11 | + */ |
| 12 | +class CustomNextjsHttpIntegration extends HttpInstrumentation { |
| 13 | + // Instead of the default behavior, we just don't do any wrapping for incoming requests |
| 14 | + protected _getPatchIncomingRequestFunction(_component: 'http' | 'https') { |
| 15 | + return ( |
| 16 | + original: (event: string, ...args: unknown[]) => boolean, |
| 17 | + ): ((this: unknown, event: string, ...args: unknown[]) => boolean) => { |
| 18 | + return function incomingRequest(this: unknown, event: string, ...args: unknown[]): boolean { |
| 19 | + return original.apply(this, [event, ...args]); |
| 20 | + }; |
| 21 | + }; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +interface HttpOptions { |
| 26 | + /** |
| 27 | + * Whether breadcrumbs should be recorded for requests. |
| 28 | + * Defaults to true |
| 29 | + */ |
| 30 | + breadcrumbs?: boolean; |
| 31 | + |
| 32 | + /** |
| 33 | + * Do not capture spans or breadcrumbs for outgoing HTTP requests to URLs where the given callback returns `true`. |
| 34 | + * This controls both span & breadcrumb creation - spans will be non recording if tracing is disabled. |
| 35 | + */ |
| 36 | + ignoreOutgoingRequests?: (url: string) => boolean; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * The http integration instruments Node's internal http and https modules. |
| 41 | + * It creates breadcrumbs and spans for outgoing HTTP requests which will be attached to the currently active span. |
| 42 | + */ |
| 43 | +export const httpIntegration = ((options: HttpOptions = {}) => { |
| 44 | + return originalHttpIntegration({ |
| 45 | + ...options, |
| 46 | + _instrumentation: CustomNextjsHttpIntegration, |
| 47 | + }); |
| 48 | +}) satisfies IntegrationFn; |
0 commit comments