1- import { PartialHttpData , parsePartialHttp } from '@internal/http'
21import { logger , logSchema , redactQueryParamFromRequest } from '@internal/monitoring'
32import { FastifyInstance } from 'fastify'
43import { FastifyReply } from 'fastify/types/reply'
@@ -49,37 +48,23 @@ export const logRequest = (options: RequestLoggerOptions) =>
4948 // Watch for connections that timeout or disconnect before complete HTTP headers are received
5049 // For keep-alive connections, track each potential request independently
5150 const onConnection = ( socket : Socket ) => {
52- const captureByteLimit = 2048
53- let currentRequestData : Buffer [ ] = [ ]
54- let currentRequestDataSize = 0
55- let currentRequestStart = Date . now ( )
56- let waitingForRequest = false
57- let pendingRequestLogged = false
58-
59- // Store cleanup function so hooks can access it
51+ const connectionStart = Date . now ( )
52+ let currentRequestStart = connectionStart
53+ let hasReceivedData = false
54+ let requestLogged = false
55+
56+ // Store cleanup function so hooks can mark requests as logged
6057 socketCleanupMap . set ( socket , ( ) => {
61- pendingRequestLogged = true
62- waitingForRequest = false
63- currentRequestData = [ ]
64- currentRequestDataSize = 0
58+ requestLogged = true
6559 } )
6660
67- // Capture partial data sent before connection closes
68- const onData = ( chunk : Buffer ) => {
69- // Start tracking a new potential request when we receive data after a completed one
70- if ( ! waitingForRequest ) {
71- waitingForRequest = true
72- currentRequestData = [ ]
73- currentRequestDataSize = 0
61+ // Track when data arrives for a potential request
62+ const onData = ( ) => {
63+ // Reset tracking for each new request on keep-alive connections
64+ if ( ! hasReceivedData || requestLogged ) {
65+ hasReceivedData = true
7466 currentRequestStart = Date . now ( )
75- pendingRequestLogged = false
76- }
77-
78- const remaining = captureByteLimit - currentRequestDataSize
79- if ( remaining > 0 ) {
80- const slicedChunk = chunk . subarray ( 0 , Math . min ( chunk . length , remaining ) )
81- currentRequestData . push ( slicedChunk )
82- currentRequestDataSize += slicedChunk . length
67+ requestLogged = false
8368 }
8469 }
8570 socket . on ( 'data' , onData )
@@ -93,19 +78,17 @@ export const logRequest = (options: RequestLoggerOptions) =>
9378 socket . removeListener ( 'data' , onData )
9479 socketCleanupMap . delete ( socket )
9580
96- // Only log if we were waiting for a request that was never properly logged
97- if ( ! waitingForRequest || currentRequestData . length === 0 || pendingRequestLogged ) {
98- return
99- }
100-
101- const parsedHttp = parsePartialHttp ( currentRequestData )
102- const req = createPartialLogRequest ( fastify , socket , parsedHttp , currentRequestStart )
81+ // Log if connection closed without a logged request
82+ // This covers: idle timeouts, partial data, malformed requests
83+ if ( ! requestLogged ) {
84+ const req = createPartialLogRequest ( fastify , socket , currentRequestStart )
10385
104- doRequestLog ( req , {
105- excludeUrls : options . excludeUrls ,
106- statusCode : 'ABORTED CONN' ,
107- responseTime : ( Date . now ( ) - req . startTime ) / 1000 ,
108- } )
86+ doRequestLog ( req , {
87+ excludeUrls : options . excludeUrls ,
88+ statusCode : 'ABORTED CONN' ,
89+ responseTime : ( Date . now ( ) - req . startTime ) / 1000 ,
90+ } )
91+ }
10992 } )
11093 }
11194
@@ -272,30 +255,25 @@ function getFirstDefined<T>(...values: any[]): T | undefined {
272255}
273256
274257/**
275- * Creates a minimal FastifyRequest from partial HTTP data .
276- * Used for consistent logging when request parsing fails .
258+ * Creates a minimal FastifyRequest for logging aborted connections .
259+ * Used when connection closes before a complete HTTP request is received .
277260 */
278261export function createPartialLogRequest (
279262 fastify : FastifyInstance ,
280263 socket : Socket ,
281- httpData : PartialHttpData ,
282264 startTime : number
283265) {
284266 return {
285- method : httpData . method ,
286- url : httpData . url ,
287- headers : httpData . headers ,
267+ method : 'UNKNOWN' ,
268+ headers : { } ,
269+ url : '/' ,
288270 ip : socket . remoteAddress || 'unknown' ,
289271 id : 'no-request' ,
290272 log : fastify . log . child ( {
291- tenantId : httpData . tenantId ,
292- project : httpData . tenantId ,
293273 reqId : 'no-request' ,
294274 appVersion : version ,
295- dataLength : httpData . length ,
296275 } ) ,
297276 startTime,
298- tenantId : httpData . tenantId ,
299277 raw : { } ,
300278 routeOptions : { config : { } } ,
301279 resources : [ ] ,
0 commit comments