Skip to content

Commit

Permalink
chore(deps): support cumulative, delta, and pass-through exporters (#…
Browse files Browse the repository at this point in the history
…2118)

Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
  • Loading branch information
sergeylanzman and vmarchaud committed Apr 26, 2021
1 parent d504f32 commit b9c8426
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 29 deletions.
12 changes: 12 additions & 0 deletions packages/opentelemetry-api-metrics/src/types/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export interface MetricOptions {
* Boundaries optional for histogram
*/
boundaries?: number[];

/**
* Aggregation Temporality of metric
*/
aggregationTemporality?: AggregationTemporality;
}

export interface BatchObserverOptions {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export function ensureExportedCounterIsCorrect(
},
],
isMonotonic: true,
aggregationTemporality: 'AGGREGATION_TEMPORALITY_DELTA',
aggregationTemporality: 'AGGREGATION_TEMPORALITY_CUMULATIVE',
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export function ensureExportedCounterIsCorrect(
},
],
isMonotonic: true,
aggregationTemporality: 'AGGREGATION_TEMPORALITY_DELTA',
aggregationTemporality: 'AGGREGATION_TEMPORALITY_CUMULATIVE',
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-exporter-collector/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ export function ensureCounterIsCorrect(
isMonotonic: true,
aggregationTemporality:
collectorTypes.opentelemetryProto.metrics.v1.AggregationTemporality
.AGGREGATION_TEMPORALITY_DELTA,
.AGGREGATION_TEMPORALITY_CUMULATIVE,
},
});
}
Expand All @@ -599,7 +599,7 @@ export function ensureDoubleCounterIsCorrect(
isMonotonic: true,
aggregationTemporality:
collectorTypes.opentelemetryProto.metrics.v1.AggregationTemporality
.AGGREGATION_TEMPORALITY_DELTA,
.AGGREGATION_TEMPORALITY_CUMULATIVE,
},
});
}
Expand Down
10 changes: 10 additions & 0 deletions packages/opentelemetry-metrics/src/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
protected readonly _valueType: api.ValueType;
protected readonly _descriptor: MetricDescriptor;
protected readonly _boundaries: number[] | undefined;
protected readonly _aggregationTemporality: api.AggregationTemporality;
private readonly _instruments: Map<string, T> = new Map();

constructor(
Expand All @@ -43,6 +44,10 @@ export abstract class Metric<T extends BaseBoundInstrument>
: api.ValueType.DOUBLE;
this._boundaries = _options.boundaries;
this._descriptor = this._getMetricDescriptor();
this._aggregationTemporality =
_options.aggregationTemporality === undefined
? api.AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE
: _options.aggregationTemporality;
}

/**
Expand Down Expand Up @@ -83,13 +88,18 @@ export abstract class Metric<T extends BaseBoundInstrument>
return this._kind;
}

getAggregationTemporality() {
return this._aggregationTemporality;
}

getMetricRecord(): Promise<MetricRecord[]> {
return new Promise(resolve => {
resolve(
Array.from(this._instruments.values()).map(instrument => ({
descriptor: this._descriptor,
labels: instrument.getLabels(),
aggregator: instrument.getAggregator(),
aggregationTemporality: this.getAggregationTemporality(),
resource: this.resource,
instrumentationLibrary: this.instrumentationLibrary,
}))
Expand Down
7 changes: 6 additions & 1 deletion packages/opentelemetry-metrics/src/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit b9c8426

Please sign in to comment.