import { defineIntegration, LRUMap, addFetchInstrumentationHandler, getClient, isSentryRequestUrl, instrumentFetchRequest, stringMatchesSomePattern, addBreadcrumb, getBreadcrumbLogLevelFromHttpStatusCode } from '@sentry/core'; const INTEGRATION_NAME = "Fetch"; const HAS_CLIENT_MAP = /* @__PURE__ */ new WeakMap(); const _fetchIntegration = ((options = {}) => { const breadcrumbs = options.breadcrumbs === void 0 ? true : options.breadcrumbs; const shouldCreateSpanForRequest = options.shouldCreateSpanForRequest; const _createSpanUrlMap = new LRUMap(100); const _headersUrlMap = new LRUMap(100); const spans = {}; function _shouldAttachTraceData(url) { const client = getClient(); if (!client) { return false; } const clientOptions = client.getOptions(); if (clientOptions.tracePropagationTargets === void 0) { return true; } const cachedDecision = _headersUrlMap.get(url); if (cachedDecision !== void 0) { return cachedDecision; } const decision = stringMatchesSomePattern(url, clientOptions.tracePropagationTargets); _headersUrlMap.set(url, decision); return decision; } function _shouldCreateSpan(url) { if (shouldCreateSpanForRequest === void 0) { return true; } const cachedDecision = _createSpanUrlMap.get(url); if (cachedDecision !== void 0) { return cachedDecision; } const decision = shouldCreateSpanForRequest(url); _createSpanUrlMap.set(url, decision); return decision; } return { name: INTEGRATION_NAME, setupOnce() { addFetchInstrumentationHandler((handlerData) => { const client = getClient(); const { propagateTraceparent } = client?.getOptions() || {}; if (!client || !HAS_CLIENT_MAP.get(client)) { return; } if (isSentryRequestUrl(handlerData.fetchData.url, client)) { return; } instrumentFetchRequest(handlerData, _shouldCreateSpan, _shouldAttachTraceData, spans, { spanOrigin: "auto.http.fetch", propagateTraceparent }); if (breadcrumbs) { createBreadcrumb(handlerData); } }, true); }, setup(client) { HAS_CLIENT_MAP.set(client, true); } }; }); const fetchIntegration = defineIntegration(_fetchIntegration); function createBreadcrumb(handlerData) { const { startTimestamp, endTimestamp } = handlerData; if (!endTimestamp) { return; } const breadcrumbData = { method: handlerData.fetchData.method, url: handlerData.fetchData.url }; if (handlerData.error) { const hint = { data: handlerData.error, input: handlerData.args, startTimestamp, endTimestamp }; addBreadcrumb( { category: "fetch", data: breadcrumbData, level: "error", type: "http" }, hint ); } else { const response = handlerData.response; breadcrumbData.request_body_size = handlerData.fetchData.request_body_size; breadcrumbData.response_body_size = handlerData.fetchData.response_body_size; breadcrumbData.status_code = response?.status; const hint = { input: handlerData.args, response, startTimestamp, endTimestamp }; const level = getBreadcrumbLogLevelFromHttpStatusCode(breadcrumbData.status_code); addBreadcrumb( { category: "fetch", data: breadcrumbData, type: "http", level }, hint ); } } export { fetchIntegration }; //# sourceMappingURL=fetch.js.map