diff --git a/packages/exporter-trace-otlp-http/src/platform/node/OTLPExporterNodeBase.ts b/packages/exporter-trace-otlp-http/src/platform/node/OTLPExporterNodeBase.ts index 11f582a83c..b4f70b65fd 100644 --- a/packages/exporter-trace-otlp-http/src/platform/node/OTLPExporterNodeBase.ts +++ b/packages/exporter-trace-otlp-http/src/platform/node/OTLPExporterNodeBase.ts @@ -21,7 +21,7 @@ import { OTLPExporterBase } from '../../OTLPExporterBase'; import { OTLPExporterNodeConfigBase, CompressionAlgorithm } from './types'; import * as otlpTypes from '../../types'; import { parseHeaders } from '../../util'; -import { createHttpAgent, sendWithHttp } from './util'; +import { createHttpAgent, sendWithHttp, configureCompression } from './util'; import { diag } from '@opentelemetry/api'; import { getEnv, baggageUtils } from '@opentelemetry/core'; @@ -53,7 +53,7 @@ export abstract class OTLPExporterNodeBase< baggageUtils.parseKeyPairsIntoRecord(getEnv().OTEL_EXPORTER_OTLP_HEADERS) ); this.agent = createHttpAgent(config); - this.compression = config.compression || CompressionAlgorithm.NONE; + this.compression = configureCompression(config.compression); } onInit(_config: OTLPExporterNodeConfigBase): void {} diff --git a/packages/exporter-trace-otlp-http/src/platform/node/util.ts b/packages/exporter-trace-otlp-http/src/platform/node/util.ts index 0e40b585db..bfeddcb61c 100644 --- a/packages/exporter-trace-otlp-http/src/platform/node/util.ts +++ b/packages/exporter-trace-otlp-http/src/platform/node/util.ts @@ -23,6 +23,7 @@ import { OTLPExporterNodeBase } from './OTLPExporterNodeBase'; import { OTLPExporterNodeConfigBase } from '.'; import { diag } from '@opentelemetry/api'; import { CompressionAlgorithm } from './types'; +import { getEnv } from '@opentelemetry/core'; let gzip: zlib.Gzip | undefined; @@ -131,3 +132,12 @@ export function createHttpAgent( return undefined; } } + +export function configureCompression(compression: CompressionAlgorithm | undefined): CompressionAlgorithm { + if (compression) { + return compression; + } else { + const definedCompression = getEnv().OTEL_EXPORTER_OTLP_TRACES_COMPRESSION || getEnv().OTEL_EXPORTER_OTLP_COMPRESSION; + return definedCompression === CompressionAlgorithm.GZIP ? CompressionAlgorithm.GZIP : CompressionAlgorithm.NONE; + } +} diff --git a/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts b/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts index af543bae22..e01ff2980c 100644 --- a/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts +++ b/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts @@ -112,6 +112,20 @@ describe('OTLPTraceExporter - node with json over http', () => { envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = ''; envSource.OTEL_EXPORTER_OTLP_HEADERS = ''; }); + it('should use compression defined via env', () => { + envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'gzip'; + const collectorExporter = new OTLPTraceExporter(); + assert.strictEqual(collectorExporter.compression, 'gzip'); + envSource.OTEL_EXPORTER_OTLP_COMPRESSION = ''; + }); + it('should override global compression config with signal compression defined via env', () => { + envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'foo'; + envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'gzip'; + const collectorExporter = new OTLPTraceExporter(); + assert.strictEqual(collectorExporter.compression, 'gzip'); + envSource.OTEL_EXPORTER_OTLP_COMPRESSION = ''; + envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = ''; + }); }); describe('export', () => { diff --git a/packages/exporter-trace-otlp-http/test/node/utils.test.ts b/packages/exporter-trace-otlp-http/test/node/utils.test.ts new file mode 100644 index 0000000000..c920f4e874 --- /dev/null +++ b/packages/exporter-trace-otlp-http/test/node/utils.test.ts @@ -0,0 +1,40 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as assert from 'assert'; +import { CompressionAlgorithm} from '../../src/platform/node/types'; +import { configureCompression} from '../../src/platform/node/util'; + +describe('configureCompression', () => { + const envSource = process.env; + it('should return none for compression', () => { + const compression = CompressionAlgorithm.NONE; + assert.strictEqual(configureCompression(compression), CompressionAlgorithm.NONE); + }); + it('should return gzip compression defined via env', () => { + envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'gzip'; + assert.strictEqual(configureCompression(undefined),CompressionAlgorithm.GZIP); + delete envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION; + }); + it('should return none for compression defined via env', () => { + envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'none'; + assert.strictEqual(configureCompression(undefined),CompressionAlgorithm.NONE); + delete envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION; + }); + it('should return none for compression when no compression is set', () => { + assert.strictEqual(configureCompression(undefined),CompressionAlgorithm.NONE); + }); +}); diff --git a/packages/opentelemetry-core/src/utils/environment.ts b/packages/opentelemetry-core/src/utils/environment.ts index e3bcfd8623..b43c85e6a5 100644 --- a/packages/opentelemetry-core/src/utils/environment.ts +++ b/packages/opentelemetry-core/src/utils/environment.ts @@ -84,6 +84,9 @@ export type ENVIRONMENT = { OTEL_TRACES_EXPORTER?: string; OTEL_TRACES_SAMPLER_ARG?: string; OTEL_TRACES_SAMPLER?: string; + OTEL_EXPORTER_OTLP_COMPRESSION?: string, + OTEL_EXPORTER_OTLP_TRACES_COMPRESSION?: string, + OTEL_EXPORTER_OTLP_METRICS_COMPRESSION?: string } & ENVIRONMENT_NUMBERS & ENVIRONMENT_LISTS; @@ -135,6 +138,9 @@ export const DEFAULT_ENVIRONMENT: Required = { OTEL_TRACES_EXPORTER: 'none', OTEL_TRACES_SAMPLER: TracesSamplerValues.ParentBasedAlwaysOn, OTEL_TRACES_SAMPLER_ARG: '', + OTEL_EXPORTER_OTLP_COMPRESSION: '', + OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: '', + OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: '' }; /**