From 0abd102a4ef8fd83bbf768f3f70c33f405972694 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 28 Mar 2025 14:47:59 +0100 Subject: [PATCH] feat(browser): Set page context instead of using request interface --- .../suites/integrations/httpContext/test.ts | 2 +- .../manual-client/browser-context/init.js | 4 +- packages/angular/src/sdk.ts | 4 +- packages/browser/src/exports.ts | 3 +- .../browser/src/integrations/httpcontext.ts | 36 ---------------- .../src/integrations/pageinformation.ts | 41 +++++++++++++++++++ packages/browser/src/sdk.ts | 4 +- 7 files changed, 50 insertions(+), 44 deletions(-) delete mode 100644 packages/browser/src/integrations/httpcontext.ts create mode 100644 packages/browser/src/integrations/pageinformation.ts diff --git a/dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts b/dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts index 190aae12ce18..a18605f7db44 100644 --- a/dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts +++ b/dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts @@ -4,7 +4,7 @@ import type { Event } from '@sentry/core'; import { sentryTest } from '../../../utils/fixtures'; import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers'; -sentryTest('httpContextIntegration captures user-agent and referrer', async ({ getLocalTestUrl, page }) => { +sentryTest('pageInformationIntegration captures user-agent and referrer', async ({ getLocalTestUrl, page }) => { const url = await getLocalTestUrl({ testDir: __dirname }); const errorEventPromise = getFirstSentryEnvelopeRequest(page); diff --git a/dev-packages/browser-integration-tests/suites/manual-client/browser-context/init.js b/dev-packages/browser-integration-tests/suites/manual-client/browser-context/init.js index a02b77c89b49..e52cc72569a1 100644 --- a/dev-packages/browser-integration-tests/suites/manual-client/browser-context/init.js +++ b/dev-packages/browser-integration-tests/suites/manual-client/browser-context/init.js @@ -5,7 +5,7 @@ import { dedupeIntegration, defaultStackParser, functionToStringIntegration, - httpContextIntegration, + pageInformationIntegration, eventFiltersIntegration, linkedErrorsIntegration, makeFetchTransport, @@ -15,7 +15,7 @@ const integrations = [ breadcrumbsIntegration(), functionToStringIntegration(), dedupeIntegration(), - httpContextIntegration(), + pageInformationIntegration(), eventFiltersIntegration(), linkedErrorsIntegration(), ]; diff --git a/packages/angular/src/sdk.ts b/packages/angular/src/sdk.ts index 9752db9cc384..3cef951d42fd 100755 --- a/packages/angular/src/sdk.ts +++ b/packages/angular/src/sdk.ts @@ -4,7 +4,7 @@ import { breadcrumbsIntegration, browserSessionIntegration, globalHandlersIntegration, - httpContextIntegration, + pageInformationIntegration, init as browserInit, linkedErrorsIntegration, setContext, @@ -41,7 +41,7 @@ export function getDefaultIntegrations(_options: BrowserOptions = {}): Integrati globalHandlersIntegration(), linkedErrorsIntegration(), dedupeIntegration(), - httpContextIntegration(), + pageInformationIntegration(), browserSessionIntegration(), ]; } diff --git a/packages/browser/src/exports.ts b/packages/browser/src/exports.ts index 8745e34106f4..345aba792ec7 100644 --- a/packages/browser/src/exports.ts +++ b/packages/browser/src/exports.ts @@ -88,7 +88,8 @@ export { getDefaultIntegrations, forceLoad, init, onLoad, showReportDialog } fro export { breadcrumbsIntegration } from './integrations/breadcrumbs'; export { globalHandlersIntegration } from './integrations/globalhandlers'; -export { httpContextIntegration } from './integrations/httpcontext'; +// eslint-disable-next-line deprecation/deprecation +export { httpContextIntegration, pageInformationIntegration } from './integrations/pageinformation'; export { linkedErrorsIntegration } from './integrations/linkederrors'; export { browserApiErrorsIntegration } from './integrations/browserapierrors'; diff --git a/packages/browser/src/integrations/httpcontext.ts b/packages/browser/src/integrations/httpcontext.ts deleted file mode 100644 index 78e27713c78f..000000000000 --- a/packages/browser/src/integrations/httpcontext.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { defineIntegration, getLocationHref } from '@sentry/core'; -import { WINDOW } from '../helpers'; - -/** - * Collects information about HTTP request headers and - * attaches them to the event. - */ -export const httpContextIntegration = defineIntegration(() => { - return { - name: 'HttpContext', - preprocessEvent(event) { - // if none of the information we want exists, don't bother - if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) { - return; - } - - // grab as much info as exists and add it to the event - const url = event.request?.url || getLocationHref(); - const { referrer } = WINDOW.document || {}; - const { userAgent } = WINDOW.navigator || {}; - - const headers = { - ...event.request?.headers, - ...(referrer && { Referer: referrer }), - ...(userAgent && { 'User-Agent': userAgent }), - }; - const request = { - ...event.request, - ...(url && { url }), - headers, - }; - - event.request = request; - }, - }; -}); diff --git a/packages/browser/src/integrations/pageinformation.ts b/packages/browser/src/integrations/pageinformation.ts new file mode 100644 index 000000000000..8a1568019ee9 --- /dev/null +++ b/packages/browser/src/integrations/pageinformation.ts @@ -0,0 +1,41 @@ +import { defineIntegration, getLocationHref } from '@sentry/core'; +import { WINDOW } from '../helpers'; + +/** + * Collects information about the current page and attaches it as context to the event. + */ +export const pageInformationIntegration = defineIntegration(() => { + return { + // TODO(v10): Update name to "PageInformation" + name: 'HttpContext', + preprocessEvent(event) { + // if none of the information we want exists, don't bother + if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) { + return; + } + + const href = getLocationHref(); + + // grab as much info as exists and add it to the event + const url = event.request?.url || href; + const request = { + ...(url && { url }), + }; + event.request = request; + + event.contexts = event.contexts || {}; + event.contexts.page = { + href: href || undefined, + referrer: WINDOW.document.referrer, + user_agent: WINDOW.navigator.userAgent, + }; + }, + }; +}); + +/** + * Collects information about the current page and attaches it as context to the event. + * + * @deprecated This integration was renamed to `pageInformationIntegration`, which should be used instead. + */ +export const httpContextIntegration = pageInformationIntegration; diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index 1c7e6fbe95ad..4dc5d1aa2f64 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -22,7 +22,7 @@ import { breadcrumbsIntegration } from './integrations/breadcrumbs'; import { browserApiErrorsIntegration } from './integrations/browserapierrors'; import { browserSessionIntegration } from './integrations/browsersession'; import { globalHandlersIntegration } from './integrations/globalhandlers'; -import { httpContextIntegration } from './integrations/httpcontext'; +import { pageInformationIntegration } from './integrations/pageinformation'; import { linkedErrorsIntegration } from './integrations/linkederrors'; import { defaultStackParser } from './stack-parsers'; import { makeFetchTransport } from './transports/fetch'; @@ -43,7 +43,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] { globalHandlersIntegration(), linkedErrorsIntegration(), dedupeIntegration(), - httpContextIntegration(), + pageInformationIntegration(), browserSessionIntegration(), ]; }