newrelic-experimental / newrelic-nextjs-integration

NewRelic Browser and APM agent NextJs integration (front and back-end respectively)
Apache License 2.0
27 stars 22 forks source link

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

Open ghost opened 10 months ago

ghost commented 10 months ago

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

System Information

alialjunied commented 8 months ago

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 commented 6 months ago

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 commented 5 months ago

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

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