Skip to content

Commit 466cebc

Browse files
committed
fix(core): use false as default for getBooleanFromEnv()
1 parent bd5ca62 commit 466cebc

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

packages/opentelemetry-core/src/platform/node/environment.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export function getEnv(): Required<ENVIRONMENT> {
3333

3434
/**
3535
* Retrieves a number from an environment variable.
36-
* - Trims leading and trailing whitespace.
3736
* - Returns `undefined` if the environment variable is empty, unset, contains only whitespace, or is not a number.
3837
* - Returns a number in all other cases.
3938
*
@@ -75,26 +74,29 @@ export function getStringFromEnv(key: string): string | undefined {
7574
/**
7675
* Retrieves a boolean value from an environment variable.
7776
* - Trims leading and trailing whitespace and ignores casing.
78-
* - Returns `undefined` if the environment variable is empty, unset, or contains only whitespace.
79-
* - Returns `undefined` for strings that cannot be mapped to a boolean.
77+
* - Returns `false` if the environment variable is empty, unset, or contains only whitespace.
78+
* - Returns `false` for strings that cannot be mapped to a boolean.
8079
*
8180
* @param {string} key - The name of the environment variable to retrieve.
82-
* @returns {boolean | undefined} - The boolean value or `undefined`.
81+
* @returns {boolean} - The boolean value or `false` if the environment variable is unset empty, unset, or contains only whitespace.
8382
*/
84-
export function getBooleanFromEnv(key: string): boolean | undefined {
83+
export function getBooleanFromEnv(key: string): boolean {
8584
const raw = process.env[key]?.trim().toLowerCase();
8685
if (raw == null || raw === '') {
87-
return undefined;
86+
// NOTE: falling back to `false` instead of `undefined` as required by the specification.
87+
// If you have a use-case that requires `undefined`, consider using `getStringFromEnv()` and applying the necessary
88+
// normalizations in the consuming code.
89+
return false;
8890
}
8991
if (raw === 'true') {
9092
return true;
9193
} else if (raw === 'false') {
9294
return false;
9395
} else {
9496
diag.warn(
95-
`Unknown value ${inspect(raw)} for ${key}, expected 'true' or 'false', using defaults`
97+
`Unknown value ${inspect(raw)} for ${key}, expected 'true' or 'false', falling back to 'false' (default)`
9698
);
97-
return undefined;
99+
return false;
98100
}
99101
}
100102

packages/opentelemetry-core/test/node/environment.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,17 @@ describe('environment utility functions', function () {
166166
sinon.restore();
167167
});
168168

169-
it('should treat empty string as undefined', function () {
169+
it('should treat empty string as false', function () {
170170
process.env.FOO = '';
171-
assert.strictEqual(getBooleanFromEnv('FOO'), undefined);
171+
assert.strictEqual(getBooleanFromEnv('FOO'), false);
172172

173173
process.env.FOO = ' ';
174-
assert.strictEqual(getBooleanFromEnv('FOO'), undefined);
174+
assert.strictEqual(getBooleanFromEnv('FOO'), false);
175175
});
176176

177-
it('should treat not defined as undefined', function () {
177+
it('should treat not defined as false', function () {
178178
delete process.env.FOO;
179-
assert.strictEqual(getBooleanFromEnv('FOO'), undefined);
179+
assert.strictEqual(getBooleanFromEnv('FOO'), false);
180180
});
181181

182182
it('should trim extra whitespace', function () {
@@ -200,7 +200,7 @@ describe('environment utility functions', function () {
200200
it('should ignore bogus data and warn', function () {
201201
const warnStub = sinon.stub(diag, 'warn');
202202
process.env.FOO = 'trueFALSE';
203-
assert.strictEqual(getBooleanFromEnv('FOO'), undefined);
203+
assert.strictEqual(getBooleanFromEnv('FOO'), false);
204204
sinon.assert.calledOnceWithMatch(warnStub, 'Unknown value');
205205
});
206206
});

0 commit comments

Comments
 (0)