From 5110fc9a82d86e694d2e6f24ff9ecf77c0355a41 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Thu, 8 Aug 2024 15:15:18 +0200 Subject: [PATCH] feat(sdk-metrics)!: drop View and Aggregation for options --- CHANGELOG_NEXT.md | 1 + .../test/metricsHelper.ts | 12 +- .../src/OTLPMetricExporterBase.ts | 11 +- .../test/metricsHelper.ts | 17 ++- .../test/node/CollectorMetricExporter.test.ts | 24 ++-- .../test/metricsHelper.ts | 12 +- .../src/PrometheusExporter.ts | 8 +- .../test/PrometheusSerializer.test.ts | 75 +++++++----- .../opentelemetry-sdk-node/src/sdk.ts | 10 +- .../opentelemetry-sdk-node/src/types.ts | 4 +- .../opentelemetry-sdk-node/test/sdk.test.ts | 5 +- packages/sdk-metrics/src/MeterProvider.ts | 8 +- .../src/export/AggregationSelector.ts | 11 +- .../sdk-metrics/src/export/MetricExporter.ts | 4 +- .../sdk-metrics/src/export/MetricReader.ts | 6 +- packages/sdk-metrics/src/index.ts | 15 +-- .../src/state/MeterProviderSharedState.ts | 9 +- packages/sdk-metrics/src/view/Aggregation.ts | 61 +++------- .../sdk-metrics/src/view/AggregationOption.ts | 107 ++++++++++++++++++ packages/sdk-metrics/src/view/View.ts | 55 +++++---- .../sdk-metrics/test/MeterProvider.test.ts | 46 ++++---- .../test/export/MetricReader.test.ts | 14 ++- .../PeriodicExportingMetricReader.test.ts | 11 +- packages/sdk-metrics/test/export/utils.ts | 2 +- .../cumulative-exponential-histogram.test.ts | 11 +- .../histogram-recording-nan.test.ts | 6 +- .../test/state/MeterSharedState.test.ts | 26 ++--- .../sdk-metrics/test/view/Aggregation.test.ts | 27 ----- packages/sdk-metrics/test/view/View.test.ts | 16 +-- .../test/view/ViewRegistry.test.ts | 2 +- 30 files changed, 375 insertions(+), 241 deletions(-) create mode 100644 packages/sdk-metrics/src/view/AggregationOption.ts diff --git a/CHANGELOG_NEXT.md b/CHANGELOG_NEXT.md index a41332c0fc8..1e19dc449ad 100644 --- a/CHANGELOG_NEXT.md +++ b/CHANGELOG_NEXT.md @@ -10,6 +10,7 @@ * chore(otel-resources): replace deprecated SpanAttributes [#4428](https://github.com/open-telemetry/opentelemetry-js/pull/4428) @JamieDanielson * feat(sdk-metrics)!: remove MeterProvider.addMetricReader() in favor of constructor option [#4419](https://github.com/open-telemetry/opentelemetry-js/pull/4419) @pichlermarc * feat(sdk-metrics)!: replace attributeKeys with custom processors option [#4532](https://github.com/open-telemetry/opentelemetry-js/pull/4532) @pichlermarc +* feat(sdk-metrics)!: drop `View` and `Aggregation` in favor of `ViewOptions` and `AggregationOption` [#4931](https://github.com/open-telemetry/opentelemetry-js/pull/4931) @pichlermarc ### :rocket: (Enhancement) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts index 7c9fbfd99c0..352169212a7 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts @@ -27,10 +27,9 @@ import * as assert from 'assert'; import * as grpc from '@grpc/grpc-js'; import { VERSION } from '@opentelemetry/core'; import { - ExplicitBucketHistogramAggregation, + AggregationType, MeterProvider, MetricReader, - View, } from '@opentelemetry/sdk-metrics'; import { encodeAsString, @@ -72,10 +71,13 @@ export function setUp() { meterProvider = new MeterProvider({ resource: testResource, views: [ - new View({ - aggregation: new ExplicitBucketHistogramAggregation([0, 100]), + { + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { boundaries: [0, 100] }, + }, instrumentName: 'int-histogram', - }), + }, ], readers: [reader], }); 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 76b7f74dfda..78e6a8c19d4 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts @@ -21,8 +21,9 @@ import { InstrumentType, PushMetricExporter, ResourceMetrics, - Aggregation, AggregationSelector, + AggregationOption, + AggregationType, } from '@opentelemetry/sdk-metrics'; import { AggregationTemporalityPreference, @@ -113,7 +114,11 @@ function chooseAggregationSelector( if (config?.aggregationPreference) { return config.aggregationPreference; } else { - return (_instrumentType: any) => Aggregation.Default(); + return (_instrumentType: any) => { + return { + type: AggregationType.DEFAULT, + }; + }; } } @@ -148,7 +153,7 @@ export class OTLPMetricExporterBase< return Promise.resolve(); } - selectAggregation(instrumentType: InstrumentType): Aggregation { + selectAggregation(instrumentType: InstrumentType): AggregationOption { return this._aggregationSelector(instrumentType); } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts index 850cdae4d0f..62f749f1afe 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts @@ -28,10 +28,10 @@ import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; import { InstrumentationScope, VERSION } from '@opentelemetry/core'; import { - ExplicitBucketHistogramAggregation, + AggregationType, MeterProvider, MetricReader, - View, + ViewOptions, } from '@opentelemetry/sdk-metrics'; import { encodeAsString, @@ -59,10 +59,15 @@ class TestMetricReader extends MetricReader { } } -export const HISTOGRAM_AGGREGATION_VIEW = new View({ - aggregation: new ExplicitBucketHistogramAggregation([0, 100]), +export const HISTOGRAM_AGGREGATION_VIEW: ViewOptions = { + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { + boundaries: [0, 100], + }, + }, instrumentName: 'int-histogram', -}); +}; const defaultResource = Resource.default().merge( new Resource({ @@ -83,7 +88,7 @@ export async function collect() { return (await reader.collect())!; } -export function setUp(views?: View[]) { +export function setUp(views?: ViewOptions[]) { reader = new TestMetricReader(); meterProvider = new MeterProvider({ resource: defaultResource, 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 7b19b84f029..676b3860830 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,9 +43,9 @@ import { } from '../metricsHelper'; import { MockedResponse } from './nodeHelpers'; import { - Aggregation, + AggregationOption, AggregationTemporality, - ExplicitBucketHistogramAggregation, + AggregationType, InstrumentType, ResourceMetrics, } from '@opentelemetry/sdk-metrics'; @@ -225,9 +225,13 @@ 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 aggregation: AggregationOption = { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { + boundaries: [0, 100, 100000], + }, + }; + const exporter = new OTLPMetricExporter({ aggregationPreference: _instrumentType => aggregation, }); @@ -239,11 +243,15 @@ describe('OTLPMetricExporter - node with json over http', () => { it('aggregationSelector returns the default aggregation preference when nothing is supplied', () => { const exporter = new OTLPMetricExporter({ - aggregationPreference: _instrumentType => Aggregation.Default(), + aggregationPreference: _instrumentType => { + return { type: AggregationType.DEFAULT }; + }, }); - assert.equal( + assert.deepStrictEqual( exporter.selectAggregation(InstrumentType.COUNTER), - Aggregation.Default() + { + type: AggregationType.DEFAULT, + } ); }); }); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts index effc732b370..756d470bc97 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts @@ -25,10 +25,9 @@ import { import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; import { - ExplicitBucketHistogramAggregation, + AggregationType, MeterProvider, MetricReader, - View, } from '@opentelemetry/sdk-metrics'; import { encodeAsString, @@ -71,10 +70,13 @@ export function setUp() { meterProvider = new MeterProvider({ resource: testResource, views: [ - new View({ - aggregation: new ExplicitBucketHistogramAggregation([0, 100]), + { + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { boundaries: [0, 100] }, + }, instrumentName: 'int-histogram', - }), + }, ], readers: [reader], }); diff --git a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts index bd763de3f0c..e9f95051f82 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts @@ -17,8 +17,8 @@ import { diag } from '@opentelemetry/api'; import { globalErrorHandler } from '@opentelemetry/core'; import { - Aggregation, AggregationTemporality, + AggregationType, MetricReader, } from '@opentelemetry/sdk-metrics'; import { createServer, IncomingMessage, Server, ServerResponse } from 'http'; @@ -60,7 +60,11 @@ export class PrometheusExporter extends MetricReader { callback: (error: Error | void) => void = () => {} ) { super({ - aggregationSelector: _instrumentType => Aggregation.Default(), + aggregationSelector: _instrumentType => { + return { + type: AggregationType.DEFAULT, + }; + }, aggregationTemporalitySelector: _instrumentType => AggregationTemporality.CUMULATIVE, metricProducers: config.metricProducers, diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index 6368cf82c30..3939d9ff410 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -17,17 +17,12 @@ import * as assert from 'assert'; import { MetricAttributes, UpDownCounter } from '@opentelemetry/api'; import { - Aggregation, AggregationTemporality, DataPoint, DataPointType, - ExplicitBucketHistogramAggregation, Histogram, - LastValueAggregation, MeterProvider, MetricReader, - SumAggregation, - View, } from '@opentelemetry/sdk-metrics'; import * as sinon from 'sinon'; import { PrometheusSerializer } from '../src'; @@ -40,6 +35,7 @@ import { serviceName, } from './util'; import { Resource } from '@opentelemetry/resources'; +import { AggregationType } from '@opentelemetry/sdk-metrics'; const attributes = { foo1: 'bar1', @@ -56,7 +52,9 @@ class TestMetricReader extends MetricReader { super({ aggregationTemporalitySelector: _instrumentType => AggregationTemporality.CUMULATIVE, - aggregationSelector: _instrumentType => Aggregation.Default(), + aggregationSelector: _instrumentType => { + return { type: AggregationType.DEFAULT }; + }, }); } @@ -86,10 +84,10 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ - aggregation: new SumAggregation(), + { + aggregation: { type: AggregationType.SUM }, instrumentName: '*', - }), + }, ], readers: [reader], }); @@ -136,10 +134,15 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ - aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]), + { + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { + boundaries: [1, 10, 100], + }, + }, instrumentName: '*', - }), + }, ], readers: [reader], }); @@ -201,10 +204,10 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ - aggregation: new SumAggregation(), + { + aggregation: { type: AggregationType.SUM }, instrumentName: '*', - }), + }, ], readers: [reader], }); @@ -256,10 +259,10 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ - aggregation: new SumAggregation(), + { + aggregation: { type: AggregationType.SUM }, instrumentName: '*', - }), + }, ], readers: [reader], }); @@ -310,10 +313,12 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ - aggregation: new LastValueAggregation(), + { + aggregation: { + type: AggregationType.LAST_VALUE, + }, instrumentName: '*', - }), + }, ], readers: [reader], }); @@ -364,10 +369,13 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ - aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]), + { + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { boundaries: [1, 10, 100] }, + }, instrumentName: '*', - }), + }, ], readers: [reader], }); @@ -419,10 +427,13 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ - aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]), + { + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { boundaries: [1, 10, 100] }, + }, instrumentName: '*', - }), + }, ], readers: [reader], }); @@ -472,7 +483,10 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ aggregation: new SumAggregation(), instrumentName: '*' }), + { + aggregation: { type: AggregationType.SUM }, + instrumentName: '*', + }, ], readers: [reader], }); @@ -561,7 +575,10 @@ describe('PrometheusSerializer', () => { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ views: [ - new View({ aggregation: new SumAggregation(), instrumentName: '*' }), + { + aggregation: { type: AggregationType.SUM }, + instrumentName: '*', + }, ], readers: [reader], }); diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index 24a7d2332d0..f13de7514f4 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -38,7 +38,11 @@ import { ResourceDetectionConfig, } from '@opentelemetry/resources'; import { LogRecordProcessor, LoggerProvider } from '@opentelemetry/sdk-logs'; -import { MeterProvider, MetricReader, View } from '@opentelemetry/sdk-metrics'; +import { + MeterProvider, + MetricReader, + ViewOptions, +} from '@opentelemetry/sdk-metrics'; import { BatchSpanProcessor, SpanProcessor, @@ -61,9 +65,9 @@ export type MeterProviderConfig = { */ reader?: MetricReader; /** - * List of {@link View}s that should be passed to the MeterProvider + * List of {@link ViewOptions}s that should be passed to the MeterProvider */ - views?: View[]; + views?: ViewOptions[]; }; export type LoggerProviderConfig = { diff --git a/experimental/packages/opentelemetry-sdk-node/src/types.ts b/experimental/packages/opentelemetry-sdk-node/src/types.ts index c3b2a1cd84c..e420318418d 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/types.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/types.ts @@ -19,7 +19,7 @@ import { TextMapPropagator } from '@opentelemetry/api'; import { Instrumentation } from '@opentelemetry/instrumentation'; import { Detector, DetectorSync, IResource } from '@opentelemetry/resources'; import { LogRecordProcessor } from '@opentelemetry/sdk-logs'; -import { MetricReader, View } from '@opentelemetry/sdk-metrics'; +import { MetricReader, ViewOptions } from '@opentelemetry/sdk-metrics'; import { Sampler, SpanExporter, @@ -34,7 +34,7 @@ export interface NodeSDKConfiguration { textMapPropagator: TextMapPropagator; logRecordProcessor: LogRecordProcessor; metricReader: MetricReader; - views: View[]; + views: ViewOptions[]; instrumentations: (Instrumentation | Instrumentation[])[]; resource: IResource; resourceDetectors: Array; diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index 280564ff79f..3011ef01314 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -36,7 +36,6 @@ import { InstrumentType, MeterProvider, PeriodicExportingMetricReader, - View, } from '@opentelemetry/sdk-metrics'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { @@ -376,11 +375,11 @@ describe('Node SDK', () => { const sdk = new NodeSDK({ metricReader: metricReader, views: [ - new View({ + { name: 'test-view', instrumentName: 'test_counter', instrumentType: InstrumentType.COUNTER, - }), + }, ], autoDetectResources: false, }); diff --git a/packages/sdk-metrics/src/MeterProvider.ts b/packages/sdk-metrics/src/MeterProvider.ts index f4668d27cd5..3471f414d5d 100644 --- a/packages/sdk-metrics/src/MeterProvider.ts +++ b/packages/sdk-metrics/src/MeterProvider.ts @@ -26,7 +26,7 @@ import { MetricReader } from './export/MetricReader'; import { MeterProviderSharedState } from './state/MeterProviderSharedState'; import { MetricCollector } from './state/MetricCollector'; import { ForceFlushOptions, ShutdownOptions } from './types'; -import { View } from './view/View'; +import { View, ViewOptions } from './view/View'; /** * MeterProviderOptions provides an interface for configuring a MeterProvider. @@ -34,7 +34,7 @@ import { View } from './view/View'; export interface MeterProviderOptions { /** Resource associated with metric telemetry */ resource?: IResource; - views?: View[]; + views?: ViewOptions[]; readers?: MetricReader[]; } @@ -51,8 +51,8 @@ export class MeterProvider implements IMeterProvider { ); this._sharedState = new MeterProviderSharedState(resource); if (options?.views != null && options.views.length > 0) { - for (const view of options.views) { - this._sharedState.viewRegistry.addView(view); + for (const viewOption of options.views) { + this._sharedState.viewRegistry.addView(new View(viewOption)); } } diff --git a/packages/sdk-metrics/src/export/AggregationSelector.ts b/packages/sdk-metrics/src/export/AggregationSelector.ts index 7a4eaca9357..96d10daca4a 100644 --- a/packages/sdk-metrics/src/export/AggregationSelector.ts +++ b/packages/sdk-metrics/src/export/AggregationSelector.ts @@ -15,15 +15,15 @@ */ import { InstrumentType } from '../InstrumentDescriptor'; -import { Aggregation } from '../view/Aggregation'; import { AggregationTemporality } from './AggregationTemporality'; +import { AggregationOption, AggregationType } from '../view/AggregationOption'; /** * Aggregation selector based on metric instrument types. */ export type AggregationSelector = ( instrumentType: InstrumentType -) => Aggregation; +) => AggregationOption; /** * Aggregation temporality selector based on metric instrument types. @@ -33,6 +33,11 @@ export type AggregationTemporalitySelector = ( ) => AggregationTemporality; export const DEFAULT_AGGREGATION_SELECTOR: AggregationSelector = - _instrumentType => Aggregation.Default(); + _instrumentType => { + return { + type: AggregationType.DEFAULT, + }; + }; + export const DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR: AggregationTemporalitySelector = _instrumentType => AggregationTemporality.CUMULATIVE; diff --git a/packages/sdk-metrics/src/export/MetricExporter.ts b/packages/sdk-metrics/src/export/MetricExporter.ts index bf9362bcdd1..38ca30ce67b 100644 --- a/packages/sdk-metrics/src/export/MetricExporter.ts +++ b/packages/sdk-metrics/src/export/MetricExporter.ts @@ -18,7 +18,7 @@ import { AggregationTemporality } from './AggregationTemporality'; import { ResourceMetrics } from './MetricData'; import { ExportResult } from '@opentelemetry/core'; import { InstrumentType } from '../InstrumentDescriptor'; -import { Aggregation } from '../view/Aggregation'; +import { AggregationOption } from '../view/AggregationOption'; /** * An interface that allows different metric services to export recorded data @@ -55,7 +55,7 @@ export interface PushMetricExporter { * Select the {@link Aggregation} for the given * {@link InstrumentType} for this exporter. */ - selectAggregation?(instrumentType: InstrumentType): Aggregation; + selectAggregation?(instrumentType: InstrumentType): AggregationOption; /** * Returns a promise which resolves when the last exportation is completed. diff --git a/packages/sdk-metrics/src/export/MetricReader.ts b/packages/sdk-metrics/src/export/MetricReader.ts index 8aad601d70f..9ebed6e2dca 100644 --- a/packages/sdk-metrics/src/export/MetricReader.ts +++ b/packages/sdk-metrics/src/export/MetricReader.ts @@ -25,13 +25,13 @@ import { ForceFlushOptions, ShutdownOptions, } from '../types'; -import { Aggregation } from '../view/Aggregation'; import { AggregationSelector, AggregationTemporalitySelector, DEFAULT_AGGREGATION_SELECTOR, DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR, } from './AggregationSelector'; +import { AggregationOption } from '../view/AggregationOption'; export interface MetricReaderOptions { /** @@ -99,10 +99,10 @@ export abstract class MetricReader { } /** - * Select the {@link Aggregation} for the given {@link InstrumentType} for this + * Select the {@link AggregationOption} for the given {@link InstrumentType} for this * reader. */ - selectAggregation(instrumentType: InstrumentType): Aggregation { + selectAggregation(instrumentType: InstrumentType): AggregationOption { return this._aggregationSelector(instrumentType); } diff --git a/packages/sdk-metrics/src/index.ts b/packages/sdk-metrics/src/index.ts index e2f9ad2d0e4..4f359214bc8 100644 --- a/packages/sdk-metrics/src/index.ts +++ b/packages/sdk-metrics/src/index.ts @@ -67,18 +67,9 @@ export type InstrumentDescriptor = MetricDescriptor; export { MeterProvider, MeterProviderOptions } from './MeterProvider'; -export { - DefaultAggregation, - ExplicitBucketHistogramAggregation, - ExponentialHistogramAggregation, - DropAggregation, - HistogramAggregation, - LastValueAggregation, - SumAggregation, - Aggregation, -} from './view/Aggregation'; - -export { View, ViewOptions } from './view/View'; +export { AggregationOption, AggregationType } from './view/AggregationOption'; + +export { ViewOptions } from './view/View'; export { IAttributesProcessor, diff --git a/packages/sdk-metrics/src/state/MeterProviderSharedState.ts b/packages/sdk-metrics/src/state/MeterProviderSharedState.ts index fa7903b20e2..2e8bc57796d 100644 --- a/packages/sdk-metrics/src/state/MeterProviderSharedState.ts +++ b/packages/sdk-metrics/src/state/MeterProviderSharedState.ts @@ -16,11 +16,13 @@ import { InstrumentationScope } from '@opentelemetry/core'; import { IResource } from '@opentelemetry/resources'; -import { Aggregation, InstrumentType } from '..'; +import { InstrumentType } from '..'; import { instrumentationScopeId } from '../utils'; import { ViewRegistry } from '../view/ViewRegistry'; import { MeterSharedState } from './MeterSharedState'; import { MetricCollector, MetricCollectorHandle } from './MetricCollector'; +import { toAggregation } from '../view/AggregationOption'; +import { Aggregation } from '../view/Aggregation'; /** * An internal record for shared meter provider states. @@ -47,7 +49,10 @@ export class MeterProviderSharedState { selectAggregations(instrumentType: InstrumentType) { const result: [MetricCollectorHandle, Aggregation][] = []; for (const collector of this.metricCollectors) { - result.push([collector, collector.selectAggregation(instrumentType)]); + result.push([ + collector, + toAggregation(collector.selectAggregation(instrumentType)), + ]); } return result; } diff --git a/packages/sdk-metrics/src/view/Aggregation.ts b/packages/sdk-metrics/src/view/Aggregation.ts index f34bd1231a3..f3894489b17 100644 --- a/packages/sdk-metrics/src/view/Aggregation.ts +++ b/packages/sdk-metrics/src/view/Aggregation.ts @@ -32,40 +32,16 @@ import { Maybe } from '../utils'; * * Aggregation provides a set of built-in aggregations via static methods. */ -export abstract class Aggregation { - abstract createAggregator( +export interface Aggregation { + createAggregator( instrument: InstrumentDescriptor ): Aggregator>; - - static Drop(): Aggregation { - return DROP_AGGREGATION; - } - - static Sum(): Aggregation { - return SUM_AGGREGATION; - } - - static LastValue(): Aggregation { - return LAST_VALUE_AGGREGATION; - } - - static Histogram(): Aggregation { - return HISTOGRAM_AGGREGATION; - } - - static ExponentialHistogram(): Aggregation { - return EXPONENTIAL_HISTOGRAM_AGGREGATION; - } - - static Default(): Aggregation { - return DEFAULT_AGGREGATION; - } } /** * The default drop aggregation. */ -export class DropAggregation extends Aggregation { +export class DropAggregation implements Aggregation { private static DEFAULT_INSTANCE = new DropAggregator(); createAggregator(_instrument: InstrumentDescriptor) { return DropAggregation.DEFAULT_INSTANCE; @@ -75,7 +51,7 @@ export class DropAggregation extends Aggregation { /** * The default sum aggregation. */ -export class SumAggregation extends Aggregation { +export class SumAggregation implements Aggregation { private static MONOTONIC_INSTANCE = new SumAggregator(true); private static NON_MONOTONIC_INSTANCE = new SumAggregator(false); createAggregator(instrument: InstrumentDescriptor) { @@ -95,7 +71,7 @@ export class SumAggregation extends Aggregation { /** * The default last value aggregation. */ -export class LastValueAggregation extends Aggregation { +export class LastValueAggregation implements Aggregation { private static DEFAULT_INSTANCE = new LastValueAggregator(); createAggregator(_instrument: InstrumentDescriptor) { return LastValueAggregation.DEFAULT_INSTANCE; @@ -104,8 +80,9 @@ export class LastValueAggregation extends Aggregation { /** * The default histogram aggregation. + */ -export class HistogramAggregation extends Aggregation { +export class HistogramAggregation implements Aggregation { private static DEFAULT_INSTANCE = new HistogramAggregator( [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000], true @@ -118,7 +95,7 @@ export class HistogramAggregation extends Aggregation { /** * The explicit bucket histogram aggregation. */ -export class ExplicitBucketHistogramAggregation extends Aggregation { +export class ExplicitBucketHistogramAggregation implements Aggregation { private _boundaries: number[]; /** @@ -129,7 +106,6 @@ export class ExplicitBucketHistogramAggregation extends Aggregation { boundaries: number[], private readonly _recordMinMax = true ) { - super(); if (boundaries == null) { throw new Error( 'ExplicitBucketHistogramAggregation should be created with explicit boundaries, if a single bucket histogram is required, please pass an empty array' @@ -154,13 +130,11 @@ export class ExplicitBucketHistogramAggregation extends Aggregation { } } -export class ExponentialHistogramAggregation extends Aggregation { +export class ExponentialHistogramAggregation implements Aggregation { constructor( private readonly _maxSize: number = 160, private readonly _recordMinMax = true - ) { - super(); - } + ) {} createAggregator(_instrument: InstrumentDescriptor) { return new ExponentialHistogramAggregator( this._maxSize, @@ -172,7 +146,7 @@ export class ExponentialHistogramAggregation extends Aggregation { /** * The default aggregation. */ -export class DefaultAggregation extends Aggregation { +export class DefaultAggregation implements Aggregation { private _resolve(instrument: InstrumentDescriptor): Aggregation { // cast to unknown to disable complaints on the (unreachable) fallback. switch (instrument.type as unknown) { @@ -206,9 +180,10 @@ export class DefaultAggregation extends Aggregation { } } -const DROP_AGGREGATION = new DropAggregation(); -const SUM_AGGREGATION = new SumAggregation(); -const LAST_VALUE_AGGREGATION = new LastValueAggregation(); -const HISTOGRAM_AGGREGATION = new HistogramAggregation(); -const EXPONENTIAL_HISTOGRAM_AGGREGATION = new ExponentialHistogramAggregation(); -const DEFAULT_AGGREGATION = new DefaultAggregation(); +export const DROP_AGGREGATION = new DropAggregation(); +export const SUM_AGGREGATION = new SumAggregation(); +export const LAST_VALUE_AGGREGATION = new LastValueAggregation(); +export const HISTOGRAM_AGGREGATION = new HistogramAggregation(); +export const EXPONENTIAL_HISTOGRAM_AGGREGATION = + new ExponentialHistogramAggregation(); +export const DEFAULT_AGGREGATION = new DefaultAggregation(); diff --git a/packages/sdk-metrics/src/view/AggregationOption.ts b/packages/sdk-metrics/src/view/AggregationOption.ts new file mode 100644 index 00000000000..255f523bcfa --- /dev/null +++ b/packages/sdk-metrics/src/view/AggregationOption.ts @@ -0,0 +1,107 @@ +/* + * 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 { + Aggregation, + DEFAULT_AGGREGATION, + DROP_AGGREGATION, + ExplicitBucketHistogramAggregation, + ExponentialHistogramAggregation, + HISTOGRAM_AGGREGATION, + LAST_VALUE_AGGREGATION, + SUM_AGGREGATION, +} from './Aggregation'; + +export enum AggregationType { + DEFAULT = 0, + DROP = 1, + SUM = 2, + LAST_VALUE = 3, + EXPLICIT_BUCKET_HISTOGRAM = 4, + EXPONENTIAL_HISTOGRAM = 5, +} + +export type SumAggregationOption = { + type: AggregationType.SUM; +}; + +export type LastValueAggregationOption = { + type: AggregationType.LAST_VALUE; +}; + +export type DropAggregationOption = { + type: AggregationType.DROP; +}; + +export type DefaultAggregationOption = { + type: AggregationType.DEFAULT; +}; + +export type HistogramAggregationOption = { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM; + options?: { + recordMinMax?: boolean; + boundaries: number[]; + }; +}; + +export type ExponentialHistogramAggregationOption = { + type: AggregationType.EXPONENTIAL_HISTOGRAM; + options?: { + recordMinMax?: boolean; + maxSize?: number; + }; +}; + +export type AggregationOption = + | ExponentialHistogramAggregationOption + | HistogramAggregationOption + | SumAggregationOption + | DropAggregationOption + | DefaultAggregationOption + | LastValueAggregationOption; + +export function toAggregation(option: AggregationOption): Aggregation { + switch (option.type) { + case AggregationType.DEFAULT: + return DEFAULT_AGGREGATION; + case AggregationType.DROP: + return DROP_AGGREGATION; + case AggregationType.SUM: + return SUM_AGGREGATION; + case AggregationType.LAST_VALUE: + return LAST_VALUE_AGGREGATION; + case AggregationType.EXPONENTIAL_HISTOGRAM: { + const expOption = option as ExponentialHistogramAggregationOption; + return new ExponentialHistogramAggregation( + expOption.options?.maxSize, + expOption.options?.recordMinMax + ); + } + case AggregationType.EXPLICIT_BUCKET_HISTOGRAM: { + const expOption = option as HistogramAggregationOption; + if (expOption.options == null) { + return HISTOGRAM_AGGREGATION; + } else { + return new ExplicitBucketHistogramAggregation( + expOption.options?.boundaries, + expOption.options?.recordMinMax + ); + } + } + default: + throw new Error('Unsupported Aggregation'); + } +} diff --git a/packages/sdk-metrics/src/view/View.ts b/packages/sdk-metrics/src/view/View.ts index 4d46c868ba3..00227e918ba 100644 --- a/packages/sdk-metrics/src/view/View.ts +++ b/packages/sdk-metrics/src/view/View.ts @@ -24,6 +24,11 @@ import { InstrumentSelector } from './InstrumentSelector'; import { MeterSelector } from './MeterSelector'; import { Aggregation } from './Aggregation'; import { InstrumentType } from '../InstrumentDescriptor'; +import { + AggregationOption, + AggregationType, + toAggregation, +} from './AggregationOption'; export type ViewOptions = { /** @@ -57,14 +62,14 @@ export type ViewOptions = { attributesProcessors?: IAttributesProcessor[]; /** * Alters the metric stream: - * Alters the {@link Aggregation} of the metric stream. + * Alters the Aggregation of the metric stream. * * @example changes the aggregation of the selected instrument(s) to ExplicitBucketHistogramAggregation - * aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]) + * aggregation: { type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, options: { boundaries: [1, 10, 100] } } * @example changes the aggregation of the selected instrument(s) to LastValueAggregation - * aggregation: new LastValueAggregation() + * aggregation: { type: AggregationType.LAST_VALUE, options: { boundaries: [1, 10, 100] } } */ - aggregation?: Aggregation; + aggregation?: AggregationOption; /** * Instrument selection criteria: * The original type of the Instrument(s). @@ -132,6 +137,26 @@ function isSelectorNotProvided(options: ViewOptions): boolean { ); } +function validateViewOptions(viewOptions: ViewOptions) { + // If no criteria is provided, the SDK SHOULD treat it as an error. + // It is recommended that the SDK implementations fail fast. + if (isSelectorNotProvided(viewOptions)) { + throw new Error('Cannot create view with no selector arguments supplied'); + } + + // the SDK SHOULD NOT allow Views with a specified name to be declared with instrument selectors that + // may select more than one instrument (e.g. wild card instrument name) in the same Meter. + if ( + viewOptions.name != null && + (viewOptions?.instrumentName == null || + PatternPredicate.hasWildcard(viewOptions.instrumentName)) + ) { + throw new Error( + 'Views with a specified name must be declared with an instrument selector that selects at most one instrument per meter.' + ); + } +} + /** * Can be passed to a {@link MeterProvider} to select instruments and alter their metric stream. */ @@ -196,23 +221,7 @@ export class View { * }) */ constructor(viewOptions: ViewOptions) { - // If no criteria is provided, the SDK SHOULD treat it as an error. - // It is recommended that the SDK implementations fail fast. - if (isSelectorNotProvided(viewOptions)) { - throw new Error('Cannot create view with no selector arguments supplied'); - } - - // the SDK SHOULD NOT allow Views with a specified name to be declared with instrument selectors that - // may select more than one instrument (e.g. wild card instrument name) in the same Meter. - if ( - viewOptions.name != null && - (viewOptions?.instrumentName == null || - PatternPredicate.hasWildcard(viewOptions.instrumentName)) - ) { - throw new Error( - 'Views with a specified name must be declared with an instrument selector that selects at most one instrument per meter.' - ); - } + validateViewOptions(viewOptions); // Create multi-processor if attributesProcessors are defined. if (viewOptions.attributesProcessors != null) { @@ -225,7 +234,9 @@ export class View { this.name = viewOptions.name; this.description = viewOptions.description; - this.aggregation = viewOptions.aggregation ?? Aggregation.Default(); + this.aggregation = toAggregation( + viewOptions.aggregation ?? { type: AggregationType.DEFAULT } + ); this.instrumentSelector = new InstrumentSelector({ name: viewOptions.instrumentName, type: viewOptions.instrumentType, diff --git a/packages/sdk-metrics/test/MeterProvider.test.ts b/packages/sdk-metrics/test/MeterProvider.test.ts index 364cd8779ff..c9a7eafd102 100644 --- a/packages/sdk-metrics/test/MeterProvider.test.ts +++ b/packages/sdk-metrics/test/MeterProvider.test.ts @@ -19,7 +19,6 @@ import { MeterProvider, InstrumentType, DataPointType, - ExplicitBucketHistogramAggregation, HistogramMetricData, } from '../src'; import { @@ -30,9 +29,9 @@ import { } from './util'; import { TestMetricReader } from './export/TestMetricReader'; import * as sinon from 'sinon'; -import { View } from '../src/view/View'; import { Meter } from '../src/Meter'; import { createAllowListAttributesProcessor } from '../src/view/AttributesProcessor'; +import { AggregationType } from '../src/view/AggregationOption'; describe('MeterProvider', () => { afterEach(() => { @@ -139,11 +138,11 @@ describe('MeterProvider', () => { resource: defaultResource, // Add view to rename 'non-renamed-instrument' to 'renamed-instrument' views: [ - new View({ + { name: 'renamed-instrument', description: 'my renamed instrument', instrumentName: 'non-renamed-instrument', - }), + }, ], readers: [reader], }); @@ -208,12 +207,12 @@ describe('MeterProvider', () => { const meterProvider = new MeterProvider({ resource: defaultResource, views: [ - new View({ + { attributesProcessors: [ createAllowListAttributesProcessor(['attrib1']), ], instrumentName: 'non-renamed-instrument', - }), + }, ], readers: [reader], }); @@ -276,10 +275,10 @@ describe('MeterProvider', () => { const meterProvider = new MeterProvider({ resource: defaultResource, views: [ - new View({ + { name: 'renamed-instrument', instrumentName: 'test-counter', - }), + }, ], readers: [reader], }); @@ -348,11 +347,11 @@ describe('MeterProvider', () => { resource: defaultResource, views: [ // Add view that renames 'test-counter' to 'renamed-instrument' on 'meter1' - new View({ + { name: 'renamed-instrument', instrumentName: 'test-counter', meterName: 'meter1', - }), + }, ], readers: [reader], }); @@ -421,16 +420,16 @@ describe('MeterProvider', () => { resource: defaultResource, // Add Views to rename both instruments (of different types) to the same name. views: [ - new View({ + { name: 'renamed-instrument', instrumentName: 'test-counter', meterName: 'meter1', - }), - new View({ + }, + { name: 'renamed-instrument', instrumentName: 'test-histogram', meterName: 'meter1', - }), + }, ], readers: [reader], }); @@ -489,14 +488,21 @@ describe('MeterProvider', () => { const meterProvider = new MeterProvider({ resource: defaultResource, views: [ - new View({ + { instrumentUnit: 'ms', - aggregation: new ExplicitBucketHistogramAggregation(msBoundaries), - }), - new View({ + // aggregation: new ExplicitBucketHistogramAggregation(msBoundaries), + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { boundaries: msBoundaries }, + }, + }, + { instrumentUnit: 's', - aggregation: new ExplicitBucketHistogramAggregation(sBoundaries), - }), + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { boundaries: sBoundaries }, + }, + }, ], readers: [reader], }); diff --git a/packages/sdk-metrics/test/export/MetricReader.test.ts b/packages/sdk-metrics/test/export/MetricReader.test.ts index b08cde5e38d..cc091c97977 100644 --- a/packages/sdk-metrics/test/export/MetricReader.test.ts +++ b/packages/sdk-metrics/test/export/MetricReader.test.ts @@ -21,8 +21,8 @@ import { assertRejects } from '../test-utils'; import { emptyResourceMetrics, TestMetricProducer } from './TestMetricProducer'; import { TestMetricReader } from './TestMetricReader'; import { - Aggregation, AggregationTemporality, + AggregationType, DataPointType, InstrumentType, ScopeMetrics, @@ -228,9 +228,17 @@ describe('MetricReader', () => { it('should override default when provided with a selector', () => { const reader = new TestMetricReader({ - aggregationSelector: _instrumentType => Aggregation.Sum(), + aggregationSelector: _instrumentType => { + return { + type: AggregationType.SUM, + }; + }, + }); + assertAggregationSelector(reader, _instrumentType => { + return { + type: AggregationType.SUM, + }; }); - assertAggregationSelector(reader, _instrumentType => Aggregation.Sum()); reader.shutdown(); }); }); diff --git a/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts b/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts index d5ab5531267..fcfc86511f2 100644 --- a/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts +++ b/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts @@ -16,7 +16,12 @@ import { PeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader'; import { AggregationTemporality } from '../../src/export/AggregationTemporality'; -import { Aggregation, InstrumentType, PushMetricExporter } from '../../src'; +import { + AggregationOption, + AggregationType, + InstrumentType, + PushMetricExporter, +} from '../../src'; import { ResourceMetrics } from '../../src/export/MetricData'; import * as assert from 'assert'; import * as sinon from 'sinon'; @@ -107,8 +112,8 @@ class TestDeltaMetricExporter extends TestMetricExporter { } class TestDropMetricExporter extends TestMetricExporter { - selectAggregation(_instrumentType: InstrumentType): Aggregation { - return Aggregation.Drop(); + selectAggregation(_instrumentType: InstrumentType): AggregationOption { + return { type: AggregationType.DROP }; } } diff --git a/packages/sdk-metrics/test/export/utils.ts b/packages/sdk-metrics/test/export/utils.ts index a10021ee905..77b2791b203 100644 --- a/packages/sdk-metrics/test/export/utils.ts +++ b/packages/sdk-metrics/test/export/utils.ts @@ -43,7 +43,7 @@ export function assertAggregationSelector( expectedSelector: AggregationSelector ) { for (const instrumentType of instrumentTypes) { - assert.strictEqual( + assert.deepStrictEqual( reader.selectAggregation?.(instrumentType), expectedSelector(instrumentType), `incorrect aggregation selection for ${InstrumentType[instrumentType]}` diff --git a/packages/sdk-metrics/test/regression/cumulative-exponential-histogram.test.ts b/packages/sdk-metrics/test/regression/cumulative-exponential-histogram.test.ts index 2462ddf5c81..3787308d9ec 100644 --- a/packages/sdk-metrics/test/regression/cumulative-exponential-histogram.test.ts +++ b/packages/sdk-metrics/test/regression/cumulative-exponential-histogram.test.ts @@ -17,8 +17,9 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import { - Aggregation, + AggregationOption, AggregationTemporality, + AggregationType, InstrumentType, MeterProvider, MetricReader, @@ -37,15 +38,15 @@ describe('cumulative-exponential-histogram', () => { it('Cumulative Histogram should have the same startTime every collection', async () => { // Works fine and passes - await doTest(Aggregation.Histogram()); + await doTest({ type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM }); }); it('Cumulative ExponentialHistogram should have the same startTime every collection', async () => { // Fails - await doTest(Aggregation.ExponentialHistogram()); + await doTest({ type: AggregationType.EXPONENTIAL_HISTOGRAM }); }); - const doTest = async (histogramAggregation: Aggregation) => { + const doTest = async (histogramAggregation: AggregationOption) => { const reader = new TestMetricReader({ aggregationTemporalitySelector() { return AggregationTemporality.CUMULATIVE; @@ -53,7 +54,7 @@ describe('cumulative-exponential-histogram', () => { aggregationSelector(type) { return type === InstrumentType.HISTOGRAM ? histogramAggregation - : Aggregation.Default(); + : { type: AggregationType.DEFAULT }; }, }); const meterProvider = new MeterProvider({ diff --git a/packages/sdk-metrics/test/regression/histogram-recording-nan.test.ts b/packages/sdk-metrics/test/regression/histogram-recording-nan.test.ts index a37c9c8e92e..4cb73ff4984 100644 --- a/packages/sdk-metrics/test/regression/histogram-recording-nan.test.ts +++ b/packages/sdk-metrics/test/regression/histogram-recording-nan.test.ts @@ -16,13 +16,13 @@ import * as assert from 'assert'; import { - Aggregation, AggregationTemporality, MeterProvider, MetricReader, DataPoint, ExponentialHistogram, Histogram, + AggregationType, } from '../../src'; import { TestMetricReader } from '../export/TestMetricReader'; @@ -33,7 +33,7 @@ describe('histogram-recording-nan', () => { return AggregationTemporality.CUMULATIVE; }, aggregationSelector(type) { - return Aggregation.ExponentialHistogram(); + return { type: AggregationType.EXPONENTIAL_HISTOGRAM }; }, }); const meterProvider = new MeterProvider({ @@ -65,7 +65,7 @@ describe('histogram-recording-nan', () => { return AggregationTemporality.CUMULATIVE; }, aggregationSelector(type) { - return Aggregation.Histogram(); + return { type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM }; }, }); const meterProvider = new MeterProvider({ diff --git a/packages/sdk-metrics/test/state/MeterSharedState.test.ts b/packages/sdk-metrics/test/state/MeterSharedState.test.ts index 9e814a4b439..74b4ff9a273 100644 --- a/packages/sdk-metrics/test/state/MeterSharedState.test.ts +++ b/packages/sdk-metrics/test/state/MeterSharedState.test.ts @@ -19,10 +19,10 @@ import * as sinon from 'sinon'; import { MeterProvider, DataPointType, - View, - Aggregation, MetricReader, InstrumentType, + AggregationType, + ViewOptions, } from '../../src'; import { assertMetricData, @@ -44,7 +44,7 @@ describe('MeterSharedState', () => { }); describe('registerMetricStorage', () => { - function setupMeter(views?: View[], readers?: MetricReader[]) { + function setupMeter(views?: ViewOptions[], readers?: MetricReader[]) { const meterProvider = new MeterProvider({ resource: defaultResource, views, @@ -69,7 +69,7 @@ describe('MeterSharedState', () => { }, }); const { meter, meterSharedState, collectors } = setupMeter( - [new View({ instrumentName: 'test-counter' })], + [{ instrumentName: 'test-counter' }], [reader] ); @@ -92,7 +92,7 @@ describe('MeterSharedState', () => { }, }); const { meter, meterSharedState, collectors } = setupMeter( - [new View({ instrumentName: 'test-counter' })], + [{ instrumentName: 'test-counter' }], [reader] ); @@ -111,7 +111,7 @@ describe('MeterSharedState', () => { it('should register metric storages with the collector', () => { const reader = new TestMetricReader({ aggregationSelector: (instrumentType: InstrumentType) => { - return Aggregation.Drop(); + return { type: AggregationType.DROP }; }, }); const readerAggregationSelectorSpy = sinon.spy( @@ -141,12 +141,12 @@ describe('MeterSharedState', () => { it('should register metric storages with collectors', () => { const reader = new TestMetricReader({ aggregationSelector: (instrumentType: InstrumentType) => { - return Aggregation.Drop(); + return { type: AggregationType.DROP }; }, }); const reader2 = new TestMetricReader({ aggregationSelector: (instrumentType: InstrumentType) => { - return Aggregation.LastValue(); + return { type: AggregationType.LAST_VALUE }; }, }); @@ -184,7 +184,7 @@ describe('MeterSharedState', () => { }); describe('collect', () => { - function setupInstruments(views?: View[]) { + function setupInstruments(views?: ViewOptions[]) { const cumulativeReader = new TestMetricReader(); const deltaReader = new TestDeltaMetricReader(); @@ -245,8 +245,8 @@ describe('MeterSharedState', () => { it('should collect sync metrics with views', async () => { /** preparing test instrumentations */ const { metricCollectors, meter } = setupInstruments([ - new View({ name: 'foo', instrumentName: 'test' }), - new View({ name: 'bar', instrumentName: 'test' }), + { name: 'foo', instrumentName: 'test' }, + { name: 'bar', instrumentName: 'test' }, ]); /** creating metric events */ @@ -314,8 +314,8 @@ describe('MeterSharedState', () => { it('should call observable callback once with view-ed async instruments', async () => { /** preparing test instrumentations */ const { metricCollectors, meter } = setupInstruments([ - new View({ name: 'foo', instrumentName: 'test' }), - new View({ name: 'bar', instrumentName: 'test' }), + { name: 'foo', instrumentName: 'test' }, + { name: 'bar', instrumentName: 'test' }, ]); /** creating metric events */ diff --git a/packages/sdk-metrics/test/view/Aggregation.test.ts b/packages/sdk-metrics/test/view/Aggregation.test.ts index 51595aa5907..a2b25b77f81 100644 --- a/packages/sdk-metrics/test/view/Aggregation.test.ts +++ b/packages/sdk-metrics/test/view/Aggregation.test.ts @@ -27,43 +27,16 @@ import { InstrumentType, } from '../../src/InstrumentDescriptor'; import { - Aggregation, DefaultAggregation, - DropAggregation, ExplicitBucketHistogramAggregation, HistogramAggregation, - LastValueAggregation, - SumAggregation, } from '../../src/view/Aggregation'; import { defaultInstrumentDescriptor } from '../util'; -interface AggregationConstructor { - new (...args: any[]): Aggregation; -} - interface AggregatorConstructor { new (...args: any[]): Aggregator; } -describe('Aggregation', () => { - it('static aggregations', () => { - const staticMembers: [keyof typeof Aggregation, AggregationConstructor][] = - [ - ['Drop', DropAggregation], - ['Sum', SumAggregation], - ['LastValue', LastValueAggregation], - ['Histogram', HistogramAggregation], - ['Default', DefaultAggregation], - ]; - - for (const [key, type] of staticMembers) { - const aggregation = (Aggregation[key] as () => Aggregation)(); - assert(aggregation instanceof type); - assert(aggregation.createAggregator(defaultInstrumentDescriptor)); - } - }); -}); - describe('DefaultAggregation', () => { describe('createAggregator', () => { it('should create aggregators for instrument descriptors', () => { diff --git a/packages/sdk-metrics/test/view/View.test.ts b/packages/sdk-metrics/test/view/View.test.ts index 7328372390a..f17a6ab3fcd 100644 --- a/packages/sdk-metrics/test/view/View.test.ts +++ b/packages/sdk-metrics/test/view/View.test.ts @@ -19,12 +19,9 @@ import { createAllowListAttributesProcessor, createNoopAttributesProcessor, } from '../../src/view/AttributesProcessor'; +import { InstrumentType, AggregationType } from '../../src'; +import { DEFAULT_AGGREGATION } from '../../src/view/Aggregation'; import { View } from '../../src/view/View'; -import { - InstrumentType, - Aggregation, - ExplicitBucketHistogramAggregation, -} from '../../src'; describe('View', () => { describe('constructor', () => { @@ -33,7 +30,7 @@ describe('View', () => { const view = new View({ instrumentName: '*' }); assert.strictEqual(view.name, undefined); assert.strictEqual(view.description, undefined); - assert.strictEqual(view.aggregation, Aggregation.Default()); + assert.strictEqual(view.aggregation, DEFAULT_AGGREGATION); assert.strictEqual( view.attributesProcessor, createNoopAttributesProcessor() @@ -43,7 +40,7 @@ describe('View', () => { const view = new View({ meterName: '*' }); assert.strictEqual(view.name, undefined); assert.strictEqual(view.description, undefined); - assert.strictEqual(view.aggregation, Aggregation.Default()); + assert.strictEqual(view.aggregation, DEFAULT_AGGREGATION); assert.strictEqual( view.attributesProcessor, createNoopAttributesProcessor() @@ -69,7 +66,10 @@ describe('View', () => { assert.throws( () => new View({ - aggregation: new ExplicitBucketHistogramAggregation([1, 100]), + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + options: { boundaries: [1, 100] }, + }, }) ); }); diff --git a/packages/sdk-metrics/test/view/ViewRegistry.test.ts b/packages/sdk-metrics/test/view/ViewRegistry.test.ts index b766c0fda51..bb1dadfbf01 100644 --- a/packages/sdk-metrics/test/view/ViewRegistry.test.ts +++ b/packages/sdk-metrics/test/view/ViewRegistry.test.ts @@ -21,7 +21,7 @@ import { defaultInstrumentationScope, defaultInstrumentDescriptor, } from '../util'; -import { View } from '../../src'; +import { View } from '../../src/view/View'; describe('ViewRegistry', () => { describe('findViews', () => {