Skip to content

Commit 8bd50da

Browse files
Lms24cursoragent
andcommitted
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 #21921 Co-Authored-By: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 1d77c34 commit 8bd50da

1 file changed

Lines changed: 6 additions & 30 deletions

File tree

packages/angular/src/tracing.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import {
1919
spanToJSON,
2020
startBrowserTracingNavigationSpan,
2121
startInactiveSpan,
22-
WINDOW,
22+
getAbsoluteUrl,
2323
} from '@sentry/browser';
2424
import type { Integration, Span } from '@sentry/core';
25-
import { debug, stripUrlQueryAndFragment, timestampInSeconds } from '@sentry/core';
25+
import { debug, parseStringToURLObject, stripUrlQueryAndFragment, timestampInSeconds } from '@sentry/core';
2626
import type { Observable } from 'rxjs';
2727
import { Subscription } from 'rxjs';
2828
import { filter, tap } from 'rxjs/operators';
@@ -66,26 +66,13 @@ export function _updateSpanAttributesForParametrizedUrl(route: string, url: stri
6666
if (!attributes || attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'url') {
6767
span.updateName(route);
6868

69-
// Angular router gives us relative paths (e.g. `/users/123`). Resolve against the
70-
// current origin so that `url.full` contains the absolute URL including protocol and host.
71-
const locationOrigin = WINDOW.location?.origin;
72-
let urlFull = url;
73-
let urlPath = url;
74-
if (locationOrigin) {
75-
try {
76-
const parsed = new URL(url, locationOrigin);
77-
urlFull = parsed.href;
78-
urlPath = parsed.pathname;
79-
} catch {
80-
// fall back to the raw string
81-
}
82-
}
69+
const absoluteUrl = getAbsoluteUrl(url);
8370

8471
span.setAttributes({
8572
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.${op}.angular`,
8673
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
87-
[URL_FULL]: urlFull,
88-
[URL_PATH]: urlPath,
74+
[URL_FULL]: absoluteUrl,
75+
[URL_PATH]: parseStringToURLObject(absoluteUrl)?.pathname,
8976
[URL_TEMPLATE]: route,
9077
});
9178
}
@@ -118,17 +105,6 @@ export class TraceService implements OnDestroy {
118105
// see comment in `_isPageloadOngoing` for rationale
119106
if (!this._isPageloadOngoing()) {
120107
runOutsideAngular(() => {
121-
// Angular router gives us a relative path; resolve it against the current origin
122-
// so the browser tracing integration can set url.full correctly from the start.
123-
const locationOrigin = WINDOW.location?.origin;
124-
let absoluteUrl: string = navigationEvent.url;
125-
if (locationOrigin) {
126-
try {
127-
absoluteUrl = new URL(navigationEvent.url, locationOrigin).href;
128-
} catch {
129-
// fall back to relative path
130-
}
131-
}
132108
startBrowserTracingNavigationSpan(
133109
client,
134110
{
@@ -139,7 +115,7 @@ export class TraceService implements OnDestroy {
139115
},
140116
},
141117
{
142-
url: absoluteUrl,
118+
url: getAbsoluteUrl(navigationEvent.url),
143119
},
144120
);
145121
});

0 commit comments

Comments
 (0)