UNPKG

@sentry/browser

Version:
75 lines (74 loc) • 3.45 kB
import { WINDOW } from "../helpers.js"; import { addFetchEndInstrumentationHandler, addFetchInstrumentationHandler, defineIntegration, filterCollectedUrl, getSanitizedUrlStringFromUrlObject, getUrlDomain, hasSpanStreamingEnabled, parseStringToURLObject, stripDataUrlContent } from "@sentry/core"; import { HTTP_REQUEST_METHOD, SENTRY_OP, SENTRY_ORIGIN, URL_DOMAIN, URL_FULL } from "@sentry/conventions/attributes"; import { startInactiveSpan } from "@sentry/core/browser"; import { HTTP_CLIENT_STREAM } from "@sentry/conventions/op"; // src/integrations/fetchStreamPerformance.ts const responseToStreamSpan = /* @__PURE__ */ new WeakMap(); const responseToFallbackTimeout = /* @__PURE__ */ new WeakMap(); const STREAM_RESOLVE_FALLBACK_MS = 9e4; const STREAMING_CONTENT_TYPES = [ "text/event-stream", "application/x-ndjson", "application/stream+json" ]; /** * Tracks streamed fetch response bodies by creating an `http.client.stream` sibling span. * * The regular `http.client` span ends when response headers arrive. This integration adds * a span that starts at header arrival and ends when the body fully resolves: * * ``` * --------- pageload -------------------------------- * -- http.client -- * -- http.client.stream ------- * ``` */ const fetchStreamPerformanceIntegration = defineIntegration(() => { return { name: "FetchStreamPerformance", setup(client) { addFetchEndInstrumentationHandler((handlerData) => { if (handlerData.response) { const streamSpan = responseToStreamSpan.get(handlerData.response); if (streamSpan && handlerData.endTimestamp) { streamSpan.end(handlerData.endTimestamp); const fallbackTimeout = responseToFallbackTimeout.get(handlerData.response); if (fallbackTimeout) clearTimeout(fallbackTimeout); } } }); addFetchInstrumentationHandler((handlerData) => { if (handlerData.endTimestamp && handlerData.response) { const contentType = handlerData.response.headers?.get("content-type") || ""; if (handlerData.response.headers?.get("content-length") || !STREAMING_CONTENT_TYPES.some((t) => contentType.startsWith(t))) return; const url = handlerData.fetchData?.url || ""; const method = handlerData.fetchData?.method || "GET"; const parsedUrl = parseStringToURLObject(url); const sanitizedUrl = url.startsWith("data:") ? stripDataUrlContent(url) : parsedUrl ? getSanitizedUrlStringFromUrlObject(parsedUrl) : url; const domain = getUrlDomain(url, WINDOW.location?.href); const streamedName = domain ? `${method} ${domain}` : method; const streamSpan = startInactiveSpan({ name: hasSpanStreamingEnabled(client) ? streamedName : `${method} ${sanitizedUrl}`, startTime: handlerData.endTimestamp, attributes: { [URL_FULL]: filterCollectedUrl(stripDataUrlContent(url)), [URL_DOMAIN]: domain, [HTTP_REQUEST_METHOD]: method, type: "fetch", [SENTRY_OP]: HTTP_CLIENT_STREAM, [SENTRY_ORIGIN]: "auto.http.browser.stream" } }); responseToStreamSpan.set(handlerData.response, streamSpan); const fallbackTimeout = setTimeout(() => { if (streamSpan.isRecording()) streamSpan.end(); }, STREAM_RESOLVE_FALLBACK_MS); responseToFallbackTimeout.set(handlerData.response, fallbackTimeout); } }); } }; }); export { fetchStreamPerformanceIntegration }; //# sourceMappingURL=fetchStreamPerformance.js.map