-
Notifications
You must be signed in to change notification settings - Fork 127
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
log full body only on slow requests and only 10 per day #3215
Draft
gvelez17
wants to merge
1
commit into
log-req-body-core-189
Choose a base branch
from
feat/log-slow-requests-daily
base: log-req-body-core-189
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,38 @@ import { LoggerProvider } from '@ceramicnetwork/common' | |
import { Request, Response } from 'express' | ||
import morgan from 'morgan' | ||
|
||
const EXPECTED_RESPONSE_TIME_MS = 2000 | ||
const MAX_DAILY_SLOW_REQUESTS_TO_LOG = 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about only logging one slow query per minute instead? I think that will still be infrequent enough to not flood the logs, while making the slow queries a lot more visible if they are happening often. |
||
|
||
const ACCESS_LOG_FMT = | ||
'ip=:remote-addr ts=:date[iso] method=:method original_url=:original-url base_url=:base-url path=:path:params :body http_version=:http-version req_header:req[header] status=:status content_length=:res[content-length] content_type=":res[content-type]" ref=:referrer user_agent=":user-agent" elapsed_ms=:total-time[3] error_message=":error-message" error_code=:error-code' | ||
|
||
export function logRequests(loggerProvider: LoggerProvider): any[] { | ||
|
||
// these trackers and middleware enable us to log just a sample of slow requests each day or app restart | ||
let slowRequestCount = 0; | ||
let lastLoggedDate = new Date().toISOString().slice(0, 10); | ||
|
||
// Middleware to calculate response time and handle daily reset of the counter | ||
const responseTimeMiddleware = (req: Request, res: Response, next: Function) => { | ||
const start = Date.now(); | ||
const currentDate = new Date().toISOString().slice(0, 10); | ||
|
||
if (lastLoggedDate !== currentDate) { | ||
slowRequestCount = 0; // Reset counter if it's a new day | ||
lastLoggedDate = currentDate; // Update the last logged date | ||
} | ||
|
||
res.on('finish', () => { | ||
const responseTime = Date.now() - start; | ||
if (responseTime > EXPECTED_RESPONSE_TIME_MS && slowRequestCount < MAX_DAILY_SLOW_REQUESTS_TO_LOG) { | ||
slowRequestCount++; | ||
res.locals.logSlowRequest = true; // Flag to log this request | ||
} | ||
}); | ||
next(); | ||
}; | ||
|
||
morgan.token<Request, Response>('error-message', (req, res: Response) => { | ||
return res.locals.error?.message | ||
}) | ||
|
@@ -33,8 +61,8 @@ export function logRequests(loggerProvider: LoggerProvider): any[] { | |
} | ||
return ' params=-' | ||
}) | ||
morgan.token<Request, Response>('body', (req) => { | ||
if (req.body) { | ||
morgan.token<Request, Response>('body', (req, res: Response) => { | ||
if (req.body && res.locals.logSlowRequest) { | ||
const keys = Object.keys(req.body) | ||
if (keys.length > 0) { | ||
const body = keys.reduce((prev, curr) => { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the default threshold for what's considered a "slow" request should probably be higher. Maybe 15 seconds?
Also might be nice to make this threshold configurable.