diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 845a7e7ad69..0568fa642ad 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) [#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 db8a4a32ed1..b37dc1b5afa 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, @@ -29,6 +31,7 @@ import { import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base'; import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; import { diag } from '@opentelemetry/api'; +import { DEFAULT_AGGREGATION_SELECTOR } from '@opentelemetry/sdk-metrics/src/export/AggregationSelector'; export const CumulativeTemporalitySelector: AggregationTemporalitySelector = () => AggregationTemporality.CUMULATIVE; @@ -104,6 +107,16 @@ function chooseTemporalitySelector( return chooseTemporalitySelectorFromEnvironment(); } +function chooseAggregationSelector( + config: OTLPMetricExporterOptions | undefined +) { + if (config?.aggregationSelector) { + return config.aggregationSelector; + } else { + return DEFAULT_AGGREGATION_SELECTOR; + } +} + export class OTLPMetricExporterBase< T extends OTLPExporterBase< OTLPMetricExporterOptions, @@ -114,9 +127,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 +152,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 e03bde4e75e..a4d9412dc6e 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; + aggregationSelector?: 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 66f6af3006e..5cc7bff10cb 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,32 @@ 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({ + aggregationSelector: _instrumentType => aggregation, + }); + assert.equal( + exporter.selectAggregation(InstrumentType.COUNTER), + aggregation + ); + }); + + it('aggregationSelector returns the default aggregation preference when nothing is supplied', () => { + const exporter = new OTLPMetricExporter({ + temporalityPreference: AggregationTemporalityPreference.CUMULATIVE, + aggregationSelector: _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', () => {