newrelic-experimental / newrelic-nestjs-integration

Sample dockerized NestJs framework service integration with New Relic Node.js Agent showing how to instrument the app in order to use all the Agent’s features (like Service Maps or Distributed Tracing)
26 stars 20 forks source link

NewrelicInterceptor only in prod #7

Closed Yurichz closed 1 year ago

Yurichz commented 1 year ago

I need collect data only in production, and if i try something like this:

  if (process.env.NODE_ENV === 'production') {
    app.useGlobalInterceptors(
      new TransformInterceptor(),
      new NewrelicInterceptor(),
    );
  } else {
    app.useGlobalInterceptors(new TransformInterceptor());
  }

If i'll be in development (NODE_ENV=development) new relic collects information anyway, why? If i delete this interceptor he don't collect them.

matewilk commented 1 year ago

Hi @Yurichz ,

Thanks for the question.

As mentioned in the README.md, it is not advised to use the interceptor anymore. (When I have a bit more time I'll remove the interceptor from the repo).

Now the heavy lifting is done by the New Relic Node Agent, so as long as the newrelic npm package is installed and the configuration file is present (with correct parameters), the data is going to be sent to New Relic.

Instead of creating conditional statements in your project, I'd advise using either:

  1. different config file values for different environments
  2. different environment variables.

If you decide to go for option 1. In your newrelic.ts config file you can use the agent_enabled property and set it accordingly depending on the environment you run your project in.

Alternatively - option 2 - instead of using the config file, you could use environment variables in your environments The one that is able to control whether to send data to NR or not is NEW_RELIC_ENABLED

Hope it helps.

Yurichz commented 1 year ago

Ty) I have one more method, if some-one need:

  if (process.env.NODE_ENV === 'production') {
    // eslint-disable-next-line global-require
    const newrelic = require('newrelic');

    newrelic.instrumentLoadedModule(
      'app',
      app,
    );
  }

Try to write: (const newrelic = require('newrelic')) only in condition, if you try to write upper, new relic will always send data, no matter if you write: ( newrelic.instrumentLoadedModule( 'app', app, ); ) In condition.