11import { logger , logSchema , redactQueryParamFromRequest } from '@internal/monitoring'
2+ import { RouteGenericInterface } from 'fastify'
23import { FastifyReply } from 'fastify/types/reply'
34import { FastifyRequest } from 'fastify/types/request'
45import fastifyPlugin from 'fastify-plugin'
@@ -7,6 +8,11 @@ interface RequestLoggerOptions {
78 excludeUrls ?: string [ ]
89}
910
11+ type RawRequestMetadata = FastifyRequest [ 'raw' ] & {
12+ executionError ?: Error
13+ resources ?: string [ ]
14+ }
15+
1016declare module 'fastify' {
1117 interface FastifyRequest {
1218 executionError ?: Error
@@ -18,8 +24,8 @@ declare module 'fastify' {
1824
1925 interface FastifyContextConfig {
2026 operation ?: { type : string }
21- resources ?: ( req : FastifyRequest < any > ) => string [ ]
22- logMetadata ?: ( req : FastifyRequest < any > ) => Record < string , unknown >
27+ resources ?( req : FastifyRequest < RouteGenericInterface > ) : string [ ]
28+ logMetadata ?( req : FastifyRequest < RouteGenericInterface > ) : Record < string , unknown >
2329 }
2430}
2531
@@ -64,30 +70,11 @@ export const logRequest = (options: RequestLoggerOptions) =>
6470 }
6571
6672 if ( resources === undefined ) {
67- resources = ( req . raw as any ) . resources
73+ resources = getRawRequest ( req ) . resources
6874 }
6975
7076 if ( resources === undefined ) {
71- const params = req . params as Record < string , unknown > | undefined
72- let resourceFromParams = ''
73-
74- if ( params ) {
75- let first = true
76- for ( const key in params ) {
77- if ( ! Object . prototype . hasOwnProperty . call ( params , key ) ) {
78- continue
79- }
80-
81- if ( ! first ) {
82- resourceFromParams += '/'
83- }
84-
85- const value = params [ key ]
86- resourceFromParams += value == null ? '' : String ( value )
87- first = false
88- }
89- }
90-
77+ const resourceFromParams = getResourceFromParams ( req . params )
9178 resources = resourceFromParams ? [ resourceFromParams ] : [ ]
9279 }
9380
@@ -150,7 +137,7 @@ function doRequestLog(req: FastifyRequest, options: LogRequestOptions) {
150137 const rId = req . id
151138 const cIP = req . ip
152139 const statusCode = options . statusCode
153- const error = ( req . raw as any ) . executionError || req . executionError
140+ const error = getRawRequest ( req ) . executionError || req . executionError
154141 const tenantId = req . tenantId
155142
156143 let reqMetadata : Record < string , unknown > = { }
@@ -197,3 +184,32 @@ function doRequestLog(req: FastifyRequest, options: LogRequestOptions) {
197184 serverTimes : req . serverTimings ,
198185 } )
199186}
187+
188+ function getRawRequest ( req : FastifyRequest ) : RawRequestMetadata {
189+ return req . raw as RawRequestMetadata
190+ }
191+
192+ function getResourceFromParams ( params : unknown ) : string {
193+ if ( ! params || typeof params !== 'object' ) {
194+ return ''
195+ }
196+
197+ let resource = ''
198+ let first = true
199+
200+ for ( const key in params ) {
201+ if ( ! Object . prototype . hasOwnProperty . call ( params , key ) ) {
202+ continue
203+ }
204+
205+ if ( ! first ) {
206+ resource += '/'
207+ }
208+
209+ const value = ( params as Record < string , unknown > ) [ key ]
210+ resource += value == null ? '' : String ( value )
211+ first = false
212+ }
213+
214+ return resource
215+ }
0 commit comments