diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 4fa6f675390..c1762a1da63 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -29,6 +29,8 @@ All notable changes to experimental packages in this project will be documented ### :house: (Internal) +* refactor(otlp-exporter-base): remove unnecessary isNaN() checks [#5374](https://github.com/open-telemetry/opentelemetry-js/pull/5374) @cjihrig + ## 0.57.0 ### :rocket: (Enhancement) diff --git a/experimental/packages/otlp-exporter-base/src/configuration/shared-configuration.ts b/experimental/packages/otlp-exporter-base/src/configuration/shared-configuration.ts index d9d589e3699..c771cc07678 100644 --- a/experimental/packages/otlp-exporter-base/src/configuration/shared-configuration.ts +++ b/experimental/packages/otlp-exporter-base/src/configuration/shared-configuration.ts @@ -29,11 +29,7 @@ export interface OtlpSharedConfiguration { } export function validateTimeoutMillis(timeoutMillis: number) { - if ( - !Number.isNaN(timeoutMillis) && - Number.isFinite(timeoutMillis) && - timeoutMillis > 0 - ) { + if (Number.isFinite(timeoutMillis) && timeoutMillis > 0) { return timeoutMillis; } throw new Error( diff --git a/experimental/packages/otlp-exporter-base/src/configuration/shared-env-configuration.ts b/experimental/packages/otlp-exporter-base/src/configuration/shared-env-configuration.ts index 767ec05f4a8..d91b4417b57 100644 --- a/experimental/packages/otlp-exporter-base/src/configuration/shared-env-configuration.ts +++ b/experimental/packages/otlp-exporter-base/src/configuration/shared-env-configuration.ts @@ -22,11 +22,7 @@ function parseAndValidateTimeoutFromEnv( const envTimeout = process.env[timeoutEnvVar]?.trim(); if (envTimeout != null && envTimeout !== '') { const definedTimeout = Number(envTimeout); - if ( - !Number.isNaN(definedTimeout) && - Number.isFinite(definedTimeout) && - definedTimeout > 0 - ) { + if (Number.isFinite(definedTimeout) && definedTimeout > 0) { return definedTimeout; } diag.warn( diff --git a/experimental/packages/otlp-exporter-base/test/common/configuration/shared-configuration.test.ts b/experimental/packages/otlp-exporter-base/test/common/configuration/shared-configuration.test.ts index 413ec5e5e6a..b2439c01f06 100644 --- a/experimental/packages/otlp-exporter-base/test/common/configuration/shared-configuration.test.ts +++ b/experimental/packages/otlp-exporter-base/test/common/configuration/shared-configuration.test.ts @@ -19,6 +19,7 @@ import { mergeOtlpSharedConfigurationWithDefaults, OtlpSharedConfiguration, } from '../../../src'; +import { validateTimeoutMillis } from '../../../src/configuration/shared-configuration'; export function testSharedConfigBehavior( sut: ( @@ -134,3 +135,15 @@ describe('mergeOtlpSharedConfigurationWithDefaults', function () { concurrencyLimit: 2, }); }); + +describe('validateTimeoutMillis', function () { + it('throws if timeout is not a positive number', () => { + const values = [null, '1', true, NaN, Infinity, -Infinity, -1, 0]; + + for (let i = 0; i < values.length; ++i) { + assert.throws(() => { + validateTimeoutMillis(values[i] as any); + }, /Configuration: timeoutMillis is invalid/); + } + }); +});