-
Notifications
You must be signed in to change notification settings - Fork 0
Logging: Add request id to the app logs for easy log tracking #68
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import { Response, NextFunction } from 'express'; | ||
import { RequestMetadata, saveStore } from './requestStore'; | ||
|
||
@Injectable() | ||
export class CreateRequestStoreMiddleware implements NestMiddleware { | ||
constructor() {} | ||
|
||
use(req: any, res: Response, next: NextFunction) { | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const requestMetaData = new RequestMetadata({}); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
saveStore(requestMetaData, next); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { AsyncLocalStorage } from 'async_hooks'; | ||
import { NextFunction } from 'express'; | ||
import { nanoid } from 'nanoid'; | ||
|
||
// Class for storing request specific metadata | ||
export class RequestMetadata { | ||
requestId: string; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-object-type | ||
constructor(params: { requestId?: string }) { | ||
this.requestId = params.requestId ?? nanoid(11); | ||
} | ||
} | ||
|
||
// Create a AsyncLocalStorage of type RequestMetaData for storing request specific data | ||
const asyncStorage = new AsyncLocalStorage<RequestMetadata>(); | ||
|
||
// Gets the RequestMetadada object associated with the current request | ||
export function getStore(): RequestMetadata { | ||
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. high 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. this is happening only for "root"-level logging - and is expected - they are not part of any request. |
||
let store = asyncStorage.getStore(); | ||
if (store === undefined) { | ||
store = new RequestMetadata({ | ||
requestId: '', | ||
}); | ||
} | ||
|
||
return store; | ||
} | ||
|
||
// For use in middleware | ||
// Saves RequestMetadata for the current request | ||
export function saveStore( | ||
requestMetaData: RequestMetadata, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
next: NextFunction, | ||
) { | ||
asyncStorage.run(requestMetaData, () => { | ||
next(); | ||
}); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.