@sentry/browser
Version:
Official Sentry SDK for browsers
46 lines (43 loc) • 1.75 kB
JavaScript
import { defineIntegration, safeSetSpanJSONAttributes } from '@sentry/core/browser';
import { WINDOW, getHttpRequestData } from '../helpers.js';
import { HTTP_REQUEST_HEADER_KEY_BASE, URL_FULL, SENTRY_OP, USER_AGENT_ORIGINAL } from '@sentry/conventions/attributes';
const httpContextIntegration = defineIntegration(() => {
return {
name: "HttpContext",
preprocessEvent(event) {
if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) {
return;
}
const reqData = getHttpRequestData();
const headers = {
...reqData.headers,
...event.request?.headers
};
event.request = {
...reqData,
...event.request,
headers
};
},
processSpan(span) {
if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) {
return;
}
const reqData = getHttpRequestData();
safeSetSpanJSONAttributes(span, {
// This attribute is used by the "Filter out events from legacy browsers and crawlers" features on the Sentry backend.
// Therefore, it's set on every span.
[USER_AGENT_ORIGINAL]: reqData.headers["User-Agent"],
// These attributes, we only need on the segment span (analogous to the `request` context for events)
...span.is_segment && {
// Coerce empty string to undefined so the helper's nullish check drops it,
// rather than writing an empty `url.full` attribute onto the span.
[URL_FULL]: span.attributes?.[SENTRY_OP] !== "http.client" ? reqData.url : void 0,
[`${HTTP_REQUEST_HEADER_KEY_BASE}.referer`]: reqData.headers["Referer"]
}
});
}
};
});
export { httpContextIntegration };
//# sourceMappingURL=httpcontext.js.map