Skip to content

fix(nextjs): Use Proxies to wrap to preserve static methods #7002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ type AppGetInitialProps = typeof App['getInitialProps'];
* so we are consistent with the serverside implementation.
*/
export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps {
return async function (this: unknown, ...args: Parameters<AppGetInitialProps>): ReturnType<AppGetInitialProps> {
return await origAppGetInitialProps.apply(this, args);
};
return new Proxy(origAppGetInitialProps, {
apply: async (wrappingTarget, thisArg, args: Parameters<AppGetInitialProps>) => {
return await wrappingTarget.apply(thisArg, args);
},
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ type DocumentGetInitialProps = typeof Document.getInitialProps;
export function wrapDocumentGetInitialPropsWithSentry(
origDocumentGetInitialProps: DocumentGetInitialProps,
): DocumentGetInitialProps {
return async function (
this: unknown,
...args: Parameters<DocumentGetInitialProps>
): ReturnType<DocumentGetInitialProps> {
return await origDocumentGetInitialProps.apply(this, args);
};
return new Proxy(origDocumentGetInitialProps, {
apply: async (wrappingTarget, thisArg, args: Parameters<DocumentGetInitialProps>) => {
return await wrappingTarget.apply(thisArg, args);
},
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ type ErrorGetInitialProps = (context: NextPageContext) => Promise<ErrorProps>;
export function wrapErrorGetInitialPropsWithSentry(
origErrorGetInitialProps: ErrorGetInitialProps,
): ErrorGetInitialProps {
return async function (this: unknown, ...args: Parameters<ErrorGetInitialProps>): ReturnType<ErrorGetInitialProps> {
return await origErrorGetInitialProps.apply(this, args);
};
return new Proxy(origErrorGetInitialProps, {
apply: async (wrappingTarget, thisArg, args: Parameters<ErrorGetInitialProps>) => {
return await wrappingTarget.apply(thisArg, args);
},
});
}

/**
Expand Down
8 changes: 5 additions & 3 deletions packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ type GetInitialProps = Required<NextPage>['getInitialProps'];
* so we are consistent with the serverside implementation.
*/
export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps {
return async function (this: unknown, ...args: Parameters<GetInitialProps>): Promise<ReturnType<GetInitialProps>> {
return origGetInitialProps.apply(this, args);
};
return new Proxy(origGetInitialProps, {
apply: async (wrappingTarget, thisArg, args: Parameters<GetInitialProps>) => {
return await wrappingTarget.apply(thisArg, args);
},
});
}

/**
Expand Down
28 changes: 15 additions & 13 deletions packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ export function wrapApiHandlerWithSentry<H extends EdgeRouteHandler>(
handler: H,
parameterizedRoute: string,
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
return async function (this: unknown, ...args: Parameters<H>): Promise<ReturnType<H>> {
const req = args[0];
return new Proxy(handler, {
apply: async (wrappingTarget, thisArg, args: Parameters<H>) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supported, e.g. having an async apply? Just wondering :D I guess we don't even really need it here, though? Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the term "supported" but from my testing, using an async function just seems to return a promise which is about what I would expect to happen. We definitely need it here though because we wanna await the result so that the events are flushed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good, if it works then 👍 maybe it is just a restriction for get based proxies, I vaguely remember having issues with such a thing in the past. But all good then, nice!

const req = args[0];

const activeSpan = !!getCurrentHub().getScope()?.getSpan();
const activeSpan = !!getCurrentHub().getScope()?.getSpan();

const wrappedHandler = withEdgeWrapping(handler, {
spanDescription:
activeSpan || !(req instanceof Request)
? `handler (${parameterizedRoute})`
: `${req.method} ${parameterizedRoute}`,
spanOp: activeSpan ? 'function' : 'http.server',
mechanismFunctionName: 'wrapApiHandlerWithSentry',
});
const wrappedHandler = withEdgeWrapping(wrappingTarget, {
spanDescription:
activeSpan || !(req instanceof Request)
? `handler (${parameterizedRoute})`
: `${req.method} ${parameterizedRoute}`,
spanOp: activeSpan ? 'function' : 'http.server',
mechanismFunctionName: 'wrapApiHandlerWithSentry',
});

return await wrappedHandler.apply(this, args);
};
return await wrappedHandler.apply(thisArg, args);
},
});
}

/**
Expand Down
12 changes: 8 additions & 4 deletions packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import { withEdgeWrapping } from './utils/edgeWrapperUtils';
export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
middleware: H,
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
return withEdgeWrapping(middleware, {
spanDescription: 'middleware',
spanOp: 'middleware.nextjs',
mechanismFunctionName: 'withSentryMiddleware',
return new Proxy(middleware, {
apply: async (wrappingTarget, thisArg, args: Parameters<H>) => {
return withEdgeWrapping(wrappingTarget, {
spanDescription: 'middleware',
spanOp: 'middleware.nextjs',
mechanismFunctionName: 'withSentryMiddleware',
}).apply(thisArg, args);
},
});
}
31 changes: 0 additions & 31 deletions packages/nextjs/src/server/utils/nextLogger.ts

This file was deleted.

Loading