Skip to content

Commit 3b19400

Browse files
authored
fix: NODE_ENV envvar for 'environment' was broken in v4.2.0 (#3815)
Fixes: #3807
1 parent ab89a68 commit 3b19400

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

Diff for: CHANGELOG.asciidoc

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ See the <<upgrade-to-v4>> guide.
4545
[float]
4646
===== Bug fixes
4747
48+
* Fix bug where `NODE_ENV` environment value was not used as a default for
49+
the <<environment>> config setting. The bug was introduced in v4.2.0.
50+
({issues}3807[#3807])
51+
4852
* Improve Fastify instrumentation to no longer cause the https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP017[`FSTDEP017`]
4953
and https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP018[`FSTDEP018`]
5054
deprecation warnings. ({pull}3814[#3814])

Diff for: lib/apm-client/http-apm-client/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ See also the "Cloud & Extra Metadata" section below.
5656
specific framework, use this config option to log its version
5757
- `configuredHostname` - A user-configured hostname, if any, e.g. from the `ELASTIC_APM_HOSTNAME` envvar.
5858
See <https://github.com/elastic/apm/blob/main/specs/agents/metadata.md#hostname>.
59-
- `environment` - Environment name (default: `process.env.NODE_ENV || 'development'`)
59+
- `environment` - Environment name (e.g. 'development', 'production')
6060
- `containerId` - Docker container id, if not given will be parsed from `/proc/self/cgroup`
6161
- `kubernetesNodeName` - Kubernetes node name
6262
- `kubernetesNamespace` - Kubernetes namespace

Diff for: lib/apm-client/http-apm-client/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ Client.prototype.config = function (opts) {
252252
if (!this._conf.serverTimeout && this._conf.serverTimeout !== 0)
253253
this._conf.serverTimeout = 15000;
254254
if (!this._conf.serverUrl) this._conf.serverUrl = 'http://127.0.0.1:8200';
255-
if (!this._conf.environment)
256-
this._conf.environment = process.env.NODE_ENV || 'development';
257255
if (!this._conf.truncateKeywordsAt) this._conf.truncateKeywordsAt = 1024;
258256
if (!this._conf.truncateStringsAt) this._conf.truncateStringsAt = 1024;
259257
if (!this._conf.truncateCustomKeysAt) this._conf.truncateCustomKeysAt = 1024;

Diff for: lib/config/schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ const CONFIG_SCHEMA = [
188188
{
189189
name: 'environment',
190190
configType: 'string',
191-
defaultValue: 'development',
191+
defaultValue: process.env.NODE_ENV || 'development',
192192
envVar: 'ELASTIC_APM_ENVIRONMENT',
193193
},
194194
{

Diff for: test/apm-client/http-apm-client/lib/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ function validOpts(opts) {
230230
agentVersion: 'my-agent-version',
231231
serviceName: 'my-service-name',
232232
userAgent: 'my-user-agent',
233+
environment: 'development',
233234
},
234235
opts,
235236
);

Diff for: test/config/config.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,24 @@ const testFixtures = [
395395
);
396396
},
397397
},
398+
{
399+
name: 'use agent - NODE_ENV envvar sets "environment"',
400+
script: 'fixtures/use-agent.js',
401+
cwd: __dirname,
402+
noConvenienceConfig: true,
403+
env: {
404+
NODE_ENV: 'this-is-from-node-env',
405+
TEST_APM_START_OPTIONS: JSON.stringify({
406+
configFile: 'fixtures/use-agent-config.js',
407+
}),
408+
},
409+
checkScriptResult: (t, err, stdout) => {
410+
t.error(err, `use-agent.js script succeeded: err=${err}`);
411+
const useAgentLogs = getUseAgentLogs(stdout);
412+
const resolvedConfig = JSON.parse(useAgentLogs[2]);
413+
t.equal(resolvedConfig.environment, 'this-is-from-node-env');
414+
},
415+
},
398416
{
399417
name: 'use agent - should support key/value pairs formats (string & object)',
400418
script: 'fixtures/use-agent.js',

0 commit comments

Comments
 (0)