From 9d41e7adf7ef59f4e6f564a3baef8b8630d3f2a3 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 8 Jul 2026 14:50:03 +0200 Subject: [PATCH] feat(angular): Use getAbsoluteUrl for pageload and navigation url attributes Refactors Angular routing to use the shared getAbsoluteUrl helper for url.full and url.path on pageload and navigation spans. part of https://github.com/getsentry/sentry-javascript/issues/21921 Co-Authored-By: Cursor Agent Co-authored-by: Cursor --- packages/angular/src/tracing.ts | 36 ++++++--------------------------- 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/packages/angular/src/tracing.ts b/packages/angular/src/tracing.ts index f720d5c8803c..3825e199c742 100644 --- a/packages/angular/src/tracing.ts +++ b/packages/angular/src/tracing.ts @@ -19,10 +19,10 @@ import { spanToJSON, startBrowserTracingNavigationSpan, startInactiveSpan, - WINDOW, + getAbsoluteUrl, } from '@sentry/browser'; import type { Integration, Span } from '@sentry/core'; -import { debug, stripUrlQueryAndFragment, timestampInSeconds } from '@sentry/core'; +import { debug, parseStringToURLObject, stripUrlQueryAndFragment, timestampInSeconds } from '@sentry/core'; import type { Observable } from 'rxjs'; import { Subscription } from 'rxjs'; import { filter, tap } from 'rxjs/operators'; @@ -66,26 +66,13 @@ export function _updateSpanAttributesForParametrizedUrl(route: string, url: stri if (!attributes || attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'url') { span.updateName(route); - // Angular router gives us relative paths (e.g. `/users/123`). Resolve against the - // current origin so that `url.full` contains the absolute URL including protocol and host. - const locationOrigin = WINDOW.location?.origin; - let urlFull = url; - let urlPath = url; - if (locationOrigin) { - try { - const parsed = new URL(url, locationOrigin); - urlFull = parsed.href; - urlPath = parsed.pathname; - } catch { - // fall back to the raw string - } - } + const absoluteUrl = getAbsoluteUrl(url); span.setAttributes({ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.${op}.angular`, [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', - [URL_FULL]: urlFull, - [URL_PATH]: urlPath, + [URL_FULL]: absoluteUrl, + [URL_PATH]: parseStringToURLObject(absoluteUrl)?.pathname, [URL_TEMPLATE]: route, }); } @@ -118,17 +105,6 @@ export class TraceService implements OnDestroy { // see comment in `_isPageloadOngoing` for rationale if (!this._isPageloadOngoing()) { runOutsideAngular(() => { - // Angular router gives us a relative path; resolve it against the current origin - // so the browser tracing integration can set url.full correctly from the start. - const locationOrigin = WINDOW.location?.origin; - let absoluteUrl: string = navigationEvent.url; - if (locationOrigin) { - try { - absoluteUrl = new URL(navigationEvent.url, locationOrigin).href; - } catch { - // fall back to relative path - } - } startBrowserTracingNavigationSpan( client, { @@ -139,7 +115,7 @@ export class TraceService implements OnDestroy { }, }, { - url: absoluteUrl, + url: getAbsoluteUrl(navigationEvent.url), }, ); });