From ed87f150d1bc860712f467def1cc341322273d36 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Tue, 12 Nov 2024 11:47:44 +0100 Subject: [PATCH] chore: more details in event-observer http server logging (#2160) * fix: more details in event-observer http server logging * chore: misc cleanup --- src/event-stream/event-server.ts | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/event-stream/event-server.ts b/src/event-stream/event-server.ts index 2a2154b73..64fbf4f45 100644 --- a/src/event-stream/event-server.ts +++ b/src/event-stream/event-server.ts @@ -757,10 +757,32 @@ 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?.remotePort, + 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 +790,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);