-
-
Notifications
You must be signed in to change notification settings - 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(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure about the scope, but do we not need to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we can add one, we just need to hook it onto closing the Sentry client, which we currently don't have an exposed hook for in the public API. |
||
const state = router.state; | ||
const matches = state.pendingMatches ?? state.matches; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems like what we do internally is more like this:
I did find multiple implementations of this with different spreading order, so I'm not sure which one would be correct 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll want one of these depending on the situation:
I recommend the former. Do not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tannerlinsley I did |
||
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
routeId
for the pageload? We can read this fromrouter.state
also 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].routeId
router.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].routeId
approach below, just forgot to put it here as well.