Skip to content

Commit 3b43514

Browse files
committed
simplify logging and remove partial data tracking
1 parent 7fadaed commit 3b43514

3 files changed

Lines changed: 9 additions & 111 deletions

File tree

src/http/plugins/log-request.ts

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { PartialHttpData, parsePartialHttp } from '@internal/http'
21
import { logger, logSchema, redactQueryParamFromRequest } from '@internal/monitoring'
32
import { FastifyInstance } from 'fastify'
43
import { FastifyReply } from 'fastify/types/reply'
@@ -49,9 +48,6 @@ 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
5551
let currentRequestStart = Date.now()
5652
let waitingForRequest = false
5753
let pendingRequestLogged = false
@@ -60,27 +56,16 @@ export const logRequest = (options: RequestLoggerOptions) =>
6056
socketCleanupMap.set(socket, () => {
6157
pendingRequestLogged = true
6258
waitingForRequest = false
63-
currentRequestData = []
64-
currentRequestDataSize = 0
6559
})
6660

67-
// Capture partial data sent before connection closes
68-
const onData = (chunk: Buffer) => {
61+
// Track when data arrives for a potential request
62+
const onData = () => {
6963
// Start tracking a new potential request when we receive data after a completed one
7064
if (!waitingForRequest) {
7165
waitingForRequest = true
72-
currentRequestData = []
73-
currentRequestDataSize = 0
7466
currentRequestStart = Date.now()
7567
pendingRequestLogged = false
7668
}
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
83-
}
8469
}
8570
socket.on('data', onData)
8671

@@ -94,12 +79,11 @@ export const logRequest = (options: RequestLoggerOptions) =>
9479
socketCleanupMap.delete(socket)
9580

9681
// Only log if we were waiting for a request that was never properly logged
97-
if (!waitingForRequest || currentRequestData.length === 0 || pendingRequestLogged) {
82+
if (!waitingForRequest || pendingRequestLogged) {
9883
return
9984
}
10085

101-
const parsedHttp = parsePartialHttp(currentRequestData)
102-
const req = createPartialLogRequest(fastify, socket, parsedHttp, currentRequestStart)
86+
const req = createPartialLogRequest(fastify, socket, currentRequestStart)
10387

10488
doRequestLog(req, {
10589
excludeUrls: options.excludeUrls,
@@ -272,30 +256,25 @@ function getFirstDefined<T>(...values: any[]): T | undefined {
272256
}
273257

274258
/**
275-
* Creates a minimal FastifyRequest from partial HTTP data.
276-
* Used for consistent logging when request parsing fails.
259+
* Creates a minimal FastifyRequest for logging aborted connections.
260+
* Used when connection closes before a complete HTTP request is received.
277261
*/
278262
export function createPartialLogRequest(
279263
fastify: FastifyInstance,
280264
socket: Socket,
281-
httpData: PartialHttpData,
282265
startTime: number
283266
) {
284267
return {
285-
method: httpData.method,
286-
url: httpData.url,
287-
headers: httpData.headers,
268+
method: 'UNKNOWN',
269+
headers: {},
270+
url: '/',
288271
ip: socket.remoteAddress || 'unknown',
289272
id: 'no-request',
290273
log: fastify.log.child({
291-
tenantId: httpData.tenantId,
292-
project: httpData.tenantId,
293274
reqId: 'no-request',
294275
appVersion: version,
295-
dataLength: httpData.length,
296276
}),
297277
startTime,
298-
tenantId: httpData.tenantId,
299278
raw: {},
300279
routeOptions: { config: {} },
301280
resources: [],

src/internal/http/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export * from './agent'
2-
export * from './partial-http-parser'

src/internal/http/partial-http-parser.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)