Skip to content

Commit

Permalink
Rebase onto changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Aug 17, 2022
1 parent 02c1d43 commit d996be7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
14 changes: 10 additions & 4 deletions packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ type GetInitialProps = Required<NextPage<unknown>>['getInitialProps'];
/**
* Create a wrapped version of the user's exported `getInitialProps` function
*
* @param origGetInitialProps: The user's `getInitialProps` function
* @param origGIPropsHost: The user's object on which `getInitialProps` lives (used for `this`)
* @param origGetInitialProps - The user's `getInitialProps` function
* @param parameterizedRoute - The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryGetInitialProps(origGetInitialProps: GetInitialProps, route: string): GetInitialProps {
export function withSentryGetInitialProps(
origGetInitialProps: GetInitialProps,
parameterizedRoute: string,
): GetInitialProps {
return async function (
...getInitialPropsArguments: Parameters<GetInitialProps>
): Promise<ReturnType<GetInitialProps>> {
return await callDataFetcherTraced(origGetInitialProps, getInitialPropsArguments, { route, op: 'getInitialProps' });
return callDataFetcherTraced(origGetInitialProps, getInitialPropsArguments, {
parameterizedRoute,
dataFetchingMethodName: 'getInitialProps',
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { callDataFetcherTraced } from './wrapperUtils';
/**
* Create a wrapped version of the user's exported `getServerSideProps` function
*
* @param origGetServerSideProps: The user's `getServerSideProps` function
* @param route: The page's parameterized route
* @param origGetServerSideProps - The user's `getServerSideProps` function
* @param parameterizedRoute - The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryGetServerSideProps(
origGetServerSideProps: GetServerSideProps,
route: string,
parameterizedRoute: string,
): GetServerSideProps {
return async function (
...getServerSidePropsArguments: Parameters<GetServerSideProps>
): ReturnType<GetServerSideProps> {
return await callDataFetcherTraced(origGetServerSideProps, getServerSidePropsArguments, {
route,
op: 'getServerSideProps',
return callDataFetcherTraced(origGetServerSideProps, getServerSidePropsArguments, {
parameterizedRoute,
dataFetchingMethodName: 'getServerSideProps',
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ type Props = { [key: string]: unknown };
* Create a wrapped version of the user's exported `getStaticProps` function
*
* @param origGetStaticProps: The user's `getStaticProps` function
* @param route: The page's parameterized route
* @param parameterizedRoute - The page's parameterized route
* @returns A wrapped version of the function
*/
export function withSentryGetStaticProps(
origGetStaticProps: GetStaticProps<Props>,
route: string,
parameterizedRoute: string,
): GetStaticProps<Props> {
return async function (
...getStaticPropsArguments: Parameters<GetStaticProps<Props>>
): ReturnType<GetStaticProps<Props>> {
return await callDataFetcherTraced(origGetStaticProps, getStaticPropsArguments, { route, op: 'getStaticProps' });
return callDataFetcherTraced(origGetStaticProps, getStaticPropsArguments, {
parameterizedRoute,
dataFetchingMethodName: 'getStaticProps',
});
};
}
27 changes: 16 additions & 11 deletions packages/nextjs/src/config/wrappers/wrapperUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { captureException } from '@sentry/core';
import { getActiveTransaction } from '@sentry/tracing';
import { Span } from '@sentry/types';

/**
* Call a data fetcher and trace it. Only traces the function if there is an active transaction on the scope.
Expand All @@ -12,36 +11,42 @@ export async function callDataFetcherTraced<F extends (...args: any[]) => Promis
origFunction: F,
origFunctionArgs: Parameters<F>,
options: {
route: string;
op: string;
parameterizedRoute: string;
dataFetchingMethodName: string;
},
): Promise<ReturnType<F>> {
const { route, op } = options;
const { parameterizedRoute, dataFetchingMethodName } = options;

const transaction = getActiveTransaction();

if (!transaction) {
return origFunction(...origFunctionArgs);
}

// Pull off any leading underscores we've added in the process of wrapping the function
const wrappedFunctionName = origFunction.name.replace(/^_*/, '');

// TODO: Make sure that the given route matches the name of the active transaction (to prevent background data
// fetching from switching the name to a completely other route) -- We'll probably switch to creating a transaction
// right here so making that check will probabably not even be necessary.
// Logic will be: If there is no active transaction, start one with correct name and source. If there is an active
// transaction, create a child span with correct name and source.
transaction.name = route;
transaction.name = parameterizedRoute;
transaction.metadata.source = 'route';

// Capture the route, since pre-loading, revalidation, etc might mean that this span may happen during another
// route's transaction
const span = transaction.startChild({ op: 'nextjs.data', description: `${wrappedFunctionName} (${route})` });
const span = transaction.startChild({
op: 'nextjs.data',
description: `${dataFetchingMethodName} (${parameterizedRoute})`,
});

try {
return await origFunction(...origFunctionArgs);
} finally {
span.finish();
} catch (err) {
if (span) {
span.finish();
}

// TODO Copy more robust error handling over from `withSentry`
captureException(err);
throw err;
}
}

0 comments on commit d996be7

Please sign in to comment.