From 576801e1f47bff6e9df1145a31ec7c3d594691d8 Mon Sep 17 00:00:00 2001 From: DavisVT Date: Mon, 22 Jun 2026 13:10:42 +0100 Subject: [PATCH] Improve debugging with unique correlation IDs per request --- listener/src/api/events-server.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/listener/src/api/events-server.ts b/listener/src/api/events-server.ts index 76fdb65..e8271ba 100644 --- a/listener/src/api/events-server.ts +++ b/listener/src/api/events-server.ts @@ -6,9 +6,8 @@ import { PreferencesUpdateInput } from '../types/preferences'; import { NotificationAPI } from '../services/notification-api'; import { NotificationType } from '../types/scheduled-notification'; import logger from '../utils/logger'; -import { generateRequestId } from '../utils/request-id'; -import { NotificationHistoryService } from '../services/notification-history'; import { generateRequestId, resolveCorrelationId } from '../utils/request-id'; +import { NotificationHistoryService } from '../services/notification-history'; import { verifySignature, extractSignature, @@ -411,6 +410,7 @@ export function createEventsServer(options: EventsServerOptions): http.Server { logger.info('Handling GET /api/notifications/history', { requestId, + correlationId, limit, offset, status, @@ -431,13 +431,14 @@ export function createEventsServer(options: EventsServerOptions): http.Server { logger.info('GET /api/notifications/history complete', { requestId, + correlationId, returned: result.records.length, total: result.total, durationMs: Date.now() - startTime, }); }) .catch((error) => { - logger.error('Failed to retrieve notification history', { error, requestId }); + logger.error('Failed to retrieve notification history', { error, requestId, correlationId }); res.writeHead(500, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: (error as Error).message })); }); @@ -453,7 +454,9 @@ export function createEventsServer(options: EventsServerOptions): http.Server { const getPrefsMatch = url.pathname.match(/^\/api\/preferences\/([^/]+)$/); if (req.method === 'GET' && getPrefsMatch) { const userId = decodeURIComponent(getPrefsMatch[1]); + logger.info('Handling GET /api/preferences/:userId', { requestId, correlationId, userId }); const prefs = preferenceStore.get(userId); + logger.info('GET /api/preferences/:userId complete', { requestId, correlationId, userId, durationMs: Date.now() - startTime }); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(prefs)); return; @@ -463,20 +466,24 @@ export function createEventsServer(options: EventsServerOptions): http.Server { const putPrefsMatch = url.pathname.match(/^\/api\/preferences\/([^/]+)$/); if (req.method === 'PUT' && putPrefsMatch) { const userId = decodeURIComponent(putPrefsMatch[1]); + logger.info('Handling PUT /api/preferences/:userId', { requestId, correlationId, userId }); let body = ''; req.on('data', (chunk) => { body += chunk; }); req.on('end', () => { try { const input: PreferencesUpdateInput = JSON.parse(body); if (!input || typeof input.categories !== 'object') { + logger.warn('PUT /api/preferences/:userId invalid body', { requestId, correlationId, userId }); res.writeHead(400, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Invalid body: expected { categories: { [key]: boolean } }' })); return; } const updated = preferenceStore.update(userId, input); + logger.info('PUT /api/preferences/:userId complete', { requestId, correlationId, userId, durationMs: Date.now() - startTime }); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(updated)); } catch { + logger.error('PUT /api/preferences/:userId invalid JSON', { requestId, correlationId, userId }); res.writeHead(400, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Invalid JSON' })); }