API-57 Fixing cloudwatch to not have additional noise#232
Merged
Conversation
There was a problem hiding this comment.
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.localsfor later logging. - Updated the JSON access log
bytesfield to fall back to the custom byte count whenContent-Lengthis missing. - Updated health check skip logic to use
req.originalUrlfor more stable matching.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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 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 on lines
+106
to
+109
| const skipHealthChecks = (req) => { | ||
| const p = (req.originalUrl || req.url).split('?')[0]; | ||
| return p === '/healthcheck' || p === '/healthcheck/'; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-Lengthheader 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:
Content-Lengthis unavailable. (app.js)bytesWrittenvalue whenContent-Lengthis missing, improving the accuracy of logged data volume. (app.js)Health Check Logging:
req.originalUrl, ensuring health check routes are properly excluded regardless of Express routing behavior. (app.js)