Skip to content
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

feat(nextjs): Instrument server-side getInitialProps of _app, _document and _error #5604

Merged
merged 4 commits into from
Aug 19, 2022

Conversation

lforst
Copy link
Member

@lforst lforst commented Aug 18, 2022

Ref: #5505

This PR adds wrappers for the "special page's" (_app, _document and error) getInitialProps. Luckily we have access to the parameterized route in getInitialProps so we can reuse the logic introduced in #5593.

Allows for transactions like the following:

Screen Shot 2022-08-18 at 10 17 20

And correctly captures exceptions when different errors are thrown in different methods!:

Screen Shot 2022-08-18 at 10 17 07

@lforst lforst changed the title feat(nextjs): Instrument server-side getInitialProps of _app, _document and error feat(nextjs): Instrument server-side getInitialProps of _app, _document and _error Aug 18, 2022
@lforst lforst marked this pull request as ready for review August 18, 2022 14:38
@github-actions
Copy link
Contributor

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.41 KB (-0.01% 🔽)
@sentry/browser - ES5 CDN Bundle (minified) 60.06 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 17.98 KB (+0.02% 🔺)
@sentry/browser - ES6 CDN Bundle (minified) 52.92 KB (0%)
@sentry/browser - Webpack (gzipped + minified) 19.77 KB (0%)
@sentry/browser - Webpack (minified) 64.31 KB (0%)
@sentry/react - Webpack (gzipped + minified) 19.79 KB (0%)
@sentry/nextjs Client - Webpack (gzipped + minified) 44.7 KB (-0.03% 🔽)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 25.9 KB (-0.06% 🔽)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 24.27 KB (-0.05% 🔽)

Copy link
Member

@lobsterkatie lobsterkatie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

withSentryServerSideAppGetInitialProps,
withSentryServerSideDocumentGetInitialProps,
withSentryServerSideErrorGetInitialProps,
} from "@sentry/nextjs";`;

if (parameterizedRouteName === '/_app') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once this is moved over to the proxy template, what would think of using a map for all of these checks? Something like

const getInitialPropsWrappers = {
  "/_app": Sentry.withSentryServerSideAppGetInitialProps,
  "/_document": Sentry.withSentryServerSideDocumentGetInitialProps,
  "/_error": Sentry.withSentryServerSideAppGetInitialProps,
}

const getInitialPropsWrapper = getInitialPropsWrappers["__ROUTE__"] || Sentry.withSentryServerSideErrorGetInitialProps

if (typeof origGetInitialProps === 'function') {
  pageComponent.getInitialProps = getInitialPropsWrapper(
    origGetInitialProps,
    '__ROUTE__',
  ) as NextPageComponent['getInitialProps'];
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we merge #5602 I will rebase this PR and also #5593 onto it so that the wrappers use the new proxy loader. This seems like a good approach! 👍

origGetInitialProps: GetInitialProps,
parameterizedRoute: string,
): GetInitialProps {
export function withSentryServerSideGetInitialProps(origGetInitialProps: GetInitialProps): GetInitialProps {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, maybe this will solve the problem I was having here, which forced me to do the typecast. I spent entirely too much time trying to bend TS to my will on that one and failed entirely.

Comment on lines +75 to +81
/** Parameterized route of the request - will be used for naming the transaction. */
requestedRouteName: string;
/** Name of the route the data fetcher was defined in - will be used for describing the data fetcher's span. */
dataFetcherRouteName: string;
/** Name of the data fetching method - will be used for describing the data fetcher's span. */
dataFetchingMethodName: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - this makes it really clear!

@lforst lforst force-pushed the lforst-create-transactions-for-getinitialprops-and-getserversideprops branch from 3141759 to a561697 Compare August 19, 2022 13:32
@lforst lforst force-pushed the lforst-wrap-special-getinitialprops branch from d2a0a55 to 0a4271b Compare August 19, 2022 13:38
Base automatically changed from lforst-create-transactions-for-getinitialprops-and-getserversideprops to master August 19, 2022 14:51
@lforst lforst force-pushed the lforst-wrap-special-getinitialprops branch from 00a8058 to d3f0311 Compare August 19, 2022 15:01
@lforst lforst enabled auto-merge (squash) August 19, 2022 15:01
@lforst lforst merged commit 5c462f1 into master Aug 19, 2022
@lforst lforst deleted the lforst-wrap-special-getinitialprops branch August 19, 2022 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants