From 9579106a9d5081d05da6d70b243918baebb6b593 Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 1 Mar 2022 18:42:44 +0800 Subject: [PATCH 1/2] test(sdk-trace-base): pin core.hrtime dependencies on timeOrigin (#2768) Co-authored-by: Valentin Marchaud --- karma.webpack.js | 2 +- .../test/common/Span.test.ts | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/karma.webpack.js b/karma.webpack.js index bc8eb3a7907..559d129b658 100644 --- a/karma.webpack.js +++ b/karma.webpack.js @@ -22,7 +22,7 @@ module.exports = { target: 'web', output: { filename: 'bundle.js' }, resolve: { extensions: ['.ts', '.js'] }, - devtool: 'inline-source-map', + devtool: 'eval-source-map', module: { rules: [ { test: /\.ts$/, use: 'ts-loader' }, diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts index 9f6c8e416fd..2a81b4c42f8 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts @@ -21,22 +21,32 @@ import { SpanContext, SpanKind, TraceFlags, + HrTime, } from '@opentelemetry/api'; import { DEFAULT_ATTRIBUTE_COUNT_LIMIT, DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT, - hrTime, hrTimeDuration, hrTimeToMilliseconds, hrTimeToNanoseconds, + otperformance as performance, } from '@opentelemetry/core'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; +import * as sinon from 'sinon'; import { BasicTracerProvider, Span, SpanProcessor } from '../../src'; -const performanceTimeOrigin = hrTime(); +const performanceTimeOrigin: HrTime = [1, 1]; describe('Span', () => { + beforeEach(() => { + sinon.stub(performance, 'timeOrigin') + .value(hrTimeToMilliseconds(performanceTimeOrigin)); + }); + afterEach(() => { + sinon.restore(); + }); + const tracer = new BasicTracerProvider({ spanLimits: { attributeValueLengthLimit: 100, From df0cee9b7fb59fbe5182b69667c00aeb984236ed Mon Sep 17 00:00:00 2001 From: Svetlana Brennan <50715937+svetlanabrennan@users.noreply.github.com> Date: Tue, 1 Mar 2022 21:46:12 -0600 Subject: [PATCH 2/2] feat(trace-otlp-http-exporter): add compression env vars (#2796) Co-authored-by: legendecas Co-authored-by: Valentin Marchaud --- .../src/platform/node/OTLPExporterNodeBase.ts | 4 +- .../src/platform/node/util.ts | 10 +++++ .../test/node/CollectorTraceExporter.test.ts | 14 +++++++ .../test/node/utils.test.ts | 40 +++++++++++++++++++ .../src/utils/environment.ts | 6 +++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 packages/exporter-trace-otlp-http/test/node/utils.test.ts 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 11f582a83ce..b4f70b65fd1 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 0e40b585dbf..bfeddcb61c5 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 af543bae221..e01ff2980c9 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 00000000000..c920f4e8743 --- /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 e3bcfd86234..b43c85e6a50 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: '' }; /**