Skip to content

Commit

Permalink
feat(logging): Limit full body logging to first 10 slow requests per day
Browse files Browse the repository at this point in the history
  • Loading branch information
gvelez17 committed Apr 24, 2024
1 parent ffe141c commit 759234d
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/cli/src/daemon/log-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

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
})
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 759234d

Please sign in to comment.