-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nextjs): Frontend + withSentry Performance Monitoring #3580
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 all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
594e5ca
feat(nextjs): Add server-side request transactions (#3533)
lobsterkatie 17105aa
Merge branch 'master' into kmclb-next-js-performance
lobsterkatie b6b922a
add missing tracing dependency back in
lobsterkatie 135b1bf
Merge branch 'master' into kmclb-next-js-performance
lobsterkatie 1d90053
Merge branch 'master' into kmclb-next-js-performance
lobsterkatie 0e46ae4
Merge branch 'master' into kmclb-next-js-performance
lobsterkatie 5d4dcab
feat(nextjs): Client performance monitoring (#3552)
iker-barriocanal da07ea7
fix(nextjs): Use domains to prevent scope bleed on backend (#3574)
lobsterkatie 4c2a385
Merge branch 'master' into kmclb-next-js-performance
lobsterkatie 415d04e
Merge branch 'master' into kmclb-next-js-performance
lobsterkatie a0bab2f
feat(nextjs): Parameterize server-side transaction names and harvest …
lobsterkatie 596f83a
chore(nextjs): Generalized server-side performance cleanup (#3581)
lobsterkatie 1bee1f3
feat(nextjs): Continue trace using incoming `sentry-trace` header (#3…
lobsterkatie 65aea8a
ref(nextjs): Improve webpack config merging (#3596)
lobsterkatie 4d37cd1
feat(nextjs): Automatic performance monitoring (#3586)
iker-barriocanal b6f9dc6
fix(nextjs): Fix duplicated extension when compiling `sentry.server.c…
lobsterkatie 6afe381
ref(nextjs): Use common util to strip query params (#3598)
AbhiPrasad b3ffadd
fix(nextjs): Check if public dir exists (#3602)
AbhiPrasad 41b6ee1
feat: transactions in withSentry handler
HazAT 82318d2
fix: Build
HazAT 11a7a26
fix: Parameter urls for API requests
HazAT 800fc88
fix(nextjs): Add `api/` to BrowserTracing tracingOrigins (#3607)
AbhiPrasad ab21117
fix(nextjs): Fix return type when injecting sentry code using webpack…
lobsterkatie d657e8f
Merge branch 'master' into kmclb-next-js-performance
HazAT 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,17 +1,48 @@ | ||
| import { configureScope, init as reactInit } from '@sentry/react'; | ||
| import { defaultRequestInstrumentationOptions, Integrations } from '@sentry/tracing'; | ||
|
|
||
| import { nextRouterInstrumentation } from './performance/client'; | ||
| import { MetadataBuilder } from './utils/metadataBuilder'; | ||
| import { NextjsOptions } from './utils/nextjsOptions'; | ||
| import { addIntegration, UserIntegrations } from './utils/userIntegrations'; | ||
|
|
||
| export * from '@sentry/react'; | ||
| export { nextRouterInstrumentation } from './performance/client'; | ||
|
|
||
| const { BrowserTracing } = Integrations; | ||
|
|
||
| /** Inits the Sentry NextJS SDK on the browser with the React SDK. */ | ||
| export function init(options: NextjsOptions): void { | ||
| const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'react']); | ||
| metadataBuilder.addSdkMetadata(); | ||
| options.environment = options.environment || process.env.NODE_ENV; | ||
| reactInit(options); | ||
|
|
||
| // Only add BrowserTracing if a tracesSampleRate or tracesSampler is set | ||
| const integrations = | ||
| options.tracesSampleRate === undefined && options.tracesSampler === undefined | ||
| ? options.integrations | ||
| : createClientIntegrations(options.integrations); | ||
|
|
||
| reactInit({ | ||
| ...options, | ||
| integrations, | ||
| }); | ||
| configureScope(scope => { | ||
| scope.setTag('runtime', 'browser'); | ||
| }); | ||
| } | ||
|
|
||
| const defaultBrowserTracingIntegration = new BrowserTracing({ | ||
| tracingOrigins: [...defaultRequestInstrumentationOptions.tracingOrigins, /^(api\/)/], | ||
| routingInstrumentation: nextRouterInstrumentation, | ||
| }); | ||
|
|
||
| function createClientIntegrations(integrations?: UserIntegrations): UserIntegrations { | ||
| if (integrations) { | ||
| return addIntegration(defaultBrowserTracingIntegration, integrations, { | ||
| BrowserTracing: { keyPath: 'options.routingInstrumentation', value: nextRouterInstrumentation }, | ||
| }); | ||
| } else { | ||
| return [defaultBrowserTracingIntegration]; | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { Primitive, Transaction, TransactionContext } from '@sentry/types'; | ||
| import { fill, getGlobalObject, stripUrlQueryAndFragment } from '@sentry/utils'; | ||
| import { default as Router } from 'next/router'; | ||
|
|
||
| const global = getGlobalObject<Window>(); | ||
|
|
||
| type StartTransactionCb = (context: TransactionContext) => Transaction | undefined; | ||
|
|
||
| const DEFAULT_TAGS = Object.freeze({ | ||
| 'routing.instrumentation': 'next-router', | ||
| }); | ||
|
|
||
| let activeTransaction: Transaction | undefined = undefined; | ||
| let prevTransactionName: string | undefined = undefined; | ||
| let startTransaction: StartTransactionCb | undefined = undefined; | ||
|
|
||
| /** | ||
| * Creates routing instrumention for Next Router. Only supported for | ||
| * client side routing. Works for Next >= 10. | ||
| * | ||
| * Leverages the SingletonRouter from the `next/router` to | ||
| * generate pageload/navigation transactions and parameterize | ||
| * transaction names. | ||
| */ | ||
| export function nextRouterInstrumentation( | ||
| startTransactionCb: StartTransactionCb, | ||
| startTransactionOnPageLoad: boolean = true, | ||
| startTransactionOnLocationChange: boolean = true, | ||
| ): void { | ||
| startTransaction = startTransactionCb; | ||
| Router.ready(() => { | ||
| // We can only start the pageload transaction when we have access to the parameterized | ||
| // route name. Setting the transaction name after the transaction is started could lead | ||
| // to possible race conditions with the router, so this approach was taken. | ||
| if (startTransactionOnPageLoad) { | ||
| prevTransactionName = Router.route !== null ? stripUrlQueryAndFragment(Router.route) : global.location.pathname; | ||
| activeTransaction = startTransactionCb({ | ||
| name: prevTransactionName, | ||
| op: 'pageload', | ||
| tags: DEFAULT_TAGS, | ||
| }); | ||
| } | ||
|
|
||
| // Spans that aren't attached to any transaction are lost; so if transactions aren't | ||
| // created (besides potentially the onpageload transaction), no need to wrap the router. | ||
| if (!startTransactionOnLocationChange) return; | ||
|
|
||
| // `withRouter` uses `useRouter` underneath: | ||
| // https://github.com/vercel/next.js/blob/de42719619ae69fbd88e445100f15701f6e1e100/packages/next/client/with-router.tsx#L21 | ||
| // Router events also use the router: | ||
| // https://github.com/vercel/next.js/blob/de42719619ae69fbd88e445100f15701f6e1e100/packages/next/client/router.ts#L92 | ||
| // `Router.changeState` handles the router state changes, so it may be enough to only wrap it | ||
| // (instead of wrapping all of the Router's functions). | ||
| const routerPrototype = Object.getPrototypeOf(Router.router); | ||
| fill(routerPrototype, 'changeState', changeStateWrapper); | ||
| }); | ||
| } | ||
|
|
||
| type RouterChangeState = ( | ||
| method: string, | ||
| url: string, | ||
| as: string, | ||
| options: Record<string, any>, | ||
| ...args: any[] | ||
| ) => void; | ||
| type WrappedRouterChangeState = RouterChangeState; | ||
|
|
||
| /** | ||
| * Wraps Router.changeState() | ||
| * https://github.com/vercel/next.js/blob/da97a18dafc7799e63aa7985adc95f213c2bf5f3/packages/next/next-server/lib/router/router.ts#L1204 | ||
| * Start a navigation transaction every time the router changes state. | ||
| */ | ||
| function changeStateWrapper(originalChangeStateWrapper: RouterChangeState): WrappedRouterChangeState { | ||
| const wrapper = function( | ||
| this: any, | ||
| method: string, | ||
| // The parameterized url, ex. posts/[id]/[comment] | ||
| url: string, | ||
| // The actual url, ex. posts/85/my-comment | ||
| as: string, | ||
| options: Record<string, any>, | ||
| // At the moment there are no additional arguments (meaning the rest parameter is empty). | ||
| // This is meant to protect from future additions to Next.js API, especially since this is an | ||
| // internal API. | ||
| ...args: any[] | ||
| ): Promise<boolean> { | ||
| if (startTransaction !== undefined) { | ||
| if (activeTransaction) { | ||
| activeTransaction.finish(); | ||
| } | ||
| const tags: Record<string, Primitive> = { | ||
| ...DEFAULT_TAGS, | ||
| method, | ||
| ...options, | ||
| }; | ||
| if (prevTransactionName) { | ||
| tags.from = prevTransactionName; | ||
| } | ||
| prevTransactionName = stripUrlQueryAndFragment(url); | ||
| activeTransaction = startTransaction({ | ||
| name: prevTransactionName, | ||
| op: 'navigation', | ||
| tags, | ||
| }); | ||
| } | ||
| return originalChangeStateWrapper.call(this, method, url, as, options, ...args); | ||
| }; | ||
| return wrapper; | ||
| } |
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.
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.
This is imported twice