-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathwrapGetInitialPropsWithSentry.ts
66 lines (57 loc) · 2.54 KB
/
wrapGetInitialPropsWithSentry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { NextPage } from 'next';
import { isBuild } from '../utils/isBuild';
import { withErrorInstrumentation, withTracedServerSideDataFetcher } from '../utils/wrapperUtils';
type GetInitialProps = Required<NextPage>['getInitialProps'];
/**
* Create a wrapped version of the user's exported `getInitialProps` function
*
* @param origGetInitialProps The user's `getInitialProps` function
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps {
return new Proxy(origGetInitialProps, {
apply: async (wrappingTarget, thisArg, args: Parameters<GetInitialProps>) => {
if (isBuild()) {
return wrappingTarget.apply(thisArg, args);
}
const [context] = args;
const { req, res } = context;
const errorWrappedGetInitialProps = withErrorInstrumentation(wrappingTarget);
// Generally we can assume that `req` and `res` are always defined on the server:
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
// span with each other when there are no req or res objects, we simply do not trace them at all here.
if (req && res) {
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
dataFetcherRouteName: context.pathname,
requestedRouteName: context.pathname,
dataFetchingMethodName: 'getInitialProps',
});
const {
data: initialProps,
baggage,
sentryTrace,
}: {
data: {
_sentryTraceData?: string;
_sentryBaggage?: string;
};
baggage?: string;
sentryTrace?: string;
} = (await tracedGetInitialProps.apply(thisArg, args)) ?? {}; // Next.js allows undefined to be returned from a getInitialPropsFunction.
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
if (sentryTrace) {
initialProps._sentryTraceData = sentryTrace;
}
// The Next.js serializer throws on undefined values so we need to guard for it (#12102)
if (baggage) {
initialProps._sentryBaggage = baggage;
}
return initialProps;
} else {
return errorWrappedGetInitialProps.apply(thisArg, args);
}
},
});
}