From 9ac9976b38296be782f015bfc6fc145db0de9a9b Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Tue, 19 Jul 2022 19:25:14 +0200 Subject: [PATCH] feat(sdk-metrics-base): distinguish between Sum and Gauge in MetricData (#3079) * feature(sdk-metrics-base): distinguish between Sum and Gauge in MetricData * feat(sdk-metrics-base): fix test. * fix(changelog): add changelog entry. * feat(prometheus-serializer): add test for gauge serialization. * feat(sdk-metrics-base): add assertions for isMonotonic in Instrument tests. * feat(otlp-transformer): add test-cases for non-monotonic sums. * feat(prometheus-exporter): add test-cases for gauges. * fix(sdk-metrics-base): remove DataPointType.SINGULAR * fix(changelog): add more details about breaking changes. --- experimental/CHANGELOG.md | 3 + .../src/PrometheusSerializer.ts | 26 ++-- .../test/PrometheusSerializer.test.ts | 121 +++++++++++++-- .../src/aggregator/LastValue.ts | 8 +- .../src/aggregator/Sum.ts | 9 +- .../src/export/MetricData.ts | 25 +++- .../test/Instruments.test.ts | 39 +++-- .../test/MeterProvider.test.ts | 14 +- .../test/aggregator/LastValue.test.ts | 2 +- .../test/aggregator/Sum.test.ts | 3 +- .../test/state/AsyncMetricStorage.test.ts | 36 ++--- .../test/state/MeterSharedState.test.ts | 10 +- .../test/state/MetricCollector.test.ts | 34 ++--- .../test/state/SyncMetricStorage.test.ts | 12 +- .../state/TemporalMetricProcessor.test.ts | 20 +-- .../otlp-transformer/src/metrics/internal.ts | 46 ++---- .../otlp-transformer/test/metrics.test.ts | 140 +++++++++++++++++- 17 files changed, 391 insertions(+), 157 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 3e7a1bc319..885a4ec292 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -7,6 +7,9 @@ All notable changes to experimental packages in this project will be documented ### :boom: Breaking Change * feature(views): move views registration to MeterProvider constructor [#3066](https://github.com/open-telemetry/opentelemetry-js/pull/3066) @pichlermarc +* feat(sdk-metrics-base): split up Singular into Sum and Gauge in MetricData [#3079](https://github.com/open-telemetry/opentelemetry-js/pull/3079) @pichlermarc + * removes `DataPointType.SINGULAR`, and replaces it with `DataPointType.SUM` and `DataPointType.GAUGE` + * removes `SingularMetricData` and replaces it with `SumMetricData` (including an additional `isMonotonic` flag) and `GaugeMetricData` ### :rocket: (Enhancement) diff --git a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index 1b84efe08b..c11ea5213c 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -104,23 +104,15 @@ function valueString(value: number) { } function toPrometheusType( - instrumentType: InstrumentType, - dataPointType: DataPointType, + metricData: MetricData, ): PrometheusDataTypeLiteral { - switch (dataPointType) { - case DataPointType.SINGULAR: - if ( - instrumentType === InstrumentType.COUNTER || - instrumentType === InstrumentType.OBSERVABLE_COUNTER - ) { + switch (metricData.dataPointType) { + case DataPointType.SUM: + if (metricData.isMonotonic) { return 'counter'; } - /** - * - HISTOGRAM - * - UP_DOWN_COUNTER - * - OBSERVABLE_GAUGE - * - OBSERVABLE_UP_DOWN_COUNTER - */ + return 'gauge'; + case DataPointType.GAUGE: return 'gauge'; case DataPointType.HISTOGRAM: return 'histogram'; @@ -210,13 +202,13 @@ export class PrometheusSerializer { metricData.descriptor.description || 'description missing' )}`; const type = `# TYPE ${name} ${toPrometheusType( - metricData.descriptor.type, - dataPointType + metricData )}`; let results = ''; switch (dataPointType) { - case DataPointType.SINGULAR: { + case DataPointType.SUM: + case DataPointType.GAUGE: { results = metricData.dataPoints .map(it => this._serializeSingularDataPoint(name, metricData.descriptor.type, it)) .join(''); diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index c8ca61e237..daea0b209f 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -18,13 +18,14 @@ import * as assert from 'assert'; import { MetricAttributes, UpDownCounter } from '@opentelemetry/api-metrics'; import { AggregationTemporality, - MeterProvider, - MetricReader, DataPoint, DataPointType, ExplicitBucketHistogramAggregation, - SumAggregation, Histogram, + LastValueAggregation, + MeterProvider, + MetricReader, + SumAggregation, View, } from '@opentelemetry/sdk-metrics-base'; import * as sinon from 'sinon'; @@ -85,7 +86,7 @@ describe('PrometheusSerializer', () => { assert.strictEqual(resourceMetrics.scopeMetrics.length, 1); assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); const metric = resourceMetrics.scopeMetrics[0].metrics[0]; - assert.strictEqual(metric.dataPointType, DataPointType.SINGULAR); + assert.strictEqual(metric.dataPointType, DataPointType.SUM); const pointData = metric.dataPoints as DataPoint[]; assert.strictEqual(pointData.length, 1); @@ -171,7 +172,7 @@ describe('PrometheusSerializer', () => { }); describe('serialize an instrumentation metrics', () => { - describe('Singular', () => { + describe('monotonic Sum', () => { async function testSerializer(serializer: PrometheusSerializer) { const reader = new TestMetricReader(); const meterProvider = new MeterProvider({ @@ -196,7 +197,7 @@ describe('PrometheusSerializer', () => { return result; } - it('should serialize metric record with sum aggregator', async () => { + it('should serialize metric record', async () => { const serializer = new PrometheusSerializer(); const result = await testSerializer(serializer); assert.strictEqual( @@ -208,7 +209,7 @@ describe('PrometheusSerializer', () => { ); }); - it('serialize metric record with sum aggregator without timestamp', async () => { + it('should serialize metric record without timestamp', async () => { const serializer = new PrometheusSerializer(undefined, false); const result = await testSerializer(serializer); assert.strictEqual( @@ -221,6 +222,108 @@ describe('PrometheusSerializer', () => { }); }); + describe('non-monotonic Sum', () => { + async function testSerializer(serializer: PrometheusSerializer) { + const reader = new TestMetricReader(); + const meterProvider = new MeterProvider({ + views: [ + new View({ aggregation: new SumAggregation(), instrumentName: '*' }) + ] + }); + meterProvider.addMetricReader(reader); + const meter = meterProvider.getMeter('test'); + + const counter = meter.createUpDownCounter('test_total', { + description: 'foobar', + }); + counter.add(1, { val: '1' }); + counter.add(1, { val: '2' }); + + const { resourceMetrics, errors } = await reader.collect(); + assert.strictEqual(errors.length, 0); + assert.strictEqual(resourceMetrics.scopeMetrics.length, 1); + assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); + const scopeMetrics = resourceMetrics.scopeMetrics[0]; + + return serializer['_serializeScopeMetrics'](scopeMetrics); + } + + it('should serialize metric record', async () => { + const serializer = new PrometheusSerializer(); + const result = await testSerializer(serializer); + assert.strictEqual( + result, + '# HELP test_total foobar\n' + + '# TYPE test_total gauge\n' + + `test_total{val="1"} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2"} 1 ${mockedHrTimeMs}\n` + ); + }); + + it('serialize metric record without timestamp', async () => { + const serializer = new PrometheusSerializer(undefined, false); + const result = await testSerializer(serializer); + assert.strictEqual( + result, + '# HELP test_total foobar\n' + + '# TYPE test_total gauge\n' + + 'test_total{val="1"} 1\n' + + 'test_total{val="2"} 1\n' + ); + }); + }); + + describe('Gauge', () => { + async function testSerializer(serializer: PrometheusSerializer) { + const reader = new TestMetricReader(); + const meterProvider = new MeterProvider({ + views: [ + new View({aggregation: new LastValueAggregation(), instrumentName: '*' }) + ] + }); + meterProvider.addMetricReader(reader); + const meter = meterProvider.getMeter('test'); + + const counter = meter.createUpDownCounter('test_total', { + description: 'foobar', + }); + counter.add(1, { val: '1' }); + counter.add(1, { val: '2' }); + + const { resourceMetrics, errors } = await reader.collect(); + assert.strictEqual(errors.length, 0); + assert.strictEqual(resourceMetrics.scopeMetrics.length, 1); + assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); + const scopeMetrics = resourceMetrics.scopeMetrics[0]; + + return serializer['_serializeScopeMetrics'](scopeMetrics); + } + + it('should serialize metric record', async () => { + const serializer = new PrometheusSerializer(); + const result = await testSerializer(serializer); + assert.strictEqual( + result, + '# HELP test_total foobar\n' + + '# TYPE test_total gauge\n' + + `test_total{val="1"} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2"} 1 ${mockedHrTimeMs}\n` + ); + }); + + it('serialize metric record without timestamp', async () => { + const serializer = new PrometheusSerializer(undefined, false); + const result = await testSerializer(serializer); + assert.strictEqual( + result, + '# HELP test_total foobar\n' + + '# TYPE test_total gauge\n' + + 'test_total{val="1"} 1\n' + + 'test_total{val="2"} 1\n' + ); + }); + }); + describe('with ExplicitBucketHistogramAggregation', () => { async function testSerializer(serializer: PrometheusSerializer) { const reader = new TestMetricReader(); @@ -293,7 +396,7 @@ describe('PrometheusSerializer', () => { assert.strictEqual(resourceMetrics.scopeMetrics.length, 1); assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); const metric = resourceMetrics.scopeMetrics[0].metrics[0]; - assert.strictEqual(metric.dataPointType, DataPointType.SINGULAR); + assert.strictEqual(metric.dataPointType, DataPointType.SUM); const pointData = metric.dataPoints as DataPoint[]; assert.strictEqual(pointData.length, 1); @@ -333,7 +436,7 @@ describe('PrometheusSerializer', () => { assert.strictEqual(resourceMetrics.scopeMetrics.length, 1); assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); const metric = resourceMetrics.scopeMetrics[0].metrics[0]; - assert.strictEqual(metric.dataPointType, DataPointType.SINGULAR); + assert.strictEqual(metric.dataPointType, DataPointType.SUM); const pointData = metric.dataPoints as DataPoint[]; assert.strictEqual(pointData.length, 1); diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/LastValue.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/LastValue.ts index 0905b03daf..dfdee59b65 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/LastValue.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/LastValue.ts @@ -14,10 +14,10 @@ * limitations under the License. */ -import { LastValue, AggregatorKind, Aggregator, Accumulation, AccumulationRecord } from './types'; +import { Accumulation, AccumulationRecord, Aggregator, AggregatorKind, LastValue } from './types'; import { HrTime } from '@opentelemetry/api'; import { hrTime, hrTimeToMicroseconds } from '@opentelemetry/core'; -import { DataPointType, SingularMetricData } from '../export/MetricData'; +import { DataPointType, GaugeMetricData } from '../export/MetricData'; import { InstrumentDescriptor } from '../InstrumentDescriptor'; import { Maybe } from '../utils'; import { AggregationTemporality } from '../export/AggregationTemporality'; @@ -74,11 +74,11 @@ export class LastValueAggregator implements Aggregator { descriptor: InstrumentDescriptor, aggregationTemporality: AggregationTemporality, accumulationByAttributes: AccumulationRecord[], - endTime: HrTime): Maybe { + endTime: HrTime): Maybe { return { descriptor, aggregationTemporality, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.GAUGE, dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => { return { attributes, diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts index 1482841106..f5e18d796f 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts @@ -16,7 +16,7 @@ import { Sum, AggregatorKind, Aggregator, Accumulation, AccumulationRecord } from './types'; import { HrTime } from '@opentelemetry/api'; -import { DataPointType, SingularMetricData } from '../export/MetricData'; +import { DataPointType, SumMetricData } from '../export/MetricData'; import { InstrumentDescriptor } from '../InstrumentDescriptor'; import { Maybe } from '../utils'; import { AggregationTemporality } from '../export/AggregationTemporality'; @@ -83,11 +83,11 @@ export class SumAggregator implements Aggregator { descriptor: InstrumentDescriptor, aggregationTemporality: AggregationTemporality, accumulationByAttributes: AccumulationRecord[], - endTime: HrTime): Maybe { + endTime: HrTime): Maybe { return { descriptor, aggregationTemporality, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => { return { attributes, @@ -95,7 +95,8 @@ export class SumAggregator implements Aggregator { endTime, value: accumulation.toPointValue(), }; - }) + }), + isMonotonic: this.monotonic }; } } diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts index 74efb96531..0522f10dea 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts @@ -38,8 +38,14 @@ export interface BaseMetricData { * Represents a metric data aggregated by either a LastValueAggregation or * SumAggregation. */ -export interface SingularMetricData extends BaseMetricData { - readonly dataPointType: DataPointType.SINGULAR; +export interface SumMetricData extends BaseMetricData { + readonly dataPointType: DataPointType.SUM; + readonly dataPoints: DataPoint[]; + readonly isMonotonic: boolean; +} + +export interface GaugeMetricData extends BaseMetricData { + readonly dataPointType: DataPointType.GAUGE; readonly dataPoints: DataPoint[]; } @@ -54,7 +60,7 @@ export interface HistogramMetricData extends BaseMetricData { /** * Represents an aggregated metric data. */ -export type MetricData = SingularMetricData | HistogramMetricData; +export type MetricData = SumMetricData | GaugeMetricData | HistogramMetricData; export interface ScopeMetrics { scope: InstrumentationScope; @@ -87,10 +93,6 @@ export interface CollectionResult { * The aggregated point data type. */ export enum DataPointType { - /** - * A singular metric data point has only a single numeric value. - */ - SINGULAR, /** * A histogram data point contains a histogram statistics of collected * values with a list of explicit bucket boundaries and statistics such @@ -104,6 +106,15 @@ export enum DataPointType { * and sum of all collected values. */ EXPONENTIAL_HISTOGRAM, + /** + * A gauge metric data point has only a single numeric value. + */ + GAUGE, + /** + * A sum metric data point has a single numeric value and a + * monotonicity-indicator. + */ + SUM } /** diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/Instruments.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/Instruments.test.ts index bac7776c67..bf4c496084 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/Instruments.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/Instruments.test.ts @@ -83,7 +83,8 @@ describe('Instruments', () => { type: InstrumentType.COUNTER, valueType: ValueType.INT, }, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: true, dataPoints: [ { attributes: {}, @@ -99,7 +100,8 @@ describe('Instruments', () => { // add negative values should not be observable. counter.add(-1.1); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: true, dataPoints: [ { attributes: {}, @@ -125,7 +127,8 @@ describe('Instruments', () => { counter.add(1, { foo: 'bar' }); counter.add(1.2, { foo: 'bar' }); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: true, dataPoints: [ { attributes: {}, @@ -141,7 +144,8 @@ describe('Instruments', () => { // add negative values should not be observable. counter.add(-1.1); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: true, dataPoints: [ { attributes: {}, @@ -199,7 +203,8 @@ describe('Instruments', () => { type: InstrumentType.UP_DOWN_COUNTER, valueType: ValueType.INT, }, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: false, dataPoints: [ { attributes: {}, @@ -224,7 +229,8 @@ describe('Instruments', () => { upDownCounter.add(4, { foo: 'bar' }); upDownCounter.add(1.1, { foo: 'bar' }); await validateExport(deltaReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: false, dataPoints: [ { attributes: {}, @@ -488,7 +494,8 @@ describe('Instruments', () => { }); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: false, dataPoints: [ { attributes: {}, @@ -501,7 +508,8 @@ describe('Instruments', () => { ], }); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: false, dataPoints: [ { attributes: {}, @@ -543,7 +551,8 @@ describe('Instruments', () => { }); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: false, dataPoints: [ { attributes: {}, @@ -556,7 +565,8 @@ describe('Instruments', () => { ], }); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: false, dataPoints: [ { attributes: {}, @@ -603,7 +613,7 @@ describe('Instruments', () => { }); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.GAUGE, dataPoints: [ { attributes: {}, @@ -616,7 +626,7 @@ describe('Instruments', () => { ], }); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.GAUGE, dataPoints: [ { attributes: {}, @@ -629,7 +639,7 @@ describe('Instruments', () => { ], }); await validateExport(cumulativeReader, { - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.GAUGE, dataPoints: [ { attributes: {}, @@ -667,8 +677,9 @@ interface ValidateMetricData { resource?: Resource; instrumentationScope?: InstrumentationScope; descriptor?: InstrumentDescriptor; - dataPointType?: DataPointType, + dataPointType?: DataPointType; dataPoints?: Partial>>[]; + isMonotonic?: boolean; } async function validateExport(reader: MetricReader, expected: ValidateMetricData) { diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/MeterProvider.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/MeterProvider.test.ts index 0a2e474b45..3d7dc68c6a 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/MeterProvider.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/MeterProvider.test.ts @@ -151,7 +151,7 @@ describe('MeterProvider', () => { assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); // View updated name and description. - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SUM, { name: 'renamed-instrument', type: InstrumentType.COUNTER, description: 'my renamed instrument' @@ -210,7 +210,7 @@ describe('MeterProvider', () => { assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); // View updated name and description. - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SUM, { name: 'non-renamed-instrument', type: InstrumentType.COUNTER, }); @@ -270,7 +270,7 @@ describe('MeterProvider', () => { assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); // View updated the name to 'renamed-instrument' and instrument is still a Counter - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SUM, { name: 'renamed-instrument', type: InstrumentType.COUNTER, }); @@ -285,7 +285,7 @@ describe('MeterProvider', () => { assert.strictEqual(resourceMetrics.scopeMetrics[1].metrics.length, 1); // View updated the name to 'renamed-instrument' and instrument is still a Counter - assertMetricData(resourceMetrics.scopeMetrics[1].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[1].metrics[0], DataPointType.SUM, { name: 'renamed-instrument', type: InstrumentType.COUNTER }); @@ -336,7 +336,7 @@ describe('MeterProvider', () => { assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); // View updated the name to 'renamed-instrument' and instrument is still a Counter - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SUM, { name: 'renamed-instrument', type: InstrumentType.COUNTER }); @@ -351,7 +351,7 @@ describe('MeterProvider', () => { assert.strictEqual(resourceMetrics.scopeMetrics[1].metrics.length, 1); // No updated name on 'test-counter'. - assertMetricData(resourceMetrics.scopeMetrics[1].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[1].metrics[0], DataPointType.SUM, { name: 'test-counter', type: InstrumentType.COUNTER }); @@ -403,7 +403,7 @@ describe('MeterProvider', () => { assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 2); // Both 'renamed-instrument' are still exported with their types. - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SUM, { name: 'renamed-instrument', type: InstrumentType.COUNTER }); diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/LastValue.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/LastValue.test.ts index 420cabde65..b309af6b6c 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/LastValue.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/LastValue.test.ts @@ -114,7 +114,7 @@ describe('LastValueAggregator', () => { const expected: MetricData = { descriptor: defaultInstrumentDescriptor, aggregationTemporality: AggregationTemporality.CUMULATIVE, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.GAUGE, dataPoints: [ { attributes: {}, diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/Sum.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/Sum.test.ts index ac3df809a6..67034ba6ca 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/Sum.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/aggregator/Sum.test.ts @@ -93,7 +93,8 @@ describe('SumAggregator', () => { const expected: MetricData = { descriptor: defaultInstrumentDescriptor, aggregationTemporality: AggregationTemporality.CUMULATIVE, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: true, dataPoints: [ { attributes: {}, diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/AsyncMetricStorage.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/AsyncMetricStorage.test.ts index dc0e7fe072..0500d012e8 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/AsyncMetricStorage.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/AsyncMetricStorage.test.ts @@ -68,7 +68,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 3); assertDataPoint(metric.dataPoints[0], { key: '1' }, 1, collectionTime, collectionTime); assertDataPoint(metric.dataPoints[1], { key: '2' }, 2, collectionTime, collectionTime); @@ -85,7 +85,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 0); } @@ -102,7 +102,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 3); // All values were diffed. StartTime is being reset for gaps. assertDataPoint(metric.dataPoints[0], { key: '1' }, 3, collectionTime, collectionTime); @@ -140,7 +140,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 100, collectionTime, collectionTime); lastCollectionTime = collectionTime; @@ -159,7 +159,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 1, lastCollectionTime, collectionTime); lastCollectionTime = collectionTime; @@ -178,7 +178,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 49, lastCollectionTime, collectionTime); } @@ -213,7 +213,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 100, collectionTime, collectionTime); lastCollectionTime = collectionTime; @@ -232,7 +232,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, -99, lastCollectionTime, collectionTime); lastCollectionTime = collectionTime; @@ -252,7 +252,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 49, lastCollectionTime, collectionTime); } @@ -292,7 +292,7 @@ describe('AsyncMetricStorage', () => { collectionTime); startTime = collectionTime; - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 3); assertDataPoint(metric.dataPoints[0], { key: '1' }, 1, startTime, collectionTime); assertDataPoint(metric.dataPoints[1], { key: '2' }, 2, startTime, collectionTime); @@ -309,7 +309,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 3); assertDataPoint(metric.dataPoints[0], { key: '1' }, 1, startTime, collectionTime); assertDataPoint(metric.dataPoints[1], { key: '2' }, 2, startTime, collectionTime); @@ -329,7 +329,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 3); assertDataPoint(metric.dataPoints[0], { key: '1' }, 4, startTime, collectionTime); assertDataPoint(metric.dataPoints[1], { key: '2' }, 5, startTime, collectionTime); @@ -367,7 +367,7 @@ describe('AsyncMetricStorage', () => { collectionTime); startTime = collectionTime; - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 100, startTime, collectionTime); } @@ -385,7 +385,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); // The startTime should be reset. assertDataPoint(metric.dataPoints[0], { key: '1' }, 1, collectionTime, collectionTime); @@ -405,7 +405,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 50, startTime, collectionTime); } @@ -441,7 +441,7 @@ describe('AsyncMetricStorage', () => { collectionTime); startTime = collectionTime; - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 100, startTime, collectionTime); } @@ -459,7 +459,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); // No reset on the value or the startTime assertDataPoint(metric.dataPoints[0], { key: '1' }, 1, startTime, collectionTime); @@ -478,7 +478,7 @@ describe('AsyncMetricStorage', () => { collectors, collectionTime); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], { key: '1' }, 50, startTime, collectionTime); } diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/MeterSharedState.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/MeterSharedState.test.ts index a332203f0c..bacbe87e3f 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/MeterSharedState.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/MeterSharedState.test.ts @@ -69,7 +69,7 @@ describe('MeterSharedState', () => { assert.strictEqual(errors.length, 0); assert.strictEqual(resourceMetrics.scopeMetrics.length, 1); assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SUM, { name: 'test', }); })); @@ -92,10 +92,10 @@ describe('MeterSharedState', () => { assert.strictEqual(errors.length, 0); assert.strictEqual(resourceMetrics.scopeMetrics.length, 1); assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 2); - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SUM, { name: 'foo', }); - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[1], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[1], DataPointType.SUM, { name: 'bar', }); })); @@ -157,10 +157,10 @@ describe('MeterSharedState', () => { assert.strictEqual(errors.length, 0); assert.strictEqual(resourceMetrics.scopeMetrics.length, 1); assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 2); - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[0], DataPointType.SUM, { name: 'foo' }); - assertMetricData(resourceMetrics.scopeMetrics[0].metrics[1], DataPointType.SINGULAR, { + assertMetricData(resourceMetrics.scopeMetrics[0].metrics[1], DataPointType.SUM, { name: 'bar' }); } diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/MetricCollector.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/MetricCollector.test.ts index 2070eb2e41..77047d9f52 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/MetricCollector.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/MetricCollector.test.ts @@ -86,7 +86,7 @@ describe('MetricCollector', () => { /** checking batch[0] */ const metricData1 = metrics[0]; - assertMetricData(metricData1, DataPointType.SINGULAR, { + assertMetricData(metricData1, DataPointType.SUM, { name: 'counter1' }); assert.strictEqual(metricData1.dataPoints.length, 2); @@ -95,7 +95,7 @@ describe('MetricCollector', () => { /** checking batch[1] */ const metricData2 = metrics[1]; - assertMetricData(metricData2, DataPointType.SINGULAR, { + assertMetricData(metricData2, DataPointType.SUM, { name: 'counter2' }); assert.strictEqual(metricData2.dataPoints.length, 1); @@ -136,7 +136,7 @@ describe('MetricCollector', () => { /** checking batch[0] */ const metricData1 = metrics[0]; - assertMetricData(metricData1, DataPointType.SINGULAR, { + assertMetricData(metricData1, DataPointType.SUM, { name: 'observable1' }); assert.strictEqual(metricData1.dataPoints.length, 2); @@ -145,7 +145,7 @@ describe('MetricCollector', () => { /** checking batch[1] */ const metricData2 = metrics[1]; - assertMetricData(metricData2, DataPointType.SINGULAR, { + assertMetricData(metricData2, DataPointType.SUM, { name: 'observable2' }); assert.strictEqual(metricData2.dataPoints.length, 2); @@ -154,7 +154,7 @@ describe('MetricCollector', () => { /** checking batch[2] */ const metricData3 = metrics[2]; - assertMetricData(metricData3, DataPointType.SINGULAR, { + assertMetricData(metricData3, DataPointType.SUM, { name: 'observable3' }); assert.strictEqual(metricData3.dataPoints.length, 0); @@ -200,13 +200,13 @@ describe('MetricCollector', () => { assert.strictEqual(metrics.length, 2); /** observer1 */ - assertMetricData(metrics[0], DataPointType.SINGULAR, { + assertMetricData(metrics[0], DataPointType.SUM, { name: 'observer1' }); assert.strictEqual(metrics[0].dataPoints.length, 0); /** observer2 */ - assertMetricData(metrics[1], DataPointType.SINGULAR, { + assertMetricData(metrics[1], DataPointType.SUM, { name: 'observer2' }); assert.strictEqual(metrics[1].dataPoints.length, 1); @@ -230,14 +230,14 @@ describe('MetricCollector', () => { assert.strictEqual(metrics.length, 2); /** observer1 */ - assertMetricData(metrics[0], DataPointType.SINGULAR, { + assertMetricData(metrics[0], DataPointType.SUM, { name: 'observer1' }); assert.strictEqual(metrics[0].dataPoints.length, 1); assertDataPoint(metrics[0].dataPoints[0], {}, 100); /** observer2 */ - assertMetricData(metrics[1], DataPointType.SINGULAR, { + assertMetricData(metrics[1], DataPointType.SUM, { name: 'observer2' }); assert.strictEqual(metrics[1].dataPoints.length, 1); @@ -268,13 +268,13 @@ describe('MetricCollector', () => { assert.strictEqual(metrics.length, 2); /** counter1 data points are collected */ - assertMetricData(metrics[0], DataPointType.SINGULAR, { + assertMetricData(metrics[0], DataPointType.SUM, { name: 'counter1' }); assert.strictEqual(metrics[0].dataPoints.length, 1); /** observer1 data points are not collected */ - assertMetricData(metrics[1], DataPointType.SINGULAR, { + assertMetricData(metrics[1], DataPointType.SUM, { name: 'observer1' }); assert.strictEqual(metrics[1].dataPoints.length, 0); @@ -320,13 +320,13 @@ describe('MetricCollector', () => { assert.strictEqual(metrics.length, 2); /** observer1 */ - assertMetricData(metrics[0], DataPointType.SINGULAR, { + assertMetricData(metrics[0], DataPointType.SUM, { name: 'observer1' }); assert.strictEqual(metrics[0].dataPoints.length, 0); /** observer2 */ - assertMetricData(metrics[1], DataPointType.SINGULAR, { + assertMetricData(metrics[1], DataPointType.SUM, { name: 'observer2' }); assert.strictEqual(metrics[1].dataPoints.length, 1); @@ -350,14 +350,14 @@ describe('MetricCollector', () => { assert.strictEqual(metrics.length, 2); /** observer1 */ - assertMetricData(metrics[0], DataPointType.SINGULAR, { + assertMetricData(metrics[0], DataPointType.SUM, { name: 'observer1' }); assert.strictEqual(metrics[0].dataPoints.length, 1); assertDataPoint(metrics[0].dataPoints[0], {}, 100); /** observer2 */ - assertMetricData(metrics[1], DataPointType.SINGULAR, { + assertMetricData(metrics[1], DataPointType.SUM, { name: 'observer2' }); assert.strictEqual(metrics[1].dataPoints.length, 1); @@ -390,13 +390,13 @@ describe('MetricCollector', () => { assert.strictEqual(metrics.length, 2); /** counter1 data points are collected */ - assertMetricData(metrics[0], DataPointType.SINGULAR, { + assertMetricData(metrics[0], DataPointType.SUM, { name: 'counter1' }); assert.strictEqual(metrics[0].dataPoints.length, 1); /** observer1 data points are not collected */ - assertMetricData(metrics[1], DataPointType.SINGULAR, { + assertMetricData(metrics[1], DataPointType.SUM, { name: 'observer1' }); assert.strictEqual(metrics[1].dataPoints.length, 0); diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/SyncMetricStorage.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/SyncMetricStorage.test.ts index 1a95d4c12a..13f0b91492 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/SyncMetricStorage.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/SyncMetricStorage.test.ts @@ -68,7 +68,7 @@ describe('SyncMetricStorage', () => { collectors, [3, 3]); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], {}, 6, [0, 0], [3, 3]); } @@ -80,7 +80,7 @@ describe('SyncMetricStorage', () => { collectors, [4, 4]); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 0); } @@ -91,7 +91,7 @@ describe('SyncMetricStorage', () => { [deltaCollector], [6, 6]); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], {}, 1, [5, 5], [6, 6]); } @@ -115,7 +115,7 @@ describe('SyncMetricStorage', () => { collectors, [3, 3]); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], {}, 6, [0, 0], [3, 3]); } @@ -127,7 +127,7 @@ describe('SyncMetricStorage', () => { collectors, [4, 4]); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], {}, 6, [0, 0], [4, 4]); } @@ -139,7 +139,7 @@ describe('SyncMetricStorage', () => { collectors, [6, 6]); - assertMetricData(metric, DataPointType.SINGULAR); + assertMetricData(metric, DataPointType.SUM); assert.strictEqual(metric.dataPoints.length, 1); assertDataPoint(metric.dataPoints[0], {}, 7, [0, 0], [6, 6]); } diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/TemporalMetricProcessor.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/TemporalMetricProcessor.test.ts index 5595d23f25..27bc2f1bbf 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/state/TemporalMetricProcessor.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/state/TemporalMetricProcessor.test.ts @@ -63,7 +63,7 @@ describe('TemporalMetricProcessor', () => { [2, 2]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.DELTA); assert.strictEqual(metric.dataPoints.length, 1); @@ -80,7 +80,7 @@ describe('TemporalMetricProcessor', () => { [4, 4]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.DELTA); assert.strictEqual(metric.dataPoints.length, 1); @@ -97,7 +97,7 @@ describe('TemporalMetricProcessor', () => { [5, 5]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.DELTA); assert.strictEqual(metric.dataPoints.length, 0); @@ -126,7 +126,7 @@ describe('TemporalMetricProcessor', () => { [2, 2]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.DELTA); assert.strictEqual(metric.dataPoints.length, 1); @@ -142,7 +142,7 @@ describe('TemporalMetricProcessor', () => { [3, 3]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.DELTA); assert.strictEqual(metric.dataPoints.length, 1); @@ -170,7 +170,7 @@ describe('TemporalMetricProcessor', () => { [2, 2]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.CUMULATIVE); assert.strictEqual(metric.dataPoints.length, 1); @@ -187,7 +187,7 @@ describe('TemporalMetricProcessor', () => { [4, 4]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.CUMULATIVE); assert.strictEqual(metric.dataPoints.length, 1); @@ -216,7 +216,7 @@ describe('TemporalMetricProcessor', () => { [2, 2]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.CUMULATIVE); assert.strictEqual(metric.dataPoints.length, 1); @@ -233,7 +233,7 @@ describe('TemporalMetricProcessor', () => { [4, 4]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.DELTA); assert.strictEqual(metric.dataPoints.length, 1); @@ -248,7 +248,7 @@ describe('TemporalMetricProcessor', () => { [5, 5]); assertMetricData(metric, - DataPointType.SINGULAR, + DataPointType.SUM, defaultInstrumentDescriptor, AggregationTemporality.CUMULATIVE); assert.strictEqual(metric.dataPoints.length, 1); diff --git a/experimental/packages/otlp-transformer/src/metrics/internal.ts b/experimental/packages/otlp-transformer/src/metrics/internal.ts index e1319a6b17..8c8f89b04c 100644 --- a/experimental/packages/otlp-transformer/src/metrics/internal.ts +++ b/experimental/packages/otlp-transformer/src/metrics/internal.ts @@ -20,10 +20,9 @@ import { DataPoint, DataPointType, Histogram, - ScopeMetrics, - InstrumentType, MetricData, - ResourceMetrics + ResourceMetrics, + ScopeMetrics } from '@opentelemetry/sdk-metrics-base'; import { toAttributes } from '../common/internal'; import { @@ -69,24 +68,18 @@ export function toMetric(metricData: MetricData): IMetric { const aggregationTemporality = toAggregationTemporality(metricData.aggregationTemporality); - if (metricData.dataPointType === DataPointType.SINGULAR) { - const dataPoints = toSingularDataPoints(metricData); - const isMonotonic = metricData.descriptor.type === InstrumentType.COUNTER || - metricData.descriptor.type === InstrumentType.OBSERVABLE_COUNTER; - if (isSum(metricData)) { - out.sum = { - aggregationTemporality, - isMonotonic, - dataPoints - }; - - } else { - // Instrument is a gauge. - out.gauge = { - dataPoints - }; - } - } else if (isHistogram(metricData)) { + if (metricData.dataPointType === DataPointType.SUM) { + out.sum = { + aggregationTemporality, + isMonotonic: metricData.isMonotonic, + dataPoints: toSingularDataPoints(metricData) + }; + } else if (metricData.dataPointType === DataPointType.GAUGE) { + // Instrument is a gauge. + out.gauge = { + dataPoints: toSingularDataPoints(metricData) + }; + } else if (metricData.dataPointType === DataPointType.HISTOGRAM) { out.histogram = { aggregationTemporality, dataPoints: toHistogramDataPoints(metricData) @@ -145,17 +138,6 @@ function toHistogramDataPoints( }); } -function isSum(metric: MetricData) { - return (metric.descriptor.type === InstrumentType.COUNTER || - metric.descriptor.type === InstrumentType.UP_DOWN_COUNTER || - metric.descriptor.type === InstrumentType.OBSERVABLE_COUNTER || - metric.descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); -} - -function isHistogram(metric: MetricData) { - return metric.dataPointType === DataPointType.HISTOGRAM; -} - function toAggregationTemporality( temporality: AggregationTemporality, ): EAggregationTemporality { diff --git a/experimental/packages/otlp-transformer/test/metrics.test.ts b/experimental/packages/otlp-transformer/test/metrics.test.ts index e9047b3f0c..beb9785f70 100644 --- a/experimental/packages/otlp-transformer/test/metrics.test.ts +++ b/experimental/packages/otlp-transformer/test/metrics.test.ts @@ -70,7 +70,31 @@ describe('Metrics', () => { valueType: ValueType.INT, }, aggregationTemporality, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: true, + dataPoints: [ + { + value: value, + startTime: START_TIME, + endTime: END_TIME, + attributes: { 'string-attribute': 'some attribute value' } + } + ] + }; + } + + function createUpDownCounterData(value: number, aggregationTemporality: AggregationTemporality): MetricData { + return { + descriptor: { + description: 'this is a description', + type: InstrumentType.UP_DOWN_COUNTER, + name: 'up-down-counter', + unit: '1', + valueType: ValueType.INT, + }, + aggregationTemporality, + dataPointType: DataPointType.SUM, + isMonotonic: false, dataPoints: [ { value: value, @@ -92,7 +116,8 @@ describe('Metrics', () => { valueType: ValueType.INT, }, aggregationTemporality, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.SUM, + isMonotonic: true, dataPoints: [ { value: value, @@ -104,6 +129,30 @@ describe('Metrics', () => { }; } + function createObservableUpDownCounterData(value: number, aggregationTemporality: AggregationTemporality): MetricData { + return { + descriptor: { + description: 'this is a description', + type: InstrumentType.OBSERVABLE_UP_DOWN_COUNTER, + name: 'observable-up-down-counter', + unit: '1', + valueType: ValueType.INT, + }, + aggregationTemporality, + dataPointType: DataPointType.SUM, + isMonotonic: false, + dataPoints: [ + { + value: value, + startTime: START_TIME, + endTime: END_TIME, + attributes: { 'string-attribute': 'some attribute value' } + } + ] + }; + } + + function createObservableGaugeData(value: number): MetricData { return { descriptor: { @@ -114,7 +163,7 @@ describe('Metrics', () => { valueType: ValueType.DOUBLE, }, aggregationTemporality: AggregationTemporality.CUMULATIVE, - dataPointType: DataPointType.SINGULAR, + dataPointType: DataPointType.GAUGE, dataPoints: [ { value: value, @@ -184,7 +233,7 @@ describe('Metrics', () => { }; } - it('serializes a sum metric record', () => { + it('serializes a monotonic sum metric record', () => { const metrics = createResourceMetrics([createCounterData(10, AggregationTemporality.DELTA)]); const exportRequest = createExportMetricsServiceRequest([metrics]); assert.ok(exportRequest); @@ -224,7 +273,47 @@ describe('Metrics', () => { }); }); - it('serializes an observable sum metric record', () => { + it('serializes a non-monotonic sum metric record', () => { + const metrics = createResourceMetrics([createUpDownCounterData(10, AggregationTemporality.DELTA)]); + const exportRequest = createExportMetricsServiceRequest([metrics]); + assert.ok(exportRequest); + + assert.deepStrictEqual(exportRequest, { + resourceMetrics: [ + { + resource: expectedResource, + schemaUrl: undefined, + scopeMetrics: [ + { + scope: expectedScope, + schemaUrl: expectedSchemaUrl, + metrics: [ + { + name: 'up-down-counter', + description: 'this is a description', + unit: '1', + sum: { + dataPoints: [ + { + attributes: expectedAttributes, + startTimeUnixNano: hrTimeToNanoseconds(START_TIME), + timeUnixNano: hrTimeToNanoseconds(END_TIME), + asInt: 10, + }, + ], + aggregationTemporality: EAggregationTemporality.AGGREGATION_TEMPORALITY_DELTA, + isMonotonic: false, + }, + }, + ], + }, + ], + }, + ], + }); + }); + + it('serializes an observable monotonic sum metric record', () => { const exportRequest = createExportMetricsServiceRequest( [createResourceMetrics([createObservableCounterData(10, AggregationTemporality.DELTA)])] ); @@ -265,6 +354,47 @@ describe('Metrics', () => { }); }); + it('serializes an observable non-monotonic sum metric record', () => { + const exportRequest = createExportMetricsServiceRequest( + [createResourceMetrics([createObservableUpDownCounterData(10, AggregationTemporality.DELTA)])] + ); + assert.ok(exportRequest); + + assert.deepStrictEqual(exportRequest, { + resourceMetrics: [ + { + resource: expectedResource, + schemaUrl: undefined, + scopeMetrics: [ + { + scope: expectedScope, + schemaUrl: expectedSchemaUrl, + metrics: [ + { + name: 'observable-up-down-counter', + description: 'this is a description', + unit: '1', + sum: { + dataPoints: [ + { + attributes: expectedAttributes, + startTimeUnixNano: hrTimeToNanoseconds(START_TIME), + timeUnixNano: hrTimeToNanoseconds(END_TIME), + asInt: 10, + }, + ], + aggregationTemporality: EAggregationTemporality.AGGREGATION_TEMPORALITY_DELTA, + isMonotonic: false, + }, + }, + ], + }, + ], + }, + ], + }); + }); + it('serializes a gauge metric record', () => { const exportRequest = createExportMetricsServiceRequest( [createResourceMetrics([createObservableGaugeData(10.5)])]