0.0.2 • Published 3 years ago

nestjs-newrelic v0.0.2

Weekly downloads
22
License
MIT
Repository
github
Last release
3 years ago

nestjs-newrelic

NestJS Newrelic APM library.

Installation

npm i nestjs-newrelic --save

or

yarn add nestjs-newrelic

Usage

import newrelic = require('newrelic'); // required by newrelic
import { ApmErrorInterceptor, ApmHttpInterceptor } from 'nestjs-newrelic';
import { NestFactory } from '@nestjs/core';

import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.useGlobalInterceptors(app.get(ApmErrorInterceptor), app.get(ApmHttpInterceptor));

  await app.listen(3000);
}
bootstrap();

As NestJS is not allowing you to use some sort of ConfigService there you need to add to your repository dotenv package or something similar to pass configuration.

Adding ApmModule

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    ApmModule.forRootAsync({
      useFactory: async () => {
        return {
          httpUserMapFunction: (req: any) => {
            return {
              id: req.user?.id,
              username: req.user?.username,
              email: req.user?.email,
            };
          },
        };
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Default ApmHttpInterceptor behavior

It won't set UserContext in transaction if httpUserMapFunction is not provided This method is starting the web transaction and ends it once response is being send.

ToDo

  • [] Inject current transaction via decorator
  • [] Add tests