From b9c842613c4439435f1644c8c8e288d1fd0e384c Mon Sep 17 00:00:00 2001 From: Sergey Lanzman Date: Mon, 26 Apr 2021 21:56:45 +0300 Subject: [PATCH] chore(deps): support cumulative, delta, and pass-through exporters (#2118) Co-authored-by: Valentin Marchaud --- .../src/types/Metric.ts | 12 +++++++++ .../test/helper.ts | 2 +- .../test/helper.ts | 2 +- .../src/transformMetrics.ts | 25 +------------------ .../test/common/transformMetrics.test.ts | 1 + .../test/helper.ts | 4 +-- packages/opentelemetry-metrics/src/Metric.ts | 10 ++++++++ .../opentelemetry-metrics/src/export/types.ts | 7 +++++- 8 files changed, 34 insertions(+), 29 deletions(-) diff --git a/packages/opentelemetry-api-metrics/src/types/Metric.ts b/packages/opentelemetry-api-metrics/src/types/Metric.ts index 8f38bb8812..aebbc46246 100644 --- a/packages/opentelemetry-api-metrics/src/types/Metric.ts +++ b/packages/opentelemetry-api-metrics/src/types/Metric.ts @@ -58,6 +58,11 @@ export interface MetricOptions { * Boundaries optional for histogram */ boundaries?: number[]; + + /** + * Aggregation Temporality of metric + */ + aggregationTemporality?: AggregationTemporality; } export interface BatchObserverOptions { @@ -73,6 +78,13 @@ export enum ValueType { DOUBLE, } +/** The kind of aggregator. */ +export enum AggregationTemporality { + AGGREGATION_TEMPORALITY_UNSPECIFIED, + AGGREGATION_TEMPORALITY_DELTA, + AGGREGATION_TEMPORALITY_CUMULATIVE, +} + /** * Metric represents a base class for different types of metric * pre aggregations. diff --git a/packages/opentelemetry-exporter-collector-grpc/test/helper.ts b/packages/opentelemetry-exporter-collector-grpc/test/helper.ts index 8d2c0bb140..147fb1c236 100644 --- a/packages/opentelemetry-exporter-collector-grpc/test/helper.ts +++ b/packages/opentelemetry-exporter-collector-grpc/test/helper.ts @@ -343,7 +343,7 @@ export function ensureExportedCounterIsCorrect( }, ], isMonotonic: true, - aggregationTemporality: 'AGGREGATION_TEMPORALITY_DELTA', + aggregationTemporality: 'AGGREGATION_TEMPORALITY_CUMULATIVE', }, }); } diff --git a/packages/opentelemetry-exporter-collector-proto/test/helper.ts b/packages/opentelemetry-exporter-collector-proto/test/helper.ts index f9826e3bb9..83a98ca10d 100644 --- a/packages/opentelemetry-exporter-collector-proto/test/helper.ts +++ b/packages/opentelemetry-exporter-collector-proto/test/helper.ts @@ -308,7 +308,7 @@ export function ensureExportedCounterIsCorrect( }, ], isMonotonic: true, - aggregationTemporality: 'AGGREGATION_TEMPORALITY_DELTA', + aggregationTemporality: 'AGGREGATION_TEMPORALITY_CUMULATIVE', }, }); } diff --git a/packages/opentelemetry-exporter-collector/src/transformMetrics.ts b/packages/opentelemetry-exporter-collector/src/transformMetrics.ts index fd089f2a0c..b992a3b5ee 100644 --- a/packages/opentelemetry-exporter-collector/src/transformMetrics.ts +++ b/packages/opentelemetry-exporter-collector/src/transformMetrics.ts @@ -47,35 +47,12 @@ export function toCollectorLabels( export function toAggregationTemporality( metric: MetricRecord ): opentelemetryProto.metrics.v1.AggregationTemporality { - if ( - metric.descriptor.metricKind === MetricKind.COUNTER || - metric.descriptor.metricKind === MetricKind.UP_DOWN_COUNTER - ) { - return opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_DELTA; - } - - if ( - metric.descriptor.metricKind === MetricKind.SUM_OBSERVER || - metric.descriptor.metricKind === MetricKind.UP_DOWN_SUM_OBSERVER - ) { - return opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE; - } - if (metric.descriptor.metricKind === MetricKind.VALUE_OBSERVER) { return opentelemetryProto.metrics.v1.AggregationTemporality .AGGREGATION_TEMPORALITY_UNSPECIFIED; } - // until spec is resolved keep it as unspecified - if (metric.descriptor.metricKind === MetricKind.VALUE_RECORDER) { - return opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE; - } - - return opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_UNSPECIFIED; + return metric.aggregationTemporality; } /** diff --git a/packages/opentelemetry-exporter-collector/test/common/transformMetrics.test.ts b/packages/opentelemetry-exporter-collector/test/common/transformMetrics.test.ts index 14203f5d60..62b93cc077 100644 --- a/packages/opentelemetry-exporter-collector/test/common/transformMetrics.test.ts +++ b/packages/opentelemetry-exporter-collector/test/common/transformMetrics.test.ts @@ -171,6 +171,7 @@ describe('transformMetrics', () => { labels: { foo: (1 as unknown) as string }, aggregator: new SumAggregator(), resource: new Resource({}), + aggregationTemporality: 0, instrumentationLibrary: { name: 'x', version: 'y' }, }, 1592602232694000000 diff --git a/packages/opentelemetry-exporter-collector/test/helper.ts b/packages/opentelemetry-exporter-collector/test/helper.ts index b38785daab..f4d28a035a 100644 --- a/packages/opentelemetry-exporter-collector/test/helper.ts +++ b/packages/opentelemetry-exporter-collector/test/helper.ts @@ -574,7 +574,7 @@ export function ensureCounterIsCorrect( isMonotonic: true, aggregationTemporality: collectorTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_DELTA, + .AGGREGATION_TEMPORALITY_CUMULATIVE, }, }); } @@ -599,7 +599,7 @@ export function ensureDoubleCounterIsCorrect( isMonotonic: true, aggregationTemporality: collectorTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_DELTA, + .AGGREGATION_TEMPORALITY_CUMULATIVE, }, }); } diff --git a/packages/opentelemetry-metrics/src/Metric.ts b/packages/opentelemetry-metrics/src/Metric.ts index e806924652..b753b322ea 100644 --- a/packages/opentelemetry-metrics/src/Metric.ts +++ b/packages/opentelemetry-metrics/src/Metric.ts @@ -27,6 +27,7 @@ export abstract class Metric protected readonly _valueType: api.ValueType; protected readonly _descriptor: MetricDescriptor; protected readonly _boundaries: number[] | undefined; + protected readonly _aggregationTemporality: api.AggregationTemporality; private readonly _instruments: Map = new Map(); constructor( @@ -43,6 +44,10 @@ export abstract class Metric : api.ValueType.DOUBLE; this._boundaries = _options.boundaries; this._descriptor = this._getMetricDescriptor(); + this._aggregationTemporality = + _options.aggregationTemporality === undefined + ? api.AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE + : _options.aggregationTemporality; } /** @@ -83,6 +88,10 @@ export abstract class Metric return this._kind; } + getAggregationTemporality() { + return this._aggregationTemporality; + } + getMetricRecord(): Promise { return new Promise(resolve => { resolve( @@ -90,6 +99,7 @@ export abstract class Metric descriptor: this._descriptor, labels: instrument.getLabels(), aggregator: instrument.getAggregator(), + aggregationTemporality: this.getAggregationTemporality(), resource: this.resource, instrumentationLibrary: this.instrumentationLibrary, })) diff --git a/packages/opentelemetry-metrics/src/export/types.ts b/packages/opentelemetry-metrics/src/export/types.ts index b28ce79640..889e96598d 100644 --- a/packages/opentelemetry-metrics/src/export/types.ts +++ b/packages/opentelemetry-metrics/src/export/types.ts @@ -15,7 +15,11 @@ */ import { HrTime } from '@opentelemetry/api'; -import { Labels, ValueType } from '@opentelemetry/api-metrics'; +import { + Labels, + AggregationTemporality, + ValueType, +} from '@opentelemetry/api-metrics'; import { ExportResult, InstrumentationLibrary } from '@opentelemetry/core'; import { Resource } from '@opentelemetry/resources'; @@ -76,6 +80,7 @@ export interface MetricRecord { readonly descriptor: MetricDescriptor; readonly labels: Labels; readonly aggregator: Aggregator; + readonly aggregationTemporality: AggregationTemporality; readonly resource: Resource; readonly instrumentationLibrary: InstrumentationLibrary; }