-
-
Couldn't load subscription status.
- Fork 1.7k
feat(react): Add TanStack Router integration #12095
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
Changes from 1 commit
7cd66ce
2983c42
7c839ed
af12b07
a3a2008
fbf0247
a27f2da
17242cc
2e9ca18
8a5b592
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { WINDOW, startBrowserTracingNavigationSpan, startBrowserTracingPageLoadSpan } from "@sentry/browser"; | ||
| import { | ||
| SEMANTIC_ATTRIBUTE_SENTRY_OP, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, | ||
| } from "@sentry/core"; | ||
|
|
||
| import { browserTracingIntegration as originalBrowserTracingIntegration } from "@sentry/react"; | ||
| import type { Client, Integration } from "@sentry/types"; | ||
|
|
||
| import type { TanstackRouter } from "./types"; | ||
|
|
||
| /** | ||
| * A custom browser tracing integration for Tanstack Router. | ||
| */ | ||
| export function tanstackRouterBrowserTracingIntegration( | ||
| router: TanstackRouter, | ||
| options: Parameters<typeof originalBrowserTracingIntegration>[0] = {} | ||
| ): Integration { | ||
| const browserTracingIntegrationInstance = originalBrowserTracingIntegration({ | ||
| ...options, | ||
| instrumentNavigation: false, | ||
| instrumentPageLoad: false, | ||
| }); | ||
|
|
||
| const { instrumentPageLoad = true, instrumentNavigation = true } = options; | ||
|
|
||
| return { | ||
| ...browserTracingIntegrationInstance, | ||
| afterAllSetup(client) { | ||
| const initPathName = WINDOW && WINDOW.location && WINDOW.location.pathname; | ||
| if (instrumentPageLoad && initPathName) { | ||
| startBrowserTracingPageLoadSpan(client, { | ||
| name: initPathName, | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: "url", | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_OP]: "pageload", | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: "auto.pageload.react.tanstack_router", | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| if (instrumentNavigation) { | ||
| tanstackRouterInstrumentNavigation(router, client); | ||
| } | ||
|
|
||
| browserTracingIntegrationInstance.afterAllSetup(client); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export function tanstackRouterInstrumentNavigation(router: TanstackRouter, client: Client): void { | ||
| router.history.subscribe(() => { | ||
|
||
| const state = router.state; | ||
| const matches = state.pendingMatches ?? state.matches; | ||
|
||
| const lastMatch = matches[matches.length - 1]; | ||
| if (!lastMatch) return; | ||
|
|
||
| const routeId = lastMatch?.routeId; | ||
| if (!routeId) return; | ||
|
|
||
| startBrowserTracingNavigationSpan(client, { | ||
| name: routeId, | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_OP]: "navigation", | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: "auto.navigation.tanstack_router.router_instrumentation", | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: "route", | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there no way for us to get the
routeIdfor the pageload? We can read this fromrouter.statealso right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I believe so. Sorry, I missed putting it there. @lforst can you put that in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
matches[matches.length - 1].routeIdrouter.routesById[routeId]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did the
matches[matches.length - 1].routeIdapproach below, just forgot to put it here as well.