|
| 1 | +import { getActiveTransaction } from '@sentry/tracing'; |
| 2 | + |
1 | 3 | import { DataFetchingFunction } from './types';
|
2 | 4 |
|
3 | 5 | /**
|
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. |
6 | 7 | *
|
7 | 8 | * @template T Types for `getInitialProps`, `getStaticProps`, and `getServerSideProps`
|
8 | 9 | * @param origFunction The user's exported `getInitialProps`, `getStaticProps`, or `getServerSideProps` function
|
9 | 10 | * @param context The context object passed by nextjs to the function
|
10 | 11 | * @returns The result of calling the user's function
|
11 | 12 | */
|
12 |
| -export async function callOriginal<T extends DataFetchingFunction>( |
13 |
| - origFunction: T['fn'], |
14 |
| - context: T['context'], |
15 |
| -): Promise<T['result']> { |
16 |
| - let props; |
17 |
| - |
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); |
23 |
| - |
24 |
| - return props; |
| 13 | +export async function wrapperCore<T extends DataFetchingFunction>(options: { |
| 14 | + origFunction: T['fn']; |
| 15 | + context: T['context']; |
| 16 | + route: string; |
| 17 | + op: string; |
| 18 | +}): Promise<T['result']> { |
| 19 | + const { origFunction, context, route, op } = options; |
| 20 | + |
| 21 | + const transaction = getActiveTransaction(); |
| 22 | + |
| 23 | + if (transaction) { |
| 24 | + // TODO: Make sure that the given route matches the name of the active transaction (to prevent background data |
| 25 | + // fetching from switching the name to a completely other route) |
| 26 | + transaction.name = route; |
| 27 | + transaction.metadata.source = 'route'; |
| 28 | + |
| 29 | + // Capture the route, since pre-loading, revalidation, etc might mean that this span may happen during another |
| 30 | + // route's transaction |
| 31 | + const span = transaction.startChild({ op, data: { route } }); |
| 32 | + |
| 33 | + // TODO: Can't figure out how to tell TS that the types are correlated - that a `GSPropsFunction` will only get passed |
| 34 | + // `GSPropsContext` and never, say, `GSSPContext`. That's what wrapping everything in objects and using the generic |
| 35 | + // and pulling the types from the generic rather than specifying them directly was supposed to do, but... no luck. |
| 36 | + // eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any |
| 37 | + const props = await (origFunction as any)(context); |
| 38 | + |
| 39 | + span.finish(); |
| 40 | + |
| 41 | + return props; |
| 42 | + } |
| 43 | + |
| 44 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 45 | + return (origFunction as any)(context); |
25 | 46 | }
|
0 commit comments