-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add span creation and transaction name replacement to wrapper core fu…
…nction
- Loading branch information
1 parent
f6ff082
commit be3a2fa
Showing
3 changed files
with
40 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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,25 +1,46 @@ | ||
import { getActiveTransaction } from '@sentry/tracing'; | ||
|
||
import { DataFetchingFunction } from './types'; | ||
|
||
/** | ||
* Pass-through wrapper for the original function, used as a first step in eventually wrapping the data-fetching | ||
* functions with code for tracing. | ||
* Create a span to track the wrapped function and update transaction name with parameterized route. | ||
* | ||
* @template T Types for `getInitialProps`, `getStaticProps`, and `getServerSideProps` | ||
* @param origFunction The user's exported `getInitialProps`, `getStaticProps`, or `getServerSideProps` function | ||
* @param context The context object passed by nextjs to the function | ||
* @returns The result of calling the user's function | ||
*/ | ||
export async function callOriginal<T extends DataFetchingFunction>( | ||
origFunction: T['fn'], | ||
context: T['context'], | ||
): Promise<T['result']> { | ||
let props; | ||
|
||
// TODO: Can't figure out how to tell TS that the types are correlated - that a `GSPropsFunction` will only get passed | ||
// `GSPropsContext` and never, say, `GSSPContext`. That's what wrapping everything in objects and using the generic | ||
// and pulling the types from the generic rather than specifying them directly was supposed to do, but... no luck. | ||
// eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any | ||
props = await (origFunction as any)(context); | ||
|
||
return props; | ||
export async function wrapperCore<T extends DataFetchingFunction>(options: { | ||
origFunction: T['fn']; | ||
context: T['context']; | ||
route: string; | ||
op: string; | ||
}): Promise<T['result']> { | ||
const { origFunction, context, route, op } = options; | ||
|
||
const transaction = getActiveTransaction(); | ||
|
||
if (transaction) { | ||
// 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) | ||
transaction.name = route; | ||
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, data: { route } }); | ||
|
||
// TODO: Can't figure out how to tell TS that the types are correlated - that a `GSPropsFunction` will only get passed | ||
// `GSPropsContext` and never, say, `GSSPContext`. That's what wrapping everything in objects and using the generic | ||
// and pulling the types from the generic rather than specifying them directly was supposed to do, but... no luck. | ||
// eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any | ||
const props = await (origFunction as any)(context); | ||
|
||
span.finish(); | ||
|
||
return props; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return (origFunction as any)(context); | ||
} |