Skip to content
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

feat(trace-otlp-http-exporter): add compression env vars #2796

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 {}
Expand Down
10 changes: 10 additions & 0 deletions packages/exporter-trace-otlp-http/src/platform/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 === 'gzip' ? CompressionAlgorithm.GZIP : CompressionAlgorithm.NONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
40 changes: 40 additions & 0 deletions packages/exporter-trace-otlp-http/test/node/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
6 changes: 6 additions & 0 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -135,6 +138,9 @@ export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
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: ''
};

/**
Expand Down