@pmc12thsuki/newrelic
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@sl-nestjs-modules/newrelic

newrelic module for NestJS project

Installation

yarn add @sl-nestjs-modules/newrelic

Usage

Setup newrelic agent and configuration

Registering module

Import NewrelicModule into the root AppModule and use the register() method to configure it. This method accepts an optional NewrelicOptions.

NewrelicOptions

Parameter Description
global Config if newrelic module is register as global module. Default is true.

Send web transaction to newrelic

Config NewrelicInterceptor as global interceptor. This interceptor will send web transactions to newrelic using startWebTransaction method, and send errors to newrelic by calling noticeError if error occurs.

// app.module.ts
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { NewrelicModule, NewrelicInterceptor } from '@sl-nestjs-modules/newrelic';

@Module({
  imports: [
    NewrelicModule.register({
      // options
    }),
  ],
  providers: [
     {
      provide: APP_INTERCEPTOR,
      useClass: NewrelicInterceptor,
    },
  ],
})
export class AppModule {}

Send background transaction to newrelic

Add StartBackgroundTransaction decorator to your background job (queue-job, cron-job, etc.) and it will send transaction to newrelic using startBackgroundTransaction method.

StartBackgroundTransaction accept handlerName as argument. The handlerName defines the transaction name and needs to be static. Do not include variable data such as user ID.

// my-cron-job.ts
import { StartBackgroundTransaction } from '@sl-nestjs-modules/newrelic';

export class MyCronJob {

  @StartBackgroundTransaction('my-cron-job')
  async function handler(){
    // job logic
  }

}

Calling newrelic APIs

You can call newrelic agent API by injecting newrelic agent into your services.

For example, you may call addCustomAttributes to set multiple custom attribute values to appear in the transaction trace detail view.

import { Inject, Injectable } from '@nestjs/common';
import { NEWRELIC } from '@sl-nestjs-modules/newrelic';

@Injectable()
export class MyService {
  constructor(@Inject(NEWRELIC) private newrelicAgent) {}

  async doSomething(arg1, arg2, ...args) {
    try {
      this.newrelicAgent.addCustomAttributes({
        myCustomAttribute1: arg1,
        myCustomAttribute2: arg2,
      });
      // your function logic
      return 'success';
    } catch (error) {
      this.newrelicAgent.noticeError(error, {
        myCustomAttribute1: arg1,
        myCustomAttribute2: arg2,
      });
    }
  }
}

For local development

You may set NEW_RELIC_ENABLED to false in local development to stop the newrelic agent from starting up.

// package.json
{
 "scripts": {
    "start": "nest start",
    "start:dev": "NEW_RELIC_ENABLED=false nest start --watch",
  }
}

For testing

You may manual mock this module by copying __mocks__ directory to your src folder. By doing so, newrelic agent will not be imported when running unit test. For advanced mocking, please refer to Jest mocking guide.

Readme

Keywords

none

Package Sidebar

Install

npm i @pmc12thsuki/newrelic

Weekly Downloads

19

Version

1.0.0

License

MIT

Unpacked Size

20.5 kB

Total Files

20

Last publish

Collaborators

  • pmc12thsuki