Skip to content

API-57 Fixing cloudwatch to not have additional noise#232

Merged
sedv8808 merged 1 commit into
developfrom
logs_apprunner
Jul 16, 2026
Merged

API-57 Fixing cloudwatch to not have additional noise#232
sedv8808 merged 1 commit into
developfrom
logs_apprunner

Conversation

@sedv8808

Copy link
Copy Markdown
Collaborator

This pull request improves how response body size is tracked and logged, especially for gzipped (compressed) responses. It introduces custom middleware to accurately count and log the number of bytes sent in each response, ensuring data volume reporting remains correct even when the Content-Length header is missing or altered by compression. It also refines how health check requests are skipped in logging to handle Express router behavior more reliably.

Logging and Metrics Improvements:

  • Added custom middleware to count the number of bytes written to each response, ensuring accurate logging of response sizes even when compression is used and Content-Length is unavailable. (app.js)
  • Updated the access log format to use the custom bytesWritten value when Content-Length is missing, improving the accuracy of logged data volume. (app.js)

Health Check Logging:

  • Improved the logic for skipping health check requests in logging by using req.originalUrl, ensuring health check routes are properly excluded regardless of Express routing behavior. (app.js)

Copilot AI review requested due to automatic review settings July 16, 2026 18:50
@sedv8808
sedv8808 merged commit 1f1ff8c into develop Jul 16, 2026
2 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts request logging/metrics in app.js to improve byte-size reporting for responses (especially when compression affects Content-Length) and to reduce CloudWatch noise by skipping health check requests more reliably.

Changes:

  • Added middleware that counts response body bytes and stores the count on res.locals for later logging.
  • Updated the JSON access log bytes field to fall back to the custom byte count when Content-Length is missing.
  • Updated health check skip logic to use req.originalUrl for more stable matching.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app.js
Comment on lines +75 to +79
// compression() strips Content-Length, so morgan's :res[content-length] token is
// empty for gzipped responses (i.e. almost all real traffic). Count the response
// body bytes ourselves so the "data volume" reporting keeps working. Registered
// after compression() so it counts the uncompressed body the app produced, which
// matches Content-Length when that header is present.
Comment thread app.js
Comment on lines +103 to +105
// Use originalUrl (not req.url): the health check is a mounted router
// (app.use('/healthcheck/', ...)), and Express rewrites req.url to '/' inside it,
// so req.url no longer reads '/healthcheck' by the time morgan evaluates skip.
Comment thread app.js
Comment on lines +106 to +109
const skipHealthChecks = (req) => {
const p = (req.originalUrl || req.url).split('?')[0];
return p === '/healthcheck' || p === '/healthcheck/';
};
@sedv8808
sedv8808 deleted the logs_apprunner branch July 16, 2026 19:36
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.

2 participants