npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pmc12thsuki/newrelic

v1.0.0

Published

[newrelic](https://newrelic.com) module for [NestJS](https://docs.nestjs.com/) project

Downloads

68

Readme

@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.