Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: more details in event-observer http server logging #2160

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading