Skip to content

Commit

Permalink
chore: more details in event-observer http server logging (#2160)
Browse files Browse the repository at this point in the history
* fix: more details in event-observer http server logging

* chore: misc cleanup
  • Loading branch information
zone117x authored Nov 12, 2024
1 parent a82583c commit ed87f15
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/event-stream/event-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,17 +757,51 @@ 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,
logger: loggerOpts,
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);

Expand Down

0 comments on commit ed87f15

Please sign in to comment.