Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk-metrics-base): distinguish between Sum and Gauge in MetricData #3079

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to experimental packages in this project will be documented

### :boom: Breaking Change

* feat(sdk-metrics-base): distinguish between Sum and Gauge in MetricData [#3079](/~https://github.com/open-telemetry/opentelemetry-js/pull/3079) @pichlermarc
dyladan marked this conversation as resolved.
Show resolved Hide resolved

### :rocket: (Enhancement)

### :bug: (Bug Fix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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<number>[];
assert.strictEqual(pointData.length, 1);

Expand Down Expand Up @@ -164,7 +164,7 @@ describe('PrometheusSerializer', () => {
});

describe('serialize an instrumentation metrics', () => {
describe('Singular', () => {
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
describe('Sum', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
Expand Down Expand Up @@ -280,7 +280,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<number>[];
assert.strictEqual(pointData.length, 1);

Expand Down Expand Up @@ -319,7 +319,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<number>[];
assert.strictEqual(pointData.length, 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -74,11 +74,11 @@ export class LastValueAggregator implements Aggregator<LastValueAccumulation> {
descriptor: InstrumentDescriptor,
aggregationTemporality: AggregationTemporality,
accumulationByAttributes: AccumulationRecord<LastValueAccumulation>[],
endTime: HrTime): Maybe<SingularMetricData> {
endTime: HrTime): Maybe<GaugeMetricData> {
return {
descriptor,
aggregationTemporality,
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.GAUGE,
dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => {
return {
attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -83,19 +83,20 @@ export class SumAggregator implements Aggregator<SumAccumulation> {
descriptor: InstrumentDescriptor,
aggregationTemporality: AggregationTemporality,
accumulationByAttributes: AccumulationRecord<SumAccumulation>[],
endTime: HrTime): Maybe<SingularMetricData> {
endTime: HrTime): Maybe<SumMetricData> {
return {
descriptor,
aggregationTemporality,
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => {
return {
attributes,
startTime: accumulation.startTime,
endTime,
value: accumulation.toPointValue(),
};
})
}),
isMonotonic: this.monotonic
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>[];
readonly isMonotonic: boolean;
}

export interface GaugeMetricData extends BaseMetricData {
readonly dataPointType: DataPointType.GAUGE;
readonly dataPoints: DataPoint<number>[];
}

Expand All @@ -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;
Expand All @@ -75,9 +81,10 @@ export interface CollectionResult {
* The aggregated point data type.
*/
export enum DataPointType {
SINGULAR,
HISTOGRAM,
EXPONENTIAL_HISTOGRAM,
GAUGE,
SUM
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('Instruments', () => {
type: InstrumentType.COUNTER,
valueType: ValueType.INT,
},
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand All @@ -99,7 +99,7 @@ describe('Instruments', () => {
// add negative values should not be observable.
counter.add(-1.1);
await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand All @@ -125,7 +125,7 @@ describe('Instruments', () => {
counter.add(1, { foo: 'bar' });
counter.add(1.2, { foo: 'bar' });
await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand All @@ -141,7 +141,7 @@ describe('Instruments', () => {
// add negative values should not be observable.
counter.add(-1.1);
await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('Instruments', () => {
type: InstrumentType.UP_DOWN_COUNTER,
valueType: ValueType.INT,
},
dataPointType: DataPointType.SINGULAR,
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand All @@ -224,7 +224,7 @@ describe('Instruments', () => {
upDownCounter.add(4, { foo: 'bar' });
upDownCounter.add(1.1, { foo: 'bar' });
await validateExport(deltaReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('Instruments', () => {
});

await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand All @@ -501,7 +501,7 @@ describe('Instruments', () => {
],
});
await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand Down Expand Up @@ -543,7 +543,7 @@ describe('Instruments', () => {
});

await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand All @@ -556,7 +556,7 @@ describe('Instruments', () => {
],
});
await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
dataPoints: [
{
attributes: {},
Expand Down Expand Up @@ -603,7 +603,7 @@ describe('Instruments', () => {
});

await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.GAUGE,
dataPoints: [
{
attributes: {},
Expand All @@ -616,7 +616,7 @@ describe('Instruments', () => {
],
});
await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.GAUGE,
dataPoints: [
{
attributes: {},
Expand All @@ -629,7 +629,7 @@ describe('Instruments', () => {
],
});
await validateExport(cumulativeReader, {
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.GAUGE,
dataPoints: [
{
attributes: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,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'
Expand Down Expand Up @@ -265,7 +265,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,
});
Expand Down Expand Up @@ -329,7 +329,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,
});
Expand All @@ -344,7 +344,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
});
Expand Down Expand Up @@ -398,7 +398,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
});
Expand All @@ -413,7 +413,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
});
Expand Down Expand Up @@ -475,7 +475,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
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('LastValueAggregator', () => {
const expected: MetricData = {
descriptor: defaultInstrumentDescriptor,
aggregationTemporality: AggregationTemporality.CUMULATIVE,
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.GAUGE,
dataPoints: [
{
attributes: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ describe('SumAggregator', () => {
const expected: MetricData = {
descriptor: defaultInstrumentDescriptor,
aggregationTemporality: AggregationTemporality.CUMULATIVE,
dataPointType: DataPointType.SINGULAR,
dataPointType: DataPointType.SUM,
isMonotonic: true,
dataPoints: [
{
attributes: {},
Expand Down
Loading