-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(react): Add reactRouterV4/V5BrowserTracingIntegration
for react router v4 & v5
#10488
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
import { WINDOW } from '@sentry/browser'; | ||
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; | ||
import type { Transaction, TransactionSource } from '@sentry/types'; | ||
import { | ||
WINDOW, | ||
browserTracingIntegration, | ||
startBrowserTracingNavigationSpan, | ||
startBrowserTracingPageLoadSpan, | ||
} from '@sentry/browser'; | ||
import { | ||
SEMANTIC_ATTRIBUTE_SENTRY_OP, | ||
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, | ||
getActiveSpan, | ||
getRootSpan, | ||
spanToJSON, | ||
} from '@sentry/core'; | ||
import type { Integration, Span, StartSpanOptions, Transaction, TransactionSource } from '@sentry/types'; | ||
import hoistNonReactStatics from 'hoist-non-react-statics'; | ||
import * as React from 'react'; | ||
|
||
|
@@ -23,29 +35,121 @@ export type RouteConfig = { | |
routes?: RouteConfig[]; | ||
}; | ||
|
||
type MatchPath = (pathname: string, props: string | string[] | any, parent?: Match | null) => Match | null; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
export type MatchPath = (pathname: string, props: string | string[] | any, parent?: Match | null) => Match | null; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
|
||
interface ReactRouterOptions { | ||
history: RouterHistory; | ||
routes?: RouteConfig[]; | ||
matchPath?: MatchPath; | ||
} | ||
|
||
let activeTransaction: Transaction | undefined; | ||
|
||
/** | ||
* A browser tracing integration that uses React Router v4 to instrument navigations. | ||
* Expects `history` (and optionally `routes` and `matchPath`) to be passed as options. | ||
*/ | ||
export function browserTracingReactRouterV4Integration( | ||
options: Parameters<typeof browserTracingIntegration>[0] & ReactRouterOptions, | ||
): Integration { | ||
const integration = browserTracingIntegration({ | ||
...options, | ||
instrumentPageLoad: false, | ||
instrumentNavigation: false, | ||
}); | ||
|
||
const { history, routes, matchPath, instrumentPageLoad = true, instrumentNavigation = true } = options; | ||
|
||
return { | ||
...integration, | ||
afterAllSetup(client) { | ||
integration.afterAllSetup(client); | ||
|
||
const startPageloadCallback = (startSpanOptions: StartSpanOptions): undefined => { | ||
startBrowserTracingPageLoadSpan(client, startSpanOptions); | ||
return undefined; | ||
}; | ||
|
||
const startNavigationCallback = (startSpanOptions: StartSpanOptions): undefined => { | ||
startBrowserTracingNavigationSpan(client, startSpanOptions); | ||
return undefined; | ||
}; | ||
|
||
// eslint-disable-next-line deprecation/deprecation | ||
const instrumentation = reactRouterV4Instrumentation(history, routes, matchPath); | ||
|
||
// Now instrument page load & navigation with correct settings | ||
instrumentation(startPageloadCallback, instrumentPageLoad, false); | ||
instrumentation(startNavigationCallback, false, instrumentNavigation); | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* A browser tracing integration that uses React Router v5 to instrument navigations. | ||
* Expects `history` (and optionally `routes` and `matchPath`) to be passed as options. | ||
*/ | ||
export function browserTracingReactRouterV5Integration( | ||
options: Parameters<typeof browserTracingIntegration>[0] & ReactRouterOptions, | ||
): Integration { | ||
const integration = browserTracingIntegration({ | ||
...options, | ||
instrumentPageLoad: false, | ||
instrumentNavigation: false, | ||
}); | ||
|
||
const { history, routes, matchPath } = options; | ||
|
||
return { | ||
...integration, | ||
afterAllSetup(client) { | ||
integration.afterAllSetup(client); | ||
|
||
const startPageloadCallback = (startSpanOptions: StartSpanOptions): undefined => { | ||
startBrowserTracingPageLoadSpan(client, startSpanOptions); | ||
return undefined; | ||
}; | ||
|
||
const startNavigationCallback = (startSpanOptions: StartSpanOptions): undefined => { | ||
startBrowserTracingNavigationSpan(client, startSpanOptions); | ||
return undefined; | ||
}; | ||
|
||
// eslint-disable-next-line deprecation/deprecation | ||
const instrumentation = reactRouterV5Instrumentation(history, routes, matchPath); | ||
|
||
// Now instrument page load & navigation with correct settings | ||
instrumentation(startPageloadCallback, options.instrumentPageLoad, false); | ||
instrumentation(startNavigationCallback, false, options.instrumentNavigation); | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* @deprecated Use `browserTracingReactRouterV4()` instead. | ||
*/ | ||
export function reactRouterV4Instrumentation( | ||
history: RouterHistory, | ||
routes?: RouteConfig[], | ||
matchPath?: MatchPath, | ||
): ReactRouterInstrumentation { | ||
return createReactRouterInstrumentation(history, 'react-router-v4', routes, matchPath); | ||
return createReactRouterInstrumentation(history, 'reactrouter_v4', routes, matchPath); | ||
} | ||
|
||
/** | ||
* @deprecated Use `browserTracingReactRouterV5()` instead. | ||
*/ | ||
export function reactRouterV5Instrumentation( | ||
history: RouterHistory, | ||
routes?: RouteConfig[], | ||
matchPath?: MatchPath, | ||
): ReactRouterInstrumentation { | ||
return createReactRouterInstrumentation(history, 'react-router-v5', routes, matchPath); | ||
return createReactRouterInstrumentation(history, 'reactrouter_v5', routes, matchPath); | ||
} | ||
|
||
function createReactRouterInstrumentation( | ||
history: RouterHistory, | ||
name: string, | ||
instrumentationName: string, | ||
allRoutes: RouteConfig[] = [], | ||
matchPath?: MatchPath, | ||
): ReactRouterInstrumentation { | ||
|
@@ -83,21 +187,17 @@ function createReactRouterInstrumentation( | |
return [pathname, 'url']; | ||
} | ||
|
||
const tags = { | ||
'routing.instrumentation': name, | ||
}; | ||
|
||
return (customStartTransaction, startTransactionOnPageLoad = true, startTransactionOnLocationChange = true): void => { | ||
const initPathName = getInitPathName(); | ||
|
||
if (startTransactionOnPageLoad && initPathName) { | ||
const [name, source] = normalizeTransactionName(initPathName); | ||
activeTransaction = customStartTransaction({ | ||
name, | ||
op: 'pageload', | ||
origin: 'auto.pageload.react.reactrouter', | ||
tags, | ||
metadata: { | ||
source, | ||
attributes: { | ||
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload', | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.pageload.react.${instrumentationName}`, | ||
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source, | ||
}, | ||
}); | ||
} | ||
|
@@ -112,11 +212,10 @@ function createReactRouterInstrumentation( | |
const [name, source] = normalizeTransactionName(location.pathname); | ||
activeTransaction = customStartTransaction({ | ||
name, | ||
op: 'navigation', | ||
origin: 'auto.navigation.react.reactrouter', | ||
tags, | ||
metadata: { | ||
source, | ||
attributes: { | ||
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation', | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.navigation.react.${instrumentationName}`, | ||
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source, | ||
}, | ||
}); | ||
} | ||
|
@@ -164,10 +263,12 @@ function computeRootMatch(pathname: string): Match { | |
export function withSentryRouting<P extends Record<string, any>, R extends React.ComponentType<P>>(Route: R): R { | ||
const componentDisplayName = (Route as any).displayName || (Route as any).name; | ||
|
||
const activeRootSpan = getActiveRootSpan(); | ||
|
||
const WrappedRoute: React.FC<P> = (props: P) => { | ||
if (activeTransaction && props && props.computedMatch && props.computedMatch.isExact) { | ||
activeTransaction.updateName(props.computedMatch.path); | ||
activeTransaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route'); | ||
if (activeRootSpan && props && props.computedMatch && props.computedMatch.isExact) { | ||
activeRootSpan.updateName(props.computedMatch.path); | ||
activeRootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route'); | ||
} | ||
|
||
// @ts-expect-error Setting more specific React Component typing for `R` generic above | ||
|
@@ -184,3 +285,22 @@ export function withSentryRouting<P extends Record<string, any>, R extends React | |
return WrappedRoute; | ||
} | ||
/* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */ | ||
|
||
function getActiveRootSpan(): Span | undefined { | ||
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. might be worth lifting this utility out 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, but here it's a bit special because it also takes the |
||
// Legacy behavior for "old" react router instrumentation | ||
if (activeTransaction) { | ||
return activeTransaction; | ||
} | ||
|
||
const span = getActiveSpan(); | ||
const rootSpan = span ? getRootSpan(span) : undefined; | ||
|
||
if (!rootSpan) { | ||
return undefined; | ||
} | ||
|
||
const op = spanToJSON(rootSpan).op; | ||
|
||
// Only use this root span if it is a pageload or navigation span | ||
return op === 'navigation' || op === 'pageload' ? rootSpan : undefined; | ||
} |
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.
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.
Idk if this makes sense but
reactRouterV5BrowserTracingIntegration
would sound better to me. Any reason you chose this order? ^^