Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Read Environment Variables in newrelic.js file for @newrelic/next agent #19

Open
ghost opened this issue Aug 21, 2023 · 5 comments

Comments

@ghost
Copy link

ghost commented Aug 21, 2023

Description

I'm trying to pull in my license_key into the newrelic.js file, but it won't read any of my environment variables. I get an error stating that:

New Relic for Node.js halted startup due to an error:
Error: Not starting without license key!

my environment variables are stored in a file called .env.local

Here's an example of my newrelic.js file:

"use strict";
/**
* New Relic agent configuration.
*
* See lib/config/default.js in the agent distribution for a more complete
* description of configuration variables and their potential values.
*/
exports.config = {
    /**
     * Array of application names.
     */
    app_name: ["GSF App 2 NextJS Web App"],
    /**
     * Your New Relic license key.
     */
    license_key: process.env.NEW_RELIC_LICENSE_KEY,
    logging: {
        /**
         * Level at which to log. 'trace' is most useful to New Relic when diagnosing
         * issues with the agent, 'info' and higher will impose the least overhead on
         * production applications.
         */
        level: "info",
    },
    /**
     * When true, all request headers except for those listed in attributes.exclude
     * will be captured for all traces, unless otherwise specified in a destination's
     * attributes include/exclude lists.
     */
    allow_all_headers: true,
    attributes: {
        /**
         * Prefix of attributes to exclude from all destinations. Allows * as wildcard
         * at end.
         *
         * NOTE: If excluding headers, they must be in camelCase form to be filtered.
         *
         * @name NEW_RELIC_ATTRIBUTES_EXCLUDE
         */
        exclude: [
            "request.headers.cookie",
            "request.headers.authorization",
            "request.headers.proxyAuthorization",
            "request.headers.setCookie*",
            "request.headers.x*",
            "response.headers.cookie",
            "response.headers.authorization",
            "response.headers.proxyAuthorization",
            "response.headers.setCookie*",
            "response.headers.x*",
        ],
    },
};

What I've tried to do

  • Use process.env.NEW_RELIC_LICENSE_KEY
  • Use require require("dotenv").config();, this then breaks the app
  • It does work when I just directly enter the license key into the newrelic.js file. But obviously, for security reasons, I don't want to do this.

System Information

  • OS Name: Microsoft Windows 10 Pro
  • Version: 10.0.19045 Build 19045
  • Node Version: 18.17.0
@alialjunied
Copy link

alialjunied commented Oct 18, 2023

You need to "force" nextjs to read the env variables from your file (i.e. .env.local)

use 'strict'

import { loadEnvConfig } from '@next/env'
loadEnvConfig(process.cwd())

/**
* New Relic agent configuration.
*
* See lib/config/default.js in the agent distribution for a more complete
* description of configuration variables and their potential values.
*/
exports.config = {
    /**
     * Array of application names.
     */
    app_name: ["GSF App 2 NextJS Web App"],
    /**
        /**
     * Your New Relic license key.
     */
    license_key: process.env.NEW_RELIC_LICENSE_KEY,
    // ...
}

@7i4g0
Copy link

7i4g0 commented Jan 4, 2024

When I try use:

'use strict'

import { loadEnvConfig } from '@next/env'
loadEnvConfig(process.cwd())

exports.config = {
  app_name: process.env.NEW_RELIC_APP_NAME,
  license_key: process.env.NEW_RELIC_LICENSE_KEY,
  distributed_tracing: {
    enabled: true,
  },
  ...

returns this error message:

(/home/node/node_modules/@newrelic/next/index.js:13:18)
    2024-01-03T07:01:59.238-03:00    New Relic for Node.js was unable to bootstrap itself due to an error:
    2024-01-03T07:01:59.243-03:00    Error: New Relic requires that you name this application!
    2024-01-03T07:01:59.243-03:00    Set app_name in your newrelic.js or newrelic.cjs file or set environment variable
    2024-01-03T07:01:59.243-03:00    NEW_RELIC_APP_NAME. Not starting!
    2024-01-03T07:01:59.243-03:00    at createAgent (/home/node/node_modules/newrelic/index.js:160:11)
    2024-01-03T07:01:59.243-03:00    at initialize (/home/node/node_modules/newrelic/index.js:105:15)
    2024-01-03T07:01:59.243-03:00    at Object.<anonymous> (/home/node/node_modules/newrelic/index.js:39:3)
    2024-01-03T07:01:59.243-03:00    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    2024-01-03T07:01:59.243-03:00    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    2024-01-03T07:01:59.243-03:00    at Module.load (node:internal/modules/cjs/loader:1119:32)
    2024-01-03T07:01:59.243-03:00    at Module._load (node:internal/modules/cjs/loader:960:12)
    2024-01-03T07:01:59.243-03:00    at Module.require (node:internal/modules/cjs/loader:1143:19)
    2024-01-03T07:01:59.243-03:00    at require (node:internal/modules/cjs/helpers:119:18)
    2024-01-03T07:01:59.243-03:00    at Object.<anonymous> (/home/node/node_modules/@newrelic/next/index.js:13:18)

My .env.development file looks like this:

NEW_RELIC_APP_NAME='my-next-application'
NEW_RELIC_LICENSE_KEY='my_key'

@YutaMoriJP
Copy link

YutaMoriJP commented Jan 24, 2024

If your setup is like mine and you use dotenv, then the following did the trick for me:

// newrelic.js
require('./src/configs/env');

@matewilk
Copy link
Contributor

matewilk commented Jul 11, 2024

When using dotenv or .env file please ADD

NEW_RELIC_APP_NAME='my-next-application'
NEW_RELIC_LICENSE_KEY='my_key'

to your .env file

and REMOVE

app_name
license_key

from your newrelic.js file.

This will force the New Relic node agent to read those variables from .env file.

That's it, no need to "force" reading env vars.

Hope this helps

@SimoSavonenVincit
Copy link

@matewilk is this method still valid? I'm not seeing the node agent reading .env without adding NODE_OPTIONS='-r dotenv/config -r newrelic'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants