From 590392dbd413502ae9ff73fa6737420bca5e624c Mon Sep 17 00:00:00 2001 From: Aksel Allas Date: Wed, 10 Jan 2024 17:15:37 +0200 Subject: [PATCH] feat(add-aggregation-selector-option-to-otlp-metric-exporter) --- experimental/CHANGELOG.md | 2 ++ .../src/OTLPMetricExporterBase.ts | 18 +++++++++++++ .../src/OTLPMetricExporterOptions.ts | 6 ++++- .../test/node/CollectorMetricExporter.test.ts | 27 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 845a7e7ad6..6733db73d3 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -13,6 +13,8 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* feat(exporter-metrics-otlp-http): add option to set the exporter aggregation preference [#4409](https://github.com/open-telemetry/opentelemetry-js/pull/4409) @AkselAllas + ### :bug: (Bug Fix) * fix(instrumentation): use caret range on import-in-the-middle [#4380](https://github.com/open-telemetry/opentelemetry-js/pull/4380) @pichlermarc diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts index db8a4a32ed..66a5bfb596 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts @@ -21,6 +21,8 @@ import { InstrumentType, PushMetricExporter, ResourceMetrics, + Aggregation, + AggregationSelector, } from '@opentelemetry/sdk-metrics'; import { AggregationTemporalityPreference, @@ -104,6 +106,16 @@ function chooseTemporalitySelector( return chooseTemporalitySelectorFromEnvironment(); } +function chooseAggregationSelector( + config: OTLPMetricExporterOptions | undefined +) { + if (config?.aggregationPreference) { + return config.aggregationPreference; + } else { + return (_instrumentType: any) => Aggregation.Default(); + } +} + export class OTLPMetricExporterBase< T extends OTLPExporterBase< OTLPMetricExporterOptions, @@ -114,9 +126,11 @@ export class OTLPMetricExporterBase< { public _otlpExporter: T; private _aggregationTemporalitySelector: AggregationTemporalitySelector; + private _aggregationSelector: AggregationSelector; constructor(exporter: T, config?: OTLPMetricExporterOptions) { this._otlpExporter = exporter; + this._aggregationSelector = chooseAggregationSelector(config); this._aggregationTemporalitySelector = chooseTemporalitySelector( config?.temporalityPreference ); @@ -137,6 +151,10 @@ export class OTLPMetricExporterBase< return Promise.resolve(); } + selectAggregation(instrumentType: InstrumentType): Aggregation { + return this._aggregationSelector(instrumentType); + } + selectAggregationTemporality( instrumentType: InstrumentType ): AggregationTemporality { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterOptions.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterOptions.ts index e03bde4e75..ff805e3b17 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterOptions.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterOptions.ts @@ -15,12 +15,16 @@ */ import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base'; -import { AggregationTemporality } from '@opentelemetry/sdk-metrics'; +import { + AggregationTemporality, + AggregationSelector, +} from '@opentelemetry/sdk-metrics'; export interface OTLPMetricExporterOptions extends OTLPExporterConfigBase { temporalityPreference?: | AggregationTemporalityPreference | AggregationTemporality; + aggregationPreference?: AggregationSelector; } export enum AggregationTemporalityPreference { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts index 66f6af3006..b72ed5f8cb 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts @@ -43,7 +43,9 @@ import { } from '../metricsHelper'; import { MockedResponse } from './nodeHelpers'; import { + Aggregation, AggregationTemporality, + ExplicitBucketHistogramAggregation, InstrumentType, ResourceMetrics, } from '@opentelemetry/sdk-metrics'; @@ -218,6 +220,31 @@ describe('OTLPMetricExporter - node with json over http', () => { }); }); + describe('aggregation', () => { + it('aggregationSelector calls the selector supplied to the constructor', () => { + const aggregation = new ExplicitBucketHistogramAggregation([ + 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 => Aggregation.Default(), + }); + assert.equal( + exporter.selectAggregation(InstrumentType.COUNTER), + Aggregation.Default() + ); + }); + }); + describe('when configuring via environment', () => { const envSource = process.env; it('should use url defined in env that ends with root path and append version and signal path', () => {