From 3fdadd516e37728833f9abae772ef3b937e66298 Mon Sep 17 00:00:00 2001 From: Daniel Starns Date: Tue, 25 Jun 2024 08:43:19 +0100 Subject: [PATCH] refactor: reduce and simplify usage of getEnv() (#4799) * refactor: reduce and simplify usage of getEnv() * docs: update CHANGELOG * fixup! docs: update CHANGELOG --------- Co-authored-by: Marc Pichler --- CHANGELOG.md | 2 ++ experimental/CHANGELOG.md | 2 ++ .../src/platform/config.ts | 27 ++++++++------ .../src/platform/browser/OTLPLogExporter.ts | 32 ++++++++++------- .../src/platform/node/OTLPLogExporter.ts | 30 ++++++++++------ .../src/platform/browser/OTLPTraceExporter.ts | 32 +++++++++++------ .../src/platform/node/OTLPTraceExporter.ts | 32 +++++++++++------ .../src/platform/browser/OTLPTraceExporter.ts | 32 +++++++++++------ .../src/platform/node/OTLPTraceExporter.ts | 32 +++++++++++------ .../platform/browser/OTLPMetricExporter.ts | 34 +++++++++++------- .../src/platform/node/OTLPMetricExporter.ts | 34 +++++++++++------- .../src/OTLPMetricExporter.ts | 36 +++++++++++-------- .../src/config.ts | 20 +++++------ 13 files changed, 217 insertions(+), 128 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3349baeb2..644972b950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :house: (Internal) +* refactor: Simplify the code for the `getEnv` function [#4799](https://github.com/open-telemetry/opentelemetry-js/pull/4799) @danstarns + ## 1.25.1 ### :books: (Refine Doc) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index c5f566a43f..a0b2c19201 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -15,6 +15,8 @@ All notable changes to experimental packages in this project will be documented ### :house: (Internal) +* refactor: Simplify the code for the `getEnv` function [#4799](https://github.com/open-telemetry/opentelemetry-js/pull/4799) @danstarns + ## 0.52.1 ### :rocket: (Enhancement) diff --git a/experimental/packages/exporter-logs-otlp-http/src/platform/config.ts b/experimental/packages/exporter-logs-otlp-http/src/platform/config.ts index a41ee96468..5ce6eb75f7 100644 --- a/experimental/packages/exporter-logs-otlp-http/src/platform/config.ts +++ b/experimental/packages/exporter-logs-otlp-http/src/platform/config.ts @@ -30,14 +30,21 @@ export const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_ * @returns url string */ export function getDefaultUrl(config: OTLPExporterConfigBase): string { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded(env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } diff --git a/experimental/packages/exporter-logs-otlp-proto/src/platform/browser/OTLPLogExporter.ts b/experimental/packages/exporter-logs-otlp-proto/src/platform/browser/OTLPLogExporter.ts index a1ed000d66..259ce6a080 100644 --- a/experimental/packages/exporter-logs-otlp-proto/src/platform/browser/OTLPLogExporter.ts +++ b/experimental/packages/exporter-logs-otlp-proto/src/platform/browser/OTLPLogExporter.ts @@ -40,24 +40,30 @@ export class OTLPLogExporter { constructor(config: OTLPExporterConfigBase = {}) { super(config, ProtobufLogsSerializer, 'application/x-protobuf'); + const env = getEnv(); this._headers = Object.assign( this._headers, - baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS - ) + baggageUtils.parseKeyPairsIntoRecord(env.OTEL_EXPORTER_OTLP_LOGS_HEADERS) ); } getDefaultUrl(config: OTLPExporterConfigBase): string { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded(env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts b/experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts index 989d5e237a..de3f8697b1 100644 --- a/experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts +++ b/experimental/packages/exporter-logs-otlp-proto/src/platform/node/OTLPLogExporter.ts @@ -46,26 +46,34 @@ export class OTLPLogExporter { constructor(config: OTLPExporterConfigBase = {}) { super(config, ProtobufLogsSerializer, 'application/x-protobuf'); + const env = getEnv(); this.headers = { ...this.headers, ...USER_AGENT, ...baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS + env.OTEL_EXPORTER_OTLP_LOGS_HEADERS ), ...parseHeaders(config?.headers), }; } getDefaultUrl(config: OTLPExporterConfigBase): string { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded(env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts index 594054cf49..b19ce672fd 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts @@ -39,24 +39,34 @@ export class OTLPTraceExporter { constructor(config: OTLPExporterConfigBase = {}) { super(config, JsonTraceSerializer, 'application/json'); + const env = getEnv(); this._headers = Object.assign( this._headers, baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS + env.OTEL_EXPORTER_OTLP_TRACES_HEADERS ) ); } getDefaultUrl(config: OTLPExporterConfigBase): string { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded( + env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT + ); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts index e8e77831fb..6a978bc897 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts @@ -44,26 +44,36 @@ export class OTLPTraceExporter { constructor(config: OTLPExporterNodeConfigBase = {}) { super(config, JsonTraceSerializer, 'application/json'); + const env = getEnv(); this.headers = { ...this.headers, ...USER_AGENT, ...baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS + env.OTEL_EXPORTER_OTLP_TRACES_HEADERS ), ...parseHeaders(config?.headers), }; } getDefaultUrl(config: OTLPExporterNodeConfigBase): string { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded( + env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT + ); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/exporter-trace-otlp-proto/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-proto/src/platform/browser/OTLPTraceExporter.ts index 0de8a9a962..f474e95788 100644 --- a/experimental/packages/exporter-trace-otlp-proto/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-proto/src/platform/browser/OTLPTraceExporter.ts @@ -39,24 +39,34 @@ export class OTLPTraceExporter { constructor(config: OTLPExporterConfigBase = {}) { super(config, ProtobufTraceSerializer, 'application/x-protobuf'); + const env = getEnv(); this._headers = Object.assign( this._headers, baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS + env.OTEL_EXPORTER_OTLP_TRACES_HEADERS ) ); } getDefaultUrl(config: OTLPExporterConfigBase): string { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded( + env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT + ); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/exporter-trace-otlp-proto/src/platform/node/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-proto/src/platform/node/OTLPTraceExporter.ts index bb3c916dbb..75637863ed 100644 --- a/experimental/packages/exporter-trace-otlp-proto/src/platform/node/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-proto/src/platform/node/OTLPTraceExporter.ts @@ -44,26 +44,36 @@ export class OTLPTraceExporter { constructor(config: OTLPExporterNodeConfigBase = {}) { super(config, ProtobufTraceSerializer, 'application/x-protobuf'); + const env = getEnv(); this.headers = { ...this.headers, ...USER_AGENT, ...baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS + env.OTEL_EXPORTER_OTLP_TRACES_HEADERS ), ...parseHeaders(config?.headers), }; } getDefaultUrl(config: OTLPExporterNodeConfigBase) { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded( + env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT + ); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts index 890f1e9deb..0fa43efa78 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts @@ -38,27 +38,35 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase< > { constructor(config?: OTLPMetricExporterOptions & OTLPExporterConfigBase) { super(config, JsonMetricsSerializer, 'application/json'); + const env = getEnv(); this._headers = Object.assign( this._headers, baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS + env.OTEL_EXPORTER_OTLP_METRICS_HEADERS ) ); } getDefaultUrl(config: OTLPExporterConfigBase): string { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded( - getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT - ) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded( + env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT + ); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts index d812c37b26..1f0dc59b27 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts @@ -43,29 +43,37 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase< > { constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) { super(config, JsonMetricsSerializer, 'application/json'); + const env = getEnv(); this.headers = { ...this.headers, ...USER_AGENT, ...baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS + env.OTEL_EXPORTER_OTLP_METRICS_HEADERS ), ...parseHeaders(config?.headers), }; } getDefaultUrl(config: OTLPExporterNodeConfigBase): string { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded( - getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT - ) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded( + env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT + ); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts index 150181db01..944b3e892b 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts @@ -43,29 +43,37 @@ class OTLPMetricExporterNodeProxy extends OTLPExporterNodeBase< > { constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) { super(config, ProtobufMetricsSerializer, 'application/x-protobuf'); + const env = getEnv(); this.headers = { ...this.headers, ...USER_AGENT, ...baggageUtils.parseKeyPairsIntoRecord( - getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS + env.OTEL_EXPORTER_OTLP_METRICS_HEADERS ), ...parseHeaders(config?.headers), }; } - getDefaultUrl(config: OTLPExporterNodeConfigBase) { - return typeof config.url === 'string' - ? config.url - : getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0 - ? appendRootPathToUrlIfNeeded( - getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT - ) - : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0 - ? appendResourcePathToUrl( - getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, - DEFAULT_COLLECTOR_RESOURCE_PATH - ) - : DEFAULT_COLLECTOR_URL; + getDefaultUrl(config: OTLPExporterNodeConfigBase): string { + if (typeof config.url === 'string') { + return config.url; + } + + const env = getEnv(); + if (env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0) { + return appendRootPathToUrlIfNeeded( + env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT + ); + } + + if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) { + return appendResourcePathToUrl( + env.OTEL_EXPORTER_OTLP_ENDPOINT, + DEFAULT_COLLECTOR_RESOURCE_PATH + ); + } + + return DEFAULT_COLLECTOR_URL; } } diff --git a/packages/opentelemetry-sdk-trace-base/src/config.ts b/packages/opentelemetry-sdk-trace-base/src/config.ts index 3448b793da..f97a383599 100644 --- a/packages/opentelemetry-sdk-trace-base/src/config.ts +++ b/packages/opentelemetry-sdk-trace-base/src/config.ts @@ -36,23 +36,23 @@ const DEFAULT_RATIO = 1; // object needs to be wrapped in this function and called when needed otherwise // envs are parsed before tests are ran - causes tests using these envs to fail export function loadDefaultConfig() { + const _env = getEnv(); + return { sampler: buildSamplerFromEnv(env), forceFlushTimeoutMillis: 30000, generalLimits: { - attributeValueLengthLimit: getEnv().OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, - attributeCountLimit: getEnv().OTEL_ATTRIBUTE_COUNT_LIMIT, + attributeValueLengthLimit: _env.OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, + attributeCountLimit: _env.OTEL_ATTRIBUTE_COUNT_LIMIT, }, spanLimits: { - attributeValueLengthLimit: - getEnv().OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT, - attributeCountLimit: getEnv().OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, - linkCountLimit: getEnv().OTEL_SPAN_LINK_COUNT_LIMIT, - eventCountLimit: getEnv().OTEL_SPAN_EVENT_COUNT_LIMIT, + attributeValueLengthLimit: _env.OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT, + attributeCountLimit: _env.OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, + linkCountLimit: _env.OTEL_SPAN_LINK_COUNT_LIMIT, + eventCountLimit: _env.OTEL_SPAN_EVENT_COUNT_LIMIT, attributePerEventCountLimit: - getEnv().OTEL_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT, - attributePerLinkCountLimit: - getEnv().OTEL_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT, + _env.OTEL_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT, + attributePerLinkCountLimit: _env.OTEL_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT, }, }; }