Skip to content

Commit 8a6f322

Browse files
committed
Fix "executing api route" span
1 parent d5cd72d commit 8a6f322

File tree

1 file changed

+20
-6
lines changed
  • packages/next/src/server/api-utils

1 file changed

+20
-6
lines changed

packages/next/src/server/api-utils/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IncomingMessage } from 'http'
1+
import type { IncomingMessage, ServerResponse } from 'http'
22
import type { BaseNextRequest } from '../base-http'
33
import type { CookieSerializeOptions } from 'next/dist/compiled/cookie'
44
import type { NextApiResponse } from '../../shared/lib/utils'
@@ -20,19 +20,33 @@ export type __ApiPreviewProps = {
2020
previewModeSigningKey: string
2121
}
2222

23-
export function wrapApiHandler<T extends (...args: any[]) => any>(
24-
page: string,
25-
handler: T
26-
): T {
23+
export function wrapApiHandler<
24+
T extends (
25+
...args: [req: IncomingMessage, res: ServerResponse, ...any[]]
26+
) => any,
27+
>(page: string, handler: T): T {
2728
return ((...args) => {
2829
getTracer().setRootSpanAttribute('next.route', page)
2930
// Call API route method
3031
return getTracer().trace(
3132
NodeSpan.runHandler,
3233
{
3334
spanName: `executing api route (pages) ${page}`,
35+
manualSpanEnd: true,
3436
},
35-
() => handler(...args)
37+
(span) => {
38+
if (span) {
39+
const res = args[1]
40+
res.end = new Proxy(res.end, {
41+
apply(target, thisArg, argArray) {
42+
span.end()
43+
return target.apply(thisArg, argArray as any)
44+
},
45+
})
46+
}
47+
48+
return handler(...args)
49+
}
3650
)
3751
}) as T
3852
}

0 commit comments

Comments
 (0)