From 8cd59b35444e403d7b4a2596c54937071180d568 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Fri, 10 Jan 2025 12:07:48 -0800 Subject: [PATCH 1/2] fix(exporter-metrics-otlp-http): browser OTLPMetricExporter was not passing config to OTLPMetricExporterBase super class Refs: #5031 --- .../platform/browser/OTLPMetricExporter.ts | 3 +- .../test/browser/OTLPMetricExporter.test.ts | 147 ++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) 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 01a39a24795..2678942baeb 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 @@ -31,7 +31,8 @@ export class OTLPMetricExporter extends OTLPMetricExporterBase { JsonMetricsSerializer, 'v1/metrics', { 'Content-Type': 'application/json' } - ) + ), + config ); } } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/OTLPMetricExporter.test.ts index cd6d80d8eaa..88ecd9b8256 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/OTLPMetricExporter.test.ts @@ -14,7 +14,12 @@ * limitations under the License. */ +import { AggregationTemporalityPreference } from '../../src'; import { + AggregationOption, + AggregationTemporality, + AggregationType, + InstrumentType, MeterProvider, PeriodicExportingMetricReader, } from '@opentelemetry/sdk-metrics'; @@ -105,4 +110,146 @@ describe('OTLPMetricExporter', function () { }); }); }); + + describe('temporality', () => { + it('should use the right temporality when Cumulative preference is selected', () => { + const exporter = new OTLPMetricExporter({ + temporalityPreference: AggregationTemporalityPreference.CUMULATIVE, + }); + + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.COUNTER), + AggregationTemporality.CUMULATIVE, + 'Counter' + ); + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.HISTOGRAM), + AggregationTemporality.CUMULATIVE, + 'Histogram' + ); + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.UP_DOWN_COUNTER), + AggregationTemporality.CUMULATIVE, + 'UpDownCounter' + ); + assert.equal( + exporter.selectAggregationTemporality( + InstrumentType.OBSERVABLE_COUNTER + ), + AggregationTemporality.CUMULATIVE, + 'Asynchronous Counter' + ); + assert.equal( + exporter.selectAggregationTemporality( + InstrumentType.OBSERVABLE_UP_DOWN_COUNTER + ), + AggregationTemporality.CUMULATIVE, + 'Asynchronous UpDownCounter' + ); + }); + + it('should use the right temporality when Delta preference is selected', () => { + const exporter = new OTLPMetricExporter({ + temporalityPreference: AggregationTemporalityPreference.DELTA, + }); + + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.COUNTER), + AggregationTemporality.DELTA, + 'Counter' + ); + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.HISTOGRAM), + AggregationTemporality.DELTA, + 'Histogram' + ); + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.UP_DOWN_COUNTER), + AggregationTemporality.CUMULATIVE, + 'UpDownCounter' + ); + assert.equal( + exporter.selectAggregationTemporality( + InstrumentType.OBSERVABLE_COUNTER + ), + AggregationTemporality.DELTA, + 'Asynchronous Counter' + ); + assert.equal( + exporter.selectAggregationTemporality( + InstrumentType.OBSERVABLE_UP_DOWN_COUNTER + ), + AggregationTemporality.CUMULATIVE, + 'Asynchronous UpDownCounter' + ); + }); + + it('should use the right temporality when LowMemory preference is selected', () => { + const exporter = new OTLPMetricExporter({ + temporalityPreference: AggregationTemporalityPreference.LOWMEMORY, + }); + + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.COUNTER), + AggregationTemporality.DELTA, + 'Counter' + ); + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.HISTOGRAM), + AggregationTemporality.DELTA, + 'Histogram' + ); + assert.equal( + exporter.selectAggregationTemporality(InstrumentType.UP_DOWN_COUNTER), + AggregationTemporality.CUMULATIVE, + 'UpDownCounter' + ); + assert.equal( + exporter.selectAggregationTemporality( + InstrumentType.OBSERVABLE_COUNTER + ), + AggregationTemporality.CUMULATIVE, + 'Asynchronous Counter' + ); + assert.equal( + exporter.selectAggregationTemporality( + InstrumentType.OBSERVABLE_UP_DOWN_COUNTER + ), + AggregationTemporality.CUMULATIVE, + 'Asynchronous UpDownCounter' + ); + }); + }); + + describe('aggregation', () => { + it('aggregationSelector calls the selector supplied to the constructor', () => { + const aggregation: AggregationOption = { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { + boundaries: [0, 100, 100000], + }, + }; + const exporter = new OTLPMetricExporter({ + aggregationPreference: _instrumentType => aggregation, + }); + assert.equal( + exporter.selectAggregation(InstrumentType.COUNTER), + aggregation + ); + }); + + it('aggregationSelector returns the default aggregation preference when nothing is supplied', () => { + const exporter = new OTLPMetricExporter({ + aggregationPreference: _instrumentType => ({ + type: AggregationType.DEFAULT, + }), + }); + assert.deepStrictEqual( + exporter.selectAggregation(InstrumentType.COUNTER), + { + type: AggregationType.DEFAULT, + } + ); + }); + }); }); From f890b808481c871d8348fe112b28161a11e3d9e0 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Fri, 10 Jan 2025 12:12:50 -0800 Subject: [PATCH 2/2] changelog entry --- experimental/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 4680296deae..b627766308e 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -18,6 +18,8 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) +* fix(exporter-metrics-otlp-http): browser OTLPMetricExporter was not passing config to OTLPMetricExporterBase super class [#5331](https://github.com/open-telemetry/opentelemetry-js/pull/5331) @trentm + ### :books: (Refine Doc) ### :house: (Internal)