Coder Social home page Coder Social logo

nestjs-context-tracer's Introduction

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');

Do not use 'traceId' value as key name. Because 'traceId' value is used for this library TraceID value.

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 nestjs/nest#1407

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.