-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(nuxt): Parametrize routes on the server-side #16785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2edf0af
rename error button
s1gr1d 963cc3f
test(nuxt): Add test for distributed server request
s1gr1d 86de9e0
fix button naming to be exact
s1gr1d 58e6f4f
add test for distributed client-side API request
s1gr1d 56c916f
Standardize test case
s1gr1d 2be99a2
add distributed test case to all nuxt tests
s1gr1d 32d72fb
update readme
s1gr1d 3537d54
fix(nuxt): Parametrize routes on the server-side
s1gr1d 4e75465
parametrize server api requests
s1gr1d 5362333
update attribute
s1gr1d 0c398ab
Merge branch 'develop' into sig/nitro-parametrized-routes
s1gr1d 7da3667
review suggestions and fix tests
s1gr1d e733556
remove updateSpanName
s1gr1d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/nuxt/src/runtime/hooks/updateRouteBeforeResponse.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { getActiveSpan, getCurrentScope, getRootSpan, logger, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; | ||
import type { H3Event } from 'h3'; | ||
|
||
/** | ||
* Update the root span (transaction) name for routes with parameters based on the matched route. | ||
*/ | ||
export function updateRouteBeforeResponse(event: H3Event): void { | ||
if (event.context.matchedRoute) { | ||
s1gr1d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const matchedRoutePath = event.context.matchedRoute.path; | ||
|
||
// If the matched route path is defined and differs from the event's path, it indicates a parametrized route | ||
// Example: Matched route is "/users/:id" and the event's path is "/users/123", | ||
if (matchedRoutePath && matchedRoutePath !== event._path) { | ||
if (matchedRoutePath === '/**') { | ||
// todo: support parametrized SSR pageload spans | ||
// If page is server-side rendered, the whole path gets transformed to `/**` (Example : `/users/123` becomes `/**` instead of `/users/:id`). | ||
return; // Skip if the matched route is a catch-all route. | ||
} | ||
|
||
const method = event._method || 'GET'; | ||
|
||
const parametrizedTransactionName = `${method.toUpperCase()} ${matchedRoutePath}`; | ||
getCurrentScope().setTransactionName(parametrizedTransactionName); | ||
|
||
const activeSpan = getActiveSpan(); // In development mode, getActiveSpan() is always undefined | ||
if (activeSpan) { | ||
const rootSpan = getRootSpan(activeSpan); | ||
if (rootSpan) { | ||
rootSpan.updateName(parametrizedTransactionName); | ||
rootSpan.setAttributes({ | ||
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', | ||
'http.route': matchedRoutePath, | ||
}); | ||
|
||
const params = event.context?.params || null; | ||
s1gr1d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (params && typeof params === 'object') { | ||
Object.entries(params).forEach(([key, value]) => { | ||
// Based on this convention: https://getsentry.github.io/sentry-conventions/generated/attributes/url.html#urlpathparameterkey | ||
rootSpan.setAttribute(`url.path.parameter.${key}`, String(value)); | ||
}); | ||
} | ||
|
||
logger.log(`Updated transaction name for parametrized route: ${parametrizedTransactionName}`); | ||
chargome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.