|
| 1 | +import type { |
| 2 | + Client, |
| 3 | + FetchBreadcrumbData, |
| 4 | + FetchBreadcrumbHint, |
| 5 | + HandlerDataFetch, |
| 6 | + IntegrationFn, |
| 7 | + Span, |
| 8 | +} from '@sentry/core'; |
| 9 | +import { |
| 10 | + addBreadcrumb, |
| 11 | + addFetchInstrumentationHandler, |
| 12 | + defineIntegration, |
| 13 | + getBreadcrumbLogLevelFromHttpStatusCode, |
| 14 | + getClient, |
| 15 | + instrumentFetchRequest, |
| 16 | + isSentryRequestUrl, |
| 17 | + LRUMap, |
| 18 | + stringMatchesSomePattern, |
| 19 | +} from '@sentry/core'; |
| 20 | + |
| 21 | +const INTEGRATION_NAME = 'Fetch' as const; |
| 22 | + |
| 23 | +const HAS_CLIENT_MAP = new WeakMap<Client, boolean>(); |
| 24 | + |
| 25 | +interface FetchOptions { |
| 26 | + /** |
| 27 | + * Whether breadcrumbs should be recorded for requests. |
| 28 | + * Defaults to true. |
| 29 | + */ |
| 30 | + breadcrumbs?: boolean; |
| 31 | + |
| 32 | + /** |
| 33 | + * Function determining whether or not to create spans to track outgoing requests to the given URL. |
| 34 | + * By default, spans will be created for all outgoing requests. |
| 35 | + */ |
| 36 | + shouldCreateSpanForRequest?: (url: string) => boolean; |
| 37 | +} |
| 38 | + |
| 39 | +const _fetchIntegration = ((options: FetchOptions = {}) => { |
| 40 | + const breadcrumbs = options.breadcrumbs === undefined ? true : options.breadcrumbs; |
| 41 | + const shouldCreateSpanForRequest = options.shouldCreateSpanForRequest; |
| 42 | + |
| 43 | + const _createSpanUrlMap = new LRUMap<string, boolean>(100); |
| 44 | + const _headersUrlMap = new LRUMap<string, boolean>(100); |
| 45 | + |
| 46 | + const spans: Record<string, Span> = {}; |
| 47 | + |
| 48 | + /** Decides whether to attach trace data to the outgoing fetch request */ |
| 49 | + function _shouldAttachTraceData(url: string): boolean { |
| 50 | + const client = getClient(); |
| 51 | + |
| 52 | + if (!client) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + const clientOptions = client.getOptions(); |
| 57 | + |
| 58 | + if (clientOptions.tracePropagationTargets === undefined) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + const cachedDecision = _headersUrlMap.get(url); |
| 63 | + if (cachedDecision !== undefined) { |
| 64 | + return cachedDecision; |
| 65 | + } |
| 66 | + |
| 67 | + const decision = stringMatchesSomePattern(url, clientOptions.tracePropagationTargets); |
| 68 | + _headersUrlMap.set(url, decision); |
| 69 | + return decision; |
| 70 | + } |
| 71 | + |
| 72 | + /** Helper that wraps shouldCreateSpanForRequest option */ |
| 73 | + function _shouldCreateSpan(url: string): boolean { |
| 74 | + if (shouldCreateSpanForRequest === undefined) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + const cachedDecision = _createSpanUrlMap.get(url); |
| 79 | + if (cachedDecision !== undefined) { |
| 80 | + return cachedDecision; |
| 81 | + } |
| 82 | + |
| 83 | + const decision = shouldCreateSpanForRequest(url); |
| 84 | + _createSpanUrlMap.set(url, decision); |
| 85 | + return decision; |
| 86 | + } |
| 87 | + |
| 88 | + return { |
| 89 | + name: INTEGRATION_NAME, |
| 90 | + setupOnce() { |
| 91 | + addFetchInstrumentationHandler(handlerData => { |
| 92 | + const client = getClient(); |
| 93 | + const { propagateTraceparent } = client?.getOptions() || {}; |
| 94 | + if (!client || !HAS_CLIENT_MAP.get(client)) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (isSentryRequestUrl(handlerData.fetchData.url, client)) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + instrumentFetchRequest(handlerData, _shouldCreateSpan, _shouldAttachTraceData, spans, { |
| 103 | + spanOrigin: 'auto.http.fetch', |
| 104 | + propagateTraceparent, |
| 105 | + }); |
| 106 | + |
| 107 | + if (breadcrumbs) { |
| 108 | + createBreadcrumb(handlerData); |
| 109 | + } |
| 110 | + // `skipNativeFetchCheck: true` — the native-fetch check is browser-only (it probes for |
| 111 | + // `[native code]` and falls back to an iframe DOM check). On Bun `fetch` may already be |
| 112 | + // wrapped by the host (e.g. Next.js) and there is no DOM, so we always patch the global. |
| 113 | + }, true); |
| 114 | + }, |
| 115 | + setup(client) { |
| 116 | + HAS_CLIENT_MAP.set(client, true); |
| 117 | + }, |
| 118 | + }; |
| 119 | +}) satisfies IntegrationFn; |
| 120 | + |
| 121 | +/** |
| 122 | + * Instruments outgoing `fetch` requests in Bun: creates spans, records breadcrumbs and |
| 123 | + * attaches trace propagation headers. |
| 124 | + * |
| 125 | + * Bun does not route `fetch` through undici's `diagnostics_channel`, so unlike Node the |
| 126 | + * `nativeNodeFetchIntegration` cannot observe these requests. This integration instead patches |
| 127 | + * the global `fetch`, mirroring the Cloudflare SDK. |
| 128 | + */ |
| 129 | +export const fetchIntegration = defineIntegration(_fetchIntegration); |
| 130 | + |
| 131 | +function createBreadcrumb(handlerData: HandlerDataFetch): void { |
| 132 | + const { startTimestamp, endTimestamp } = handlerData; |
| 133 | + |
| 134 | + // We only capture complete fetch requests |
| 135 | + if (!endTimestamp) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + const breadcrumbData: FetchBreadcrumbData = { |
| 140 | + method: handlerData.fetchData.method, |
| 141 | + url: handlerData.fetchData.url, |
| 142 | + }; |
| 143 | + |
| 144 | + if (handlerData.error) { |
| 145 | + const hint: FetchBreadcrumbHint = { |
| 146 | + data: handlerData.error, |
| 147 | + input: handlerData.args, |
| 148 | + startTimestamp, |
| 149 | + endTimestamp, |
| 150 | + }; |
| 151 | + |
| 152 | + addBreadcrumb( |
| 153 | + { |
| 154 | + category: 'fetch', |
| 155 | + data: breadcrumbData, |
| 156 | + level: 'error', |
| 157 | + type: 'http', |
| 158 | + }, |
| 159 | + hint, |
| 160 | + ); |
| 161 | + } else { |
| 162 | + const response = handlerData.response as Response | undefined; |
| 163 | + |
| 164 | + breadcrumbData.request_body_size = handlerData.fetchData.request_body_size; |
| 165 | + breadcrumbData.response_body_size = handlerData.fetchData.response_body_size; |
| 166 | + breadcrumbData.status_code = response?.status; |
| 167 | + |
| 168 | + const hint: FetchBreadcrumbHint = { |
| 169 | + input: handlerData.args, |
| 170 | + response, |
| 171 | + startTimestamp, |
| 172 | + endTimestamp, |
| 173 | + }; |
| 174 | + const level = getBreadcrumbLogLevelFromHttpStatusCode(breadcrumbData.status_code); |
| 175 | + |
| 176 | + addBreadcrumb( |
| 177 | + { |
| 178 | + category: 'fetch', |
| 179 | + data: breadcrumbData, |
| 180 | + type: 'http', |
| 181 | + level, |
| 182 | + }, |
| 183 | + hint, |
| 184 | + ); |
| 185 | + } |
| 186 | +} |
0 commit comments