|
| 1 | +import { addBreadcrumb, defineIntegration, getClient, instrumentFetchRequest, isSentryRequestUrl } from '@sentry/core'; |
| 2 | +import type { |
| 3 | + Client, |
| 4 | + FetchBreadcrumbData, |
| 5 | + FetchBreadcrumbHint, |
| 6 | + HandlerDataFetch, |
| 7 | + IntegrationFn, |
| 8 | + Span, |
| 9 | +} from '@sentry/types'; |
| 10 | +import { LRUMap, addFetchInstrumentationHandler, stringMatchesSomePattern } from '@sentry/utils'; |
| 11 | + |
| 12 | +const INTEGRATION_NAME = 'Fetch'; |
| 13 | + |
| 14 | +const HAS_CLIENT_MAP = new WeakMap<Client, boolean>(); |
| 15 | + |
| 16 | +export interface Options { |
| 17 | + /** |
| 18 | + * Whether breadcrumbs should be recorded for requests |
| 19 | + * Defaults to true |
| 20 | + */ |
| 21 | + breadcrumbs: boolean; |
| 22 | + |
| 23 | + /** |
| 24 | + * Function determining whether or not to create spans to track outgoing requests to the given URL. |
| 25 | + * By default, spans will be created for all outgoing requests. |
| 26 | + */ |
| 27 | + shouldCreateSpanForRequest?: (url: string) => boolean; |
| 28 | +} |
| 29 | + |
| 30 | +const _fetchIntegration = ((options: Partial<Options> = {}) => { |
| 31 | + const breadcrumbs = options.breadcrumbs === undefined ? true : options.breadcrumbs; |
| 32 | + const shouldCreateSpanForRequest = options.shouldCreateSpanForRequest; |
| 33 | + |
| 34 | + const _createSpanUrlMap = new LRUMap<string, boolean>(100); |
| 35 | + const _headersUrlMap = new LRUMap<string, boolean>(100); |
| 36 | + |
| 37 | + const spans: Record<string, Span> = {}; |
| 38 | + |
| 39 | + /** Decides whether to attach trace data to the outgoing fetch request */ |
| 40 | + function _shouldAttachTraceData(url: string): boolean { |
| 41 | + const client = getClient(); |
| 42 | + |
| 43 | + if (!client) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + const clientOptions = client.getOptions(); |
| 48 | + |
| 49 | + if (clientOptions.tracePropagationTargets === undefined) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + const cachedDecision = _headersUrlMap.get(url); |
| 54 | + if (cachedDecision !== undefined) { |
| 55 | + return cachedDecision; |
| 56 | + } |
| 57 | + |
| 58 | + const decision = stringMatchesSomePattern(url, clientOptions.tracePropagationTargets); |
| 59 | + _headersUrlMap.set(url, decision); |
| 60 | + return decision; |
| 61 | + } |
| 62 | + |
| 63 | + /** Helper that wraps shouldCreateSpanForRequest option */ |
| 64 | + function _shouldCreateSpan(url: string): boolean { |
| 65 | + if (shouldCreateSpanForRequest === undefined) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + const cachedDecision = _createSpanUrlMap.get(url); |
| 70 | + if (cachedDecision !== undefined) { |
| 71 | + return cachedDecision; |
| 72 | + } |
| 73 | + |
| 74 | + const decision = shouldCreateSpanForRequest(url); |
| 75 | + _createSpanUrlMap.set(url, decision); |
| 76 | + return decision; |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + name: INTEGRATION_NAME, |
| 81 | + setupOnce() { |
| 82 | + addFetchInstrumentationHandler(handlerData => { |
| 83 | + const client = getClient(); |
| 84 | + if (!client || !HAS_CLIENT_MAP.get(client)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (isSentryRequestUrl(handlerData.fetchData.url, client)) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + instrumentFetchRequest( |
| 93 | + handlerData, |
| 94 | + _shouldCreateSpan, |
| 95 | + _shouldAttachTraceData, |
| 96 | + spans, |
| 97 | + 'auto.http.wintercg_fetch', |
| 98 | + ); |
| 99 | + |
| 100 | + if (breadcrumbs) { |
| 101 | + createBreadcrumb(handlerData); |
| 102 | + } |
| 103 | + }); |
| 104 | + }, |
| 105 | + setup(client) { |
| 106 | + HAS_CLIENT_MAP.set(client, true); |
| 107 | + }, |
| 108 | + }; |
| 109 | +}) satisfies IntegrationFn; |
| 110 | + |
| 111 | +/** |
| 112 | + * Creates spans and attaches tracing headers to fetch requests. |
| 113 | + */ |
| 114 | +export const fetchIntegration = defineIntegration(_fetchIntegration); |
| 115 | + |
| 116 | +function createBreadcrumb(handlerData: HandlerDataFetch): void { |
| 117 | + const { startTimestamp, endTimestamp } = handlerData; |
| 118 | + |
| 119 | + // We only capture complete fetch requests |
| 120 | + if (!endTimestamp) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if (handlerData.error) { |
| 125 | + const data = handlerData.fetchData; |
| 126 | + const hint: FetchBreadcrumbHint = { |
| 127 | + data: handlerData.error, |
| 128 | + input: handlerData.args, |
| 129 | + startTimestamp, |
| 130 | + endTimestamp, |
| 131 | + }; |
| 132 | + |
| 133 | + addBreadcrumb( |
| 134 | + { |
| 135 | + category: 'fetch', |
| 136 | + data, |
| 137 | + level: 'error', |
| 138 | + type: 'http', |
| 139 | + }, |
| 140 | + hint, |
| 141 | + ); |
| 142 | + } else { |
| 143 | + const data: FetchBreadcrumbData = { |
| 144 | + ...handlerData.fetchData, |
| 145 | + status_code: handlerData.response && handlerData.response.status, |
| 146 | + }; |
| 147 | + const hint: FetchBreadcrumbHint = { |
| 148 | + input: handlerData.args, |
| 149 | + response: handlerData.response, |
| 150 | + startTimestamp, |
| 151 | + endTimestamp, |
| 152 | + }; |
| 153 | + addBreadcrumb( |
| 154 | + { |
| 155 | + category: 'fetch', |
| 156 | + data, |
| 157 | + type: 'http', |
| 158 | + }, |
| 159 | + hint, |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments