0.0.6 • Published 6 months ago

@soorajk1/test-monitor v0.0.6

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
6 months ago

Open in Gitpod

An opinionated nestjs monitoring setup with Prometheus and Grafana. This project aims to create a NestJS interceptor to monitor the response times of different APIs in your NestJS app. Along with support for dynamic creation of Grafana Panels for each instance of the interceptor used in your app.

Usage

The usage of this interceptor needs you to have a the following services running: 1. Redis 2. Prometheus 3. Grafana

A sample docker-compose.yml is present in the monitoring/ folder for your reference.

At a method/endpoint level

@Get()
  @UseInterceptors(new ResponseTimeInterceptor('monitoring_response_time', 'path/to/your/grafana/config/folder')) // <<-- focus on this line
  getMonitoringHell(): string {
    return 'Hello from monitoring controller!';
  }

At a class/controller level

Add the following code in your <NAME>.controller.ts file.

@Controller()
@UseInterceptors(new ResponseTimeInterceptor('class_response_time', 'path/to/your/grafana/config/folder'))// <<-- focus on this line
export class AppController {
  constructor(
    private readonly appService: AppService,
    private readonly monitoringService: MonitoringService,
  ) {}

  @Get()
  getHello(): string {
    this.monitoringService.incrementRequestCounter();
    return this.appService.getHello();
  }

  @Get('/route')
  async randomRoute(@Query('delay') delay: number): Promise<string> {
    await new Promise((resolve) => setTimeout(resolve, delay));
    return 'Hello from random route!';
  }
}

At global level

Add the following code in your main.ts file for nestJS.

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );

  app.useGlobalInterceptors(new ResponseTimeInterceptor('global_interceptor', 'path/to/your/grafana/config/folder')); //<<-- focus on this line
}