UNPKG

@sentry/browser

Version:
46 lines (45 loc) • 2.16 kB
import { WINDOW, getHttpRequestData } from "../helpers.js"; import { _INTERNAL_filterKeyValueData, defineIntegration, filterCollectedUrl, safeSetSpanJSONAttributes } from "@sentry/core"; import { HTTP_REQUEST_HEADER_KEY_BASE, SENTRY_IS_LOCALHOST, SENTRY_OP, URL_FULL, USER_AGENT_ORIGINAL } from "@sentry/conventions/attributes"; // src/integrations/httpcontext.ts /** * Collects information about HTTP request headers and * attaches them to the event. */ const httpContextIntegration = defineIntegration(() => { return { name: "HttpContext", preprocessEvent(event, _hint, client) { if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) return; const { url, headers: collectedHeaders } = getHttpRequestData(client); const behavior = client.getDataCollectionOptions().httpHeaders.request; const userHeaders = Object.entries(event.request?.headers ?? {}).filter(([key]) => !(key in collectedHeaders)); const headers = { ..._INTERNAL_filterKeyValueData(collectedHeaders, behavior), ...Object.fromEntries(userHeaders) }; event.request = { url: filterCollectedUrl(url, client), ...event.request, ...Object.keys(headers).length > 0 ? { headers } : { headers: void 0 } }; }, processSpan(span, client) { if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) return; const reqData = getHttpRequestData(client); const headers = _INTERNAL_filterKeyValueData(reqData.headers, client.getDataCollectionOptions().httpHeaders.request); const referer = headers["Referer"]; const { hostname, protocol } = WINDOW.location || {}; safeSetSpanJSONAttributes(span, { [USER_AGENT_ORIGINAL]: headers["User-Agent"], [SENTRY_IS_LOCALHOST]: protocol === "file:" || hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]" || !!hostname?.endsWith(".localhost"), ...span.is_segment && { [URL_FULL]: span.attributes?.[SENTRY_OP] !== "http.client" ? filterCollectedUrl(reqData.url) : void 0, [`${HTTP_REQUEST_HEADER_KEY_BASE}.referer`]: referer ? [referer] : void 0 } }); } }; }); export { httpContextIntegration }; //# sourceMappingURL=httpcontext.js.map