From 7b929a684c5da7b17e3bcc3f7a75e235ceed1df6 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Thu, 7 Nov 2024 16:01:25 +0100 Subject: [PATCH 1/2] fix: more details in event-observer http server logging --- src/event-stream/event-server.ts | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/event-stream/event-server.ts b/src/event-stream/event-server.ts index 2a2154b73..48c115ea9 100644 --- a/src/event-stream/event-server.ts +++ b/src/event-stream/event-server.ts @@ -757,10 +757,33 @@ export async function startEventServer(opts: { } const bodyLimit = 1_000_000 * 500; // 500MB body limit + + const reqLogSerializer = (req: FastifyRequest) => ({ + method: req.method, + url: req.url, + version: req.headers?.['accept-version'] as string, + hostname: req.hostname, + remoteAddress: req.ip, + remotePort: req.socket ? req.socket.remotePort : undefined, + bodySize: parseInt(req.headers?.['content-length'] as string) || 'unknown', + }); + const loggerOpts: FastifyServerOptions['logger'] = { ...PINO_LOGGER_CONFIG, name: 'stacks-node-event', + serializers: { + req: reqLogSerializer, + res: reply => ({ + statusCode: reply.statusCode, + method: reply.request?.method, + url: reply.request?.url, + requestBodySize: + parseInt(reply.request?.headers?.['content-length'] as string) || 'unknown', + responseBodySize: parseInt(reply.getHeader?.('content-length') as string) || 'unknown', + }), + }, }; + const app = Fastify({ bodyLimit, trustProxy: true, @@ -768,6 +791,18 @@ export async function startEventServer(opts: { ignoreTrailingSlash: true, }); + app.addHook('onRequest', (req, reply, done) => { + req.raw.on('close', () => { + if (req.raw.aborted) { + req.log.warn( + reqLogSerializer(req), + `Request was aborted by the client: ${req.method} ${req.url}` + ); + } + }); + done(); + }); + const handleRawEventRequest = async (req: FastifyRequest) => { await messageHandler.handleRawEventRequest(req.url, req.body, db); From a5f485cafcd9929b3a398ad6c49f008f17cc7822 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Thu, 7 Nov 2024 16:10:21 +0100 Subject: [PATCH 2/2] chore: misc cleanup --- src/event-stream/event-server.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/event-stream/event-server.ts b/src/event-stream/event-server.ts index 48c115ea9..64fbf4f45 100644 --- a/src/event-stream/event-server.ts +++ b/src/event-stream/event-server.ts @@ -764,7 +764,7 @@ export async function startEventServer(opts: { version: req.headers?.['accept-version'] as string, hostname: req.hostname, remoteAddress: req.ip, - remotePort: req.socket ? req.socket.remotePort : undefined, + remotePort: req.socket?.remotePort, bodySize: parseInt(req.headers?.['content-length'] as string) || 'unknown', }); @@ -777,8 +777,7 @@ export async function startEventServer(opts: { statusCode: reply.statusCode, method: reply.request?.method, url: reply.request?.url, - requestBodySize: - parseInt(reply.request?.headers?.['content-length'] as string) || 'unknown', + requestBodySize: parseInt(reply.request?.headers['content-length'] as string) || 'unknown', responseBodySize: parseInt(reply.getHeader?.('content-length') as string) || 'unknown', }), },