Skip to content

fix(middleware): use parametrised route pattern in request completion logger (#668) - #673

Merged
Chucks1093 merged 1 commit into
accesslayerorg:mainfrom
khaylebfortune:add-structured/log
Jul 27, 2026
Merged

fix(middleware): use parametrised route pattern in request completion logger (#668)#673
Chucks1093 merged 1 commit into
accesslayerorg:mainfrom
khaylebfortune:add-structured/log

Conversation

@khaylebfortune

Copy link
Copy Markdown
Contributor

Summary

Closes #668

The request completion logger middleware was emitting the raw req.path (e.g. /creators/GABC123DEF456) in log entries, creating high-cardinality log keys that make it impossible to aggregate by route. This PR changes the route field to use the parametrised Express route pattern (e.g. /api/v1/creators/:id) via req.baseUrl + req.route.path, enabling route-level request rate and p95 latency calculations from logs.

Changes

  • src/middlewares/request-completion-logger.middleware.ts — Replaced req.path with the parametrised route pattern (req.baseUrl + req.route.path). Falls back to req.baseUrl + req.path when no route matches (e.g. 404s). Renamed the log payload field from path to route.

How It Works

// Before (high-cardinality)
const path = req.path;
// => "/creators/GABC123DEF456"

// After (low-cardinality, parametrised)
const route = req.route
  ? req.baseUrl + req.route.path
  : req.baseUrl + req.path;
// => "/api/v1/creators/:id"

Express sets req.route when a route handler is matched. By the time the finish event fires, req.route is available for all matched routes. For unmatched routes (404s), the fallback preserves the raw path so those requests are still logged.

Log Output Example

{
  "level": "info",
  "request_id": "abc-123",
  "method": "GET",
  "route": "/api/v1/creators/:id",
  "status_code": 200,
  "response_time_ms": 42,
  "msg": "Request completed"
}

Acceptance Criteria

  • Log emitted for every API request after response is sent
  • All four fields present with correct values (method, route, status_code, response_time_ms)
  • Route is the parametrised pattern, not the raw URL
  • response_time_ms reflects full request-to-response time (measured via process.hrtime from middleware entry to finish event)

… logger (accesslayerorg#668)

Replace req.path with req.baseUrl + req.route.path in the request
completion logger so log entries contain the parametrised Express route
pattern (e.g. /api/v1/creators/:id) instead of the raw URL path with
high-cardinality identifiers.

Falls back to req.baseUrl + req.path when no route matches (404s).
@drips-wave

drips-wave Bot commented Jul 27, 2026

Copy link
Copy Markdown

@khaylebfortune Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Chucks1093
Chucks1093 merged commit 423f4e9 into accesslayerorg:main Jul 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add structured log for each API request with method, route, status code, and response time

2 participants