Skip to content

Commit df0cee9

Browse files
svetlanabrennanlegendecasvmarchaud
authored
feat(trace-otlp-http-exporter): add compression env vars (#2796)
Co-authored-by: legendecas <[email protected]> Co-authored-by: Valentin Marchaud <[email protected]>
1 parent 9579106 commit df0cee9

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

packages/exporter-trace-otlp-http/src/platform/node/OTLPExporterNodeBase.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { OTLPExporterBase } from '../../OTLPExporterBase';
2121
import { OTLPExporterNodeConfigBase, CompressionAlgorithm } from './types';
2222
import * as otlpTypes from '../../types';
2323
import { parseHeaders } from '../../util';
24-
import { createHttpAgent, sendWithHttp } from './util';
24+
import { createHttpAgent, sendWithHttp, configureCompression } from './util';
2525
import { diag } from '@opentelemetry/api';
2626
import { getEnv, baggageUtils } from '@opentelemetry/core';
2727

@@ -53,7 +53,7 @@ export abstract class OTLPExporterNodeBase<
5353
baggageUtils.parseKeyPairsIntoRecord(getEnv().OTEL_EXPORTER_OTLP_HEADERS)
5454
);
5555
this.agent = createHttpAgent(config);
56-
this.compression = config.compression || CompressionAlgorithm.NONE;
56+
this.compression = configureCompression(config.compression);
5757
}
5858

5959
onInit(_config: OTLPExporterNodeConfigBase): void {}

packages/exporter-trace-otlp-http/src/platform/node/util.ts

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { OTLPExporterNodeBase } from './OTLPExporterNodeBase';
2323
import { OTLPExporterNodeConfigBase } from '.';
2424
import { diag } from '@opentelemetry/api';
2525
import { CompressionAlgorithm } from './types';
26+
import { getEnv } from '@opentelemetry/core';
2627

2728
let gzip: zlib.Gzip | undefined;
2829

@@ -131,3 +132,12 @@ export function createHttpAgent(
131132
return undefined;
132133
}
133134
}
135+
136+
export function configureCompression(compression: CompressionAlgorithm | undefined): CompressionAlgorithm {
137+
if (compression) {
138+
return compression;
139+
} else {
140+
const definedCompression = getEnv().OTEL_EXPORTER_OTLP_TRACES_COMPRESSION || getEnv().OTEL_EXPORTER_OTLP_COMPRESSION;
141+
return definedCompression === CompressionAlgorithm.GZIP ? CompressionAlgorithm.GZIP : CompressionAlgorithm.NONE;
142+
}
143+
}

packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ describe('OTLPTraceExporter - node with json over http', () => {
112112
envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = '';
113113
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
114114
});
115+
it('should use compression defined via env', () => {
116+
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'gzip';
117+
const collectorExporter = new OTLPTraceExporter();
118+
assert.strictEqual(collectorExporter.compression, 'gzip');
119+
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = '';
120+
});
121+
it('should override global compression config with signal compression defined via env', () => {
122+
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'foo';
123+
envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'gzip';
124+
const collectorExporter = new OTLPTraceExporter();
125+
assert.strictEqual(collectorExporter.compression, 'gzip');
126+
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = '';
127+
envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = '';
128+
});
115129
});
116130

117131
describe('export', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import { CompressionAlgorithm} from '../../src/platform/node/types';
19+
import { configureCompression} from '../../src/platform/node/util';
20+
21+
describe('configureCompression', () => {
22+
const envSource = process.env;
23+
it('should return none for compression', () => {
24+
const compression = CompressionAlgorithm.NONE;
25+
assert.strictEqual(configureCompression(compression), CompressionAlgorithm.NONE);
26+
});
27+
it('should return gzip compression defined via env', () => {
28+
envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'gzip';
29+
assert.strictEqual(configureCompression(undefined),CompressionAlgorithm.GZIP);
30+
delete envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION;
31+
});
32+
it('should return none for compression defined via env', () => {
33+
envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'none';
34+
assert.strictEqual(configureCompression(undefined),CompressionAlgorithm.NONE);
35+
delete envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION;
36+
});
37+
it('should return none for compression when no compression is set', () => {
38+
assert.strictEqual(configureCompression(undefined),CompressionAlgorithm.NONE);
39+
});
40+
});

packages/opentelemetry-core/src/utils/environment.ts

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export type ENVIRONMENT = {
8484
OTEL_TRACES_EXPORTER?: string;
8585
OTEL_TRACES_SAMPLER_ARG?: string;
8686
OTEL_TRACES_SAMPLER?: string;
87+
OTEL_EXPORTER_OTLP_COMPRESSION?: string,
88+
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION?: string,
89+
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION?: string
8790
} & ENVIRONMENT_NUMBERS &
8891
ENVIRONMENT_LISTS;
8992

@@ -135,6 +138,9 @@ export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
135138
OTEL_TRACES_EXPORTER: 'none',
136139
OTEL_TRACES_SAMPLER: TracesSamplerValues.ParentBasedAlwaysOn,
137140
OTEL_TRACES_SAMPLER_ARG: '',
141+
OTEL_EXPORTER_OTLP_COMPRESSION: '',
142+
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: '',
143+
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: ''
138144
};
139145

140146
/**

0 commit comments

Comments
 (0)