|
| 1 | +import { captureException, getCurrentHub, runWithAsyncContext, startSpan, Transaction } from '@sentry/core'; |
| 2 | +import type { Integration } from '@sentry/types'; |
| 3 | +import { addExceptionMechanism, getSanitizedUrlString, parseUrl, tracingContextFromHeaders } from '@sentry/utils'; |
| 4 | + |
| 5 | +function sendErrorToSentry(e: unknown): unknown { |
| 6 | + captureException(e, scope => { |
| 7 | + scope.addEventProcessor(event => { |
| 8 | + addExceptionMechanism(event, { |
| 9 | + type: 'bun', |
| 10 | + handled: false, |
| 11 | + data: { |
| 12 | + function: 'serve', |
| 13 | + }, |
| 14 | + }); |
| 15 | + return event; |
| 16 | + }); |
| 17 | + |
| 18 | + return scope; |
| 19 | + }); |
| 20 | + |
| 21 | + return e; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Instruments `Bun.serve` to automatically create transactions and capture errors. |
| 26 | + */ |
| 27 | +export class BunServer implements Integration { |
| 28 | + /** |
| 29 | + * @inheritDoc |
| 30 | + */ |
| 31 | + public static id: string = 'BunServer'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritDoc |
| 35 | + */ |
| 36 | + public name: string = BunServer.id; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritDoc |
| 40 | + */ |
| 41 | + public setupOnce(): void { |
| 42 | + instrumentBunServe(); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Instruments Bun.serve by patching it's options. |
| 48 | + */ |
| 49 | +export function instrumentBunServe(): void { |
| 50 | + Bun.serve = new Proxy(Bun.serve, { |
| 51 | + apply(serveTarget, serveThisArg, serveArgs: Parameters<typeof Bun.serve>) { |
| 52 | + instrumentBunServeOptions(serveArgs[0]); |
| 53 | + return serveTarget.apply(serveThisArg, serveArgs); |
| 54 | + }, |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Instruments Bun.serve `fetch` option to automatically create spans and capture errors. |
| 60 | + */ |
| 61 | +function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]): void { |
| 62 | + serveOptions.fetch = new Proxy(serveOptions.fetch, { |
| 63 | + apply(fetchTarget, fetchThisArg, fetchArgs: Parameters<typeof serveOptions.fetch>) { |
| 64 | + return runWithAsyncContext(() => { |
| 65 | + const hub = getCurrentHub(); |
| 66 | + |
| 67 | + const request = fetchArgs[0]; |
| 68 | + const upperCaseMethod = request.method.toUpperCase(); |
| 69 | + if (upperCaseMethod === 'OPTIONS' || upperCaseMethod === 'HEAD') { |
| 70 | + return fetchTarget.apply(fetchThisArg, fetchArgs); |
| 71 | + } |
| 72 | + |
| 73 | + const sentryTrace = request.headers.get('sentry-trace') || ''; |
| 74 | + const baggage = request.headers.get('baggage'); |
| 75 | + const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders( |
| 76 | + sentryTrace, |
| 77 | + baggage, |
| 78 | + ); |
| 79 | + hub.getScope().setPropagationContext(propagationContext); |
| 80 | + |
| 81 | + const parsedUrl = parseUrl(request.url); |
| 82 | + const data: Record<string, unknown> = { |
| 83 | + 'http.request.method': request.method || 'GET', |
| 84 | + }; |
| 85 | + if (parsedUrl.search) { |
| 86 | + data['http.query'] = parsedUrl.search; |
| 87 | + } |
| 88 | + |
| 89 | + const url = getSanitizedUrlString(parsedUrl); |
| 90 | + return startSpan( |
| 91 | + { |
| 92 | + op: 'http.server', |
| 93 | + name: `${request.method} ${parsedUrl.path || '/'}`, |
| 94 | + origin: 'auto.http.bun.serve', |
| 95 | + ...traceparentData, |
| 96 | + data, |
| 97 | + metadata: { |
| 98 | + source: 'url', |
| 99 | + dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, |
| 100 | + request: { |
| 101 | + url, |
| 102 | + method: request.method, |
| 103 | + headers: request.headers.toJSON(), |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + async span => { |
| 108 | + try { |
| 109 | + const response = await (fetchTarget.apply(fetchThisArg, fetchArgs) as ReturnType< |
| 110 | + typeof serveOptions.fetch |
| 111 | + >); |
| 112 | + if (response && response.status) { |
| 113 | + span?.setHttpStatus(response.status); |
| 114 | + span?.setData('http.response.status_code', response.status); |
| 115 | + if (span instanceof Transaction) { |
| 116 | + span.setContext('response', { |
| 117 | + headers: response.headers.toJSON(), |
| 118 | + status_code: response.status, |
| 119 | + }); |
| 120 | + } |
| 121 | + } |
| 122 | + return response; |
| 123 | + } catch (e) { |
| 124 | + sendErrorToSentry(e); |
| 125 | + throw e; |
| 126 | + } |
| 127 | + }, |
| 128 | + ); |
| 129 | + }); |
| 130 | + }, |
| 131 | + }); |
| 132 | +} |
0 commit comments