|
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 | | - shouldPropagateTraceForUrl, |
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 | | - return shouldPropagateTraceForUrl(url, client.getOptions().tracePropagationTargets, _headersUrlMap); |
57 | | - } |
58 | | - |
59 | | - /** Helper that wraps shouldCreateSpanForRequest option */ |
60 | | - function _shouldCreateSpan(url: string): boolean { |
61 | | - if (shouldCreateSpanForRequest === undefined) { |
62 | | - return true; |
63 | | - } |
64 | | - |
65 | | - const cachedDecision = _createSpanUrlMap.get(url); |
66 | | - if (cachedDecision !== undefined) { |
67 | | - return cachedDecision; |
68 | | - } |
69 | | - |
70 | | - const decision = shouldCreateSpanForRequest(url); |
71 | | - _createSpanUrlMap.set(url, decision); |
72 | | - return decision; |
73 | | - } |
74 | | - |
75 | | - return { |
76 | | - name: INTEGRATION_NAME, |
77 | | - setupOnce() { |
78 | | - addFetchInstrumentationHandler(handlerData => { |
79 | | - const client = getClient(); |
80 | | - if (!client || !HAS_CLIENT_MAP.get(client)) { |
81 | | - return; |
82 | | - } |
83 | | - const { propagateTraceparent } = client.getOptions(); |
84 | | - |
85 | | - if (isSentryRequestUrl(handlerData.fetchData.url, client)) { |
86 | | - return; |
87 | | - } |
88 | | - |
89 | | - instrumentFetchRequest(handlerData, _shouldCreateSpan, _shouldAttachTraceData, spans, { |
90 | | - spanOrigin: 'auto.http.fetch', |
91 | | - propagateTraceparent, |
92 | | - }); |
93 | | - |
94 | | - if (breadcrumbs) { |
95 | | - createBreadcrumb(handlerData); |
96 | | - } |
97 | | - }); |
98 | | - }, |
99 | | - setup(client) { |
100 | | - HAS_CLIENT_MAP.set(client, true); |
101 | | - }, |
102 | | - }; |
103 | | -}) satisfies IntegrationFn; |
| 1 | +import { createFetchIntegration } from '@sentry/core'; |
104 | 2 |
|
105 | 3 | /** |
106 | 4 | * Instruments outgoing `fetch` requests in Bun: creates spans, records breadcrumbs and |
107 | 5 | * attaches trace propagation headers. |
108 | 6 | */ |
109 | | -export const fetchIntegration = defineIntegration(_fetchIntegration); |
110 | | - |
111 | | -function createBreadcrumb(handlerData: HandlerDataFetch): void { |
112 | | - const { startTimestamp, endTimestamp } = handlerData; |
113 | | - |
114 | | - // We only capture complete fetch requests |
115 | | - if (!endTimestamp) { |
116 | | - return; |
117 | | - } |
118 | | - |
119 | | - const breadcrumbData: FetchBreadcrumbData = { |
120 | | - method: handlerData.fetchData.method, |
121 | | - url: handlerData.fetchData.url, |
122 | | - }; |
123 | | - |
124 | | - if (handlerData.error) { |
125 | | - const hint: FetchBreadcrumbHint = { |
126 | | - data: handlerData.error, |
127 | | - input: handlerData.args, |
128 | | - startTimestamp, |
129 | | - endTimestamp, |
130 | | - }; |
131 | | - |
132 | | - addBreadcrumb( |
133 | | - { |
134 | | - category: 'fetch', |
135 | | - data: breadcrumbData, |
136 | | - level: 'error', |
137 | | - type: 'http', |
138 | | - }, |
139 | | - hint, |
140 | | - ); |
141 | | - } else { |
142 | | - const response = handlerData.response as Response | undefined; |
143 | | - |
144 | | - breadcrumbData.request_body_size = handlerData.fetchData.request_body_size; |
145 | | - breadcrumbData.response_body_size = handlerData.fetchData.response_body_size; |
146 | | - breadcrumbData.status_code = response?.status; |
147 | | - |
148 | | - const hint: FetchBreadcrumbHint = { |
149 | | - input: handlerData.args, |
150 | | - response, |
151 | | - startTimestamp, |
152 | | - endTimestamp, |
153 | | - }; |
154 | | - const level = getBreadcrumbLogLevelFromHttpStatusCode(breadcrumbData.status_code); |
155 | | - |
156 | | - addBreadcrumb( |
157 | | - { |
158 | | - category: 'fetch', |
159 | | - data: breadcrumbData, |
160 | | - type: 'http', |
161 | | - level, |
162 | | - }, |
163 | | - hint, |
164 | | - ); |
165 | | - } |
166 | | -} |
| 7 | +export const fetchIntegration = createFetchIntegration({ |
| 8 | + name: 'Fetch', |
| 9 | + spanOrigin: 'auto.http.fetch', |
| 10 | +}); |
0 commit comments