Skip to content

Commit 20abb1f

Browse files
committed
add span creation and transaction name replacement to wrapper core function
1 parent e03682c commit 20abb1f

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GSSP } from './types';
2-
import { callOriginal } from './wrapperUtils';
2+
import { wrapperCore } from './wrapperUtils';
33

44
/**
55
* Create a wrapped version of the user's exported `getServerSideProps` function
@@ -10,6 +10,6 @@ import { callOriginal } from './wrapperUtils';
1010
*/
1111
export function withSentryGetServerSideProps(origGetServerSideProps: GSSP['fn'], route: string): GSSP['wrappedFn'] {
1212
return async function (context: GSSP['context']): Promise<GSSP['result']> {
13-
return callOriginal<GSSP>(origGetServerSideProps, context);
13+
return wrapperCore<GSSP>(origGetServerSideProps, context, route);
1414
};
1515
}

packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GSProps } from './types';
2-
import { callOriginal } from './wrapperUtils';
2+
import { wrapperCore } from './wrapperUtils';
33

44
/**
55
* Create a wrapped version of the user's exported `getStaticProps` function
@@ -10,6 +10,6 @@ import { callOriginal } from './wrapperUtils';
1010
*/
1111
export function withSentryGetStaticProps(origGetStaticProps: GSProps['fn'], route: string): GSProps['wrappedFn'] {
1212
return async function (context: GSProps['context']): Promise<GSProps['result']> {
13-
return callOriginal<GSProps>(origGetStaticProps, context);
13+
return wrapperCore<GSProps>(origGetStaticProps, context, route);
1414
};
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
1+
import { getActiveTransaction } from '@sentry/tracing';
2+
13
import { DataFetchingFunction } from './types';
24

35
/**
4-
* Pass-through wrapper for the original function, used as a first step in eventually wrapping the data-fetching
5-
* functions with code for tracing.
6+
* Create a span to track the wrapped function and update transaction name with parameterized route.
67
*
78
* @template T Types for `getInitialProps`, `getStaticProps`, and `getServerSideProps`
89
* @param origFunction The user's exported `getInitialProps`, `getStaticProps`, or `getServerSideProps` function
910
* @param context The context object passed by nextjs to the function
11+
* @param route The route currently being served
1012
* @returns The result of calling the user's function
1113
*/
12-
export async function callOriginal<T extends DataFetchingFunction>(
14+
export async function wrapperCore<T extends DataFetchingFunction>(
1315
origFunction: T['fn'],
1416
context: T['context'],
17+
route: string,
1518
): Promise<T['result']> {
16-
let props;
19+
const transaction = getActiveTransaction();
20+
21+
if (transaction) {
22+
// Pull off any leading underscores we've added in the process of wrapping the function
23+
const wrappedFunctionName = origFunction.name.replace(/^_*/, '');
24+
25+
// TODO: Make sure that the given route matches the name of the active transaction (to prevent background data
26+
// fetching from switching the name to a completely other route)
27+
transaction.name = route;
28+
transaction.metadata.source = 'route';
29+
30+
// Capture the route, since pre-loading, revalidation, etc might mean that this span may happen during another
31+
// route's transaction
32+
const span = transaction.startChild({ op: 'nextjs.data', description: `${wrappedFunctionName} (${route})` });
33+
34+
// TODO: Can't figure out how to tell TS that the types are correlated - that a `GSPropsFunction` will only get passed
35+
// `GSPropsContext` and never, say, `GSSPContext`. That's what wrapping everything in objects and using the generic
36+
// and pulling the types from the generic rather than specifying them directly was supposed to do, but... no luck.
37+
// eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any
38+
const props = await (origFunction as any)(context);
39+
40+
span.finish();
1741

18-
// TODO: Can't figure out how to tell TS that the types are correlated - that a `GSPropsFunction` will only get passed
19-
// `GSPropsContext` and never, say, `GSSPContext`. That's what wrapping everything in objects and using the generic
20-
// and pulling the types from the generic rather than specifying them directly was supposed to do, but... no luck.
21-
// eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any
22-
props = await (origFunction as any)(context);
42+
return props;
43+
}
2344

24-
return props;
45+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
46+
return (origFunction as any)(context);
2547
}

0 commit comments

Comments
 (0)