feat(angular): Set url attributes on pageload and navigation spans#21985
feat(angular): Set url attributes on pageload and navigation spans#21985Lms24 wants to merge 2 commits into
url attributes on pageload and navigation spans#21985Conversation
41cc9bd to
3831498
Compare
9afa5d7 to
26d8b46
Compare
size-limit report 📦
|
26d8b46 to
d7ce684
Compare
3831498 to
2ad4840
Compare
| data: { | ||
| 'sentry.origin': 'auto.pageload.angular', | ||
| 'sentry.source': 'route', | ||
| 'url.template': '/home/', |
There was a problem hiding this comment.
q: We always want trailing slashes for this?
There was a problem hiding this comment.
I think we generally don't want trailing slashes, but since url.template should ideally be 100% the same as the current span description (to avoid any filtering/grouping changes in the product) I'll take it for now. We're consistent because:
- if we get a parameterized url, both
url.templateandspan.descriptionhave a trailing slash - if we don't get a paramterized url,
url.templateisn't set,url.pathhas no trailing slash and neither doesspan.description.
Not great but I think it's fine. We could adjust our instrumentation in a major to no longer set trailing slashes for paramterized routes but I'd say it's logaf-l
| 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 | ||
| } | ||
| } | ||
|
|
||
| span.setAttributes({ | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.${op}.angular`, | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', | ||
| [URL_FULL]: urlFull, | ||
| [URL_PATH]: urlPath, | ||
| [URL_TEMPLATE]: route, | ||
| }); | ||
| } |
There was a problem hiding this comment.
This is pretty verbose and probably adds to bundle size. Maybe, you could write something like this:
let href, pathname;
try {
({ href, pathname } = new URL(url, WINDOW.location?.origin));
} catch {}
span.setAttributes({
[URL_FULL]: href ?? url,
[URL_PATH]: pathname ?? url,
});There was a problem hiding this comment.
Good point, thx for raising! I have an optimization lined up for this in the PR on top of the stack: #21953
We have a recurring need to get an absolute from a relative URL, so I extracted a helper for this. Right now my thinking is that I'll extract framework-by-framework changes form the top of stack and use the last PR to clean up stuff.
2ad4840 to
8832da5
Compare
d7ce684 to
76f14ee
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 76f14ee. Configure here.
url attributes on pageload and navigation spans
Add url.full, url.path, and url.template to Angular tracing spans, resolve relative router URLs against the current origin, and extend e2e performance tests to assert the new attributes. Co-Authored-By: Cursor <cursoragent@cursor.com>
76f14ee to
212546b
Compare
8832da5 to
67c4c14
Compare
| 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 | ||
| } |
There was a problem hiding this comment.
Bug: In SSR environments, the url.full span attribute is incorrectly set to a relative path because WINDOW.location.origin is unavailable.
Severity: MEDIUM
Suggested Fix
Update _updateSpanAttributesForParametrizedUrl to only set the url.full attribute if an absolute URL can be constructed. If WINDOW.location.origin is not available, the attribute should not be set, preventing a relative path from being used. This aligns with the behavior in other parts of the codebase, such as packages/browser/src/tracing/utils.ts.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: packages/angular/src/tracing.ts#L72-L81
Potential issue: In Server-Side Rendering (SSR) environments where
`WINDOW.location.origin` is unavailable, the `_updateSpanAttributesForParametrizedUrl`
function incorrectly sets the `url.full` span attribute to a relative path instead of an
absolute URL. The code falls back to the raw URL when `WINDOW.location?.origin` is
falsy, which is common in Node.js environments. This violates the semantic contract of
the `url.full` attribute, which is expected to be a complete URL. This issue occurs
because the `TraceService` is initialized and subscribes to router events during SSR, a
legitimate use case for Angular applications.
Did we get this right? 👍 / 👎 to inform future reviews.

Adjusts the Angular routing instrumentation to set
url.templateand updateurl.fullandurl.pathwhen resolving routes. We not only have to settemplate(which we can only do in the framework/router-specific instrumentation). We also have to ensure we setfullandpathagain because in the default integration (#21952), we set it to early based on the window location path. So therefore, we update it again in the router instrumentation and re-set the raw URL (but the resolved one).ref #21921