Skip to content

refactor(otlp-exporter-base): use get*FromEnv() for otlp exporter config #5583

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
### :house: Internal

* chore(sdk-node): Refactored using `get*FromEnv` utility function instead of `process.env` for NodeSDK's logging setup. [#5563](https://github.com/open-telemetry/opentelemetry-js/issues/5563) @weyert
* refactor: use get*FromEnv() for otlp exporter config. [#5583](https://github.com/open-telemetry/opentelemetry-js/issues/5583) @weyert

## 0.200.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getNumberFromEnv, getStringFromEnv } from '@opentelemetry/core';
import { OtlpSharedConfiguration } from './shared-configuration';
import { diag } from '@opentelemetry/api';

function parseAndValidateTimeoutFromEnv(
timeoutEnvVar: string
): number | undefined {
const envTimeout = process.env[timeoutEnvVar]?.trim();
if (envTimeout != null && envTimeout !== '') {
const definedTimeout = Number(envTimeout);
if (Number.isFinite(definedTimeout) && definedTimeout > 0) {
return definedTimeout;
const envTimeout = getNumberFromEnv(timeoutEnvVar);
if (envTimeout) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will accidentally not go through this if-statement if the envvar value is '0':

> process.env.FOO
'0'
> core.getNumberFromEnv('FOO')
0
> delete process.env.FOO
true
> process.env.FOO
undefined
> core.getNumberFromEnv('FOO')
undefined

The side-effect will be that OTEL_EXPORTER_OTLP_METRICS_TIMEOUT=0 will not result in the diag.warn(...) message.

Suggested change
if (envTimeout) {
if (envTimeout !== undefined) {

if (Number.isFinite(envTimeout) && envTimeout > 0) {
return envTimeout;
}
diag.warn(
`Configuration: ${timeoutEnvVar} is invalid, expected number greater than 0 (actual: ${envTimeout})`
);
}

return undefined;
}

Expand All @@ -46,7 +47,7 @@ function getTimeoutFromEnv(signalIdentifier: string) {
function parseAndValidateCompressionFromEnv(
compressionEnvVar: string
): 'none' | 'gzip' | undefined {
const compression = process.env[compressionEnvVar]?.trim();
const compression = getStringFromEnv(compressionEnvVar)?.trim();
if (compression === '') {
return undefined;
}
Comment on lines +50 to 53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified. getStringFromEnv will handle trimming and returning undefined if the envvar is an empty string.

Suggested change
const compression = getStringFromEnv(compressionEnvVar)?.trim();
if (compression === '') {
return undefined;
}
const compression = getStringFromEnv(compressionEnvVar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ export function testSharedConfigurationFromEnvironment(
sinon.assert.calledTwice(spyLoggerWarn);
sinon.assert.calledWithExactly(
spyLoggerWarn,
'Configuration: OTEL_EXPORTER_OTLP_METRICS_TIMEOUT is invalid, expected number greater than 0 (actual: NaN)'
`Unknown value 'NaN' for OTEL_EXPORTER_OTLP_METRICS_TIMEOUT, expected a number, using defaults`
);
sinon.assert.calledWithExactly(
spyLoggerWarn,
'Configuration: OTEL_EXPORTER_OTLP_TIMEOUT is invalid, expected number greater than 0 (actual: foo)'
`Unknown value 'foo' for OTEL_EXPORTER_OTLP_TIMEOUT, expected a number, using defaults`
);
assert.strictEqual(config.timeoutMillis, undefined);
});
Expand All @@ -96,7 +96,7 @@ export function testSharedConfigurationFromEnvironment(
sinon.assert.calledTwice(spyLoggerWarn);
sinon.assert.calledWithExactly(
spyLoggerWarn,
'Configuration: OTEL_EXPORTER_OTLP_METRICS_TIMEOUT is invalid, expected number greater than 0 (actual: -Infinitiy)'
`Unknown value '-Infinitiy' for OTEL_EXPORTER_OTLP_METRICS_TIMEOUT, expected a number, using defaults`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised at this one. I would expect getNumberFromEnv to pass on '-Infinity':

> process.env.FOO = '-Infinity'
'-Infinity'
> core.getNumberFromEnv('FOO')
-Infinity

);
sinon.assert.calledWithExactly(
spyLoggerWarn,
Expand Down