File tree Expand file tree Collapse file tree 1 file changed +20
-6
lines changed
packages/next/src/server/api-utils Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change 1
- import type { IncomingMessage } from 'http'
1
+ import type { IncomingMessage , ServerResponse } from 'http'
2
2
import type { BaseNextRequest } from '../base-http'
3
3
import type { CookieSerializeOptions } from 'next/dist/compiled/cookie'
4
4
import type { NextApiResponse } from '../../shared/lib/utils'
@@ -20,19 +20,33 @@ export type __ApiPreviewProps = {
20
20
previewModeSigningKey : string
21
21
}
22
22
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 {
27
28
return ( ( ...args ) => {
28
29
getTracer ( ) . setRootSpanAttribute ( 'next.route' , page )
29
30
// Call API route method
30
31
return getTracer ( ) . trace (
31
32
NodeSpan . runHandler ,
32
33
{
33
34
spanName : `executing api route (pages) ${ page } ` ,
35
+ manualSpanEnd : true ,
34
36
} ,
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
+ }
36
50
)
37
51
} ) as T
38
52
}
You can’t perform that action at this time.
0 commit comments