@@ -6,13 +6,13 @@ import {
6
6
} from '@nestjs/common' ;
7
7
import { GqlExecutionContext } from '@nestjs/graphql' ;
8
8
import XRay from 'aws-xray-sdk-core' ;
9
- import { HttpAdapter , HttpMiddleware as NestMiddleware } from '~/core/http' ;
9
+ import { GlobalHttpHook , HttpAdapter } from '~/core/http' ;
10
10
import { ConfigService } from '../config/config.service' ;
11
11
import { Sampler } from './sampler' ;
12
12
import { TracingService } from './tracing.service' ;
13
13
14
14
@Injectable ( )
15
- export class XRayMiddleware implements NestMiddleware , NestInterceptor {
15
+ export class XRayMiddleware implements NestInterceptor {
16
16
constructor (
17
17
private readonly tracing : TracingService ,
18
18
private readonly sampler : Sampler ,
@@ -23,12 +23,13 @@ export class XRayMiddleware implements NestMiddleware, NestInterceptor {
23
23
/**
24
24
* Setup root segment for request/response.
25
25
*/
26
- use : NestMiddleware [ 'use' ] = ( req , res , next ) => {
26
+ @GlobalHttpHook ( )
27
+ onRequest ( ...[ req , res , next ] : Parameters < GlobalHttpHook > ) {
27
28
const traceData = XRay . utils . processTraceData (
28
29
req . headers [ 'x-amzn-trace-id' ] as string | undefined ,
29
30
) ;
30
31
const root = new XRay . Segment ( 'cord' , traceData . root , traceData . parent ) ;
31
- const reqData = new XRay . middleware . IncomingRequestData ( req ) ;
32
+ const reqData = new XRay . middleware . IncomingRequestData ( req . raw ) ;
32
33
root . addIncomingRequestData ( reqData ) ;
33
34
// Use public DNS as url instead of specific IP
34
35
// @ts -expect-error xray library types suck
@@ -40,7 +41,7 @@ export class XRayMiddleware implements NestMiddleware, NestInterceptor {
40
41
enumerable : false ,
41
42
} ) ;
42
43
43
- res . on ( 'finish' , ( ) => {
44
+ res . raw . on ( 'finish' , ( ) => {
44
45
const status = res . statusCode . toString ( ) ;
45
46
if ( status . startsWith ( '4' ) ) {
46
47
root . addErrorFlag ( ) ;
@@ -57,7 +58,7 @@ export class XRayMiddleware implements NestMiddleware, NestInterceptor {
57
58
} ) ;
58
59
59
60
this . tracing . segmentStorage . run ( root , next ) ;
60
- } ;
61
+ }
61
62
62
63
/**
63
64
* Determine if segment should be traced/sampled.
0 commit comments