nestjs-context-tracer
v0.3.0
Published
trace http request
Downloads
19
Readme
nestjs-context-tracer
Trace each http context of your nest.js application. This enable to trace http request by unique id. Each request has unique Id and You can get id on controller, service, dao, anywhere.
Usage
Configuration
Add module and set middleware
- configure middleware for every http method and path
example
- app.module.ts
import { AsyncTraceIdMiddleware, AsyncTraceModule } from 'nestjs-context-tracer';
@Module({
imports: [ AsyncTraceModule.forRoot() ],
controllers: [AppController],
...
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(AsyncTraceIdMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL });
}
}
Get Trace Id
Use getTraceId method of AsyncContext
asyncContext.getTraceId(): string | null
example
- app.controller.ts
import { AsyncContext } from 'nestjs-context-tracer';
@Controller()
export class AppController {
constructor(
private readonly asyncContext: AsyncContext
) {}
@Get()
getHello(): string {
console.log('begin trace-id:', this.asyncContext.getTraceId());
....
}
You can see unique begin trace-id output by each http request. trace-id is generated by uuid v4.
Set & Get For Custom data
You can set any data and get by this library.
asyncContext.set(key, value): void
Arguments:
- key
string
- value
unknown
example
asyncContext.set('role', 'ADMIN');
asyncContext.get(key): unknown
Arguments:
- key
string
example
const userRole = asyncContext.get('role');
Important Note
Performance Issue
This library based on AsyncLocalStorage AsyncLocalStorage tend to drop application performance. Drop rate is different by node.js version. It recommends to use v16.2 or above version. If you want to see more information see this issue.
Context loss
If your code is based on callback, not promsie, you must read this document.
Do not use 0.2.0 or below
That version has critical bug. some http request share storeage.
This library inspired by https://github.com/nestjs/nest/pull/1407
License
MIT