UNPKG

@sentry/browser

Version:
75 lines (74 loc) • 3.64 kB
const require_helpers = require("../helpers.js"); let _sentry_core = require("@sentry/core"); let _sentry_conventions_attributes = require("@sentry/conventions/attributes"); let _sentry_core_browser = require("@sentry/core/browser"); let _sentry_conventions_op = require("@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 = (0, _sentry_core.defineIntegration)(() => { return { name: "FetchStreamPerformance", setup(client) { (0, _sentry_core.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); } } }); (0, _sentry_core.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 = (0, _sentry_core.parseStringToURLObject)(url); const sanitizedUrl = url.startsWith("data:") ? (0, _sentry_core.stripDataUrlContent)(url) : parsedUrl ? (0, _sentry_core.getSanitizedUrlStringFromUrlObject)(parsedUrl) : url; const domain = (0, _sentry_core.getUrlDomain)(url, require_helpers.WINDOW.location?.href); const streamedName = domain ? `${method} ${domain}` : method; const streamSpan = (0, _sentry_core_browser.startInactiveSpan)({ name: (0, _sentry_core.hasSpanStreamingEnabled)(client) ? streamedName : `${method} ${sanitizedUrl}`, startTime: handlerData.endTimestamp, attributes: { [_sentry_conventions_attributes.URL_FULL]: (0, _sentry_core.filterCollectedUrl)((0, _sentry_core.stripDataUrlContent)(url)), [_sentry_conventions_attributes.URL_DOMAIN]: domain, [_sentry_conventions_attributes.HTTP_REQUEST_METHOD]: method, type: "fetch", [_sentry_conventions_attributes.SENTRY_OP]: _sentry_conventions_op.HTTP_CLIENT_STREAM, [_sentry_conventions_attributes.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); } }); } }; }); exports.fetchStreamPerformanceIntegration = fetchStreamPerformanceIntegration; //# sourceMappingURL=fetchStreamPerformance.js.map