Skip to content

Commit 16ae2a7

Browse files
authored
feat(metrics): use MetricDescriptor to determine aggregator open-telemetry#989 (open-telemetry#1014)
1 parent 817397b commit 16ae2a7

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

packages/opentelemetry-metrics/src/Metric.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
3535
protected readonly _disabled: boolean;
3636
protected readonly _valueType: api.ValueType;
3737
protected readonly _logger: api.Logger;
38-
private readonly _descriptor: MetricDescriptor;
38+
protected readonly _descriptor: MetricDescriptor;
3939
private readonly _instruments: Map<string, T> = new Map();
4040

4141
constructor(
@@ -124,7 +124,7 @@ export class CounterMetric extends Metric<BoundCounter>
124124
this._valueType,
125125
this._logger,
126126
// @todo: consider to set to CounterSumAggregator always.
127-
this._batcher.aggregatorFor(MetricKind.COUNTER)
127+
this._batcher.aggregatorFor(this._descriptor)
128128
);
129129
}
130130

@@ -161,7 +161,7 @@ export class MeasureMetric extends Metric<BoundMeasure>
161161
this._absolute,
162162
this._valueType,
163163
this._logger,
164-
this._batcher.aggregatorFor(MetricKind.MEASURE)
164+
this._batcher.aggregatorFor(this._descriptor)
165165
);
166166
}
167167

@@ -191,7 +191,7 @@ export class ObserverMetric extends Metric<BoundObserver>
191191
this._monotonic,
192192
this._valueType,
193193
this._logger,
194-
this._batcher.aggregatorFor(MetricKind.OBSERVER)
194+
this._batcher.aggregatorFor(this._descriptor)
195195
);
196196
}
197197

packages/opentelemetry-metrics/src/export/Batcher.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ import {
1919
MeasureExactAggregator,
2020
ObserverAggregator,
2121
} from './aggregators';
22-
import { MetricRecord, MetricKind, Aggregator } from './types';
22+
import {
23+
MetricRecord,
24+
MetricKind,
25+
Aggregator,
26+
MetricDescriptor,
27+
} from './types';
2328

2429
/**
2530
* Base class for all batcher types.
@@ -31,8 +36,8 @@ import { MetricRecord, MetricKind, Aggregator } from './types';
3136
export abstract class Batcher {
3237
protected readonly _batchMap = new Map<string, MetricRecord>();
3338

34-
/** Returns an aggregator based off metric kind. */
35-
abstract aggregatorFor(metricKind: MetricKind): Aggregator;
39+
/** Returns an aggregator based off metric descriptor. */
40+
abstract aggregatorFor(metricKind: MetricDescriptor): Aggregator;
3641

3742
/** Stores record information to be ready for exporting. */
3843
abstract process(record: MetricRecord): void;
@@ -47,8 +52,8 @@ export abstract class Batcher {
4752
* passes them for exporting.
4853
*/
4954
export class UngroupedBatcher extends Batcher {
50-
aggregatorFor(metricKind: MetricKind): Aggregator {
51-
switch (metricKind) {
55+
aggregatorFor(metricDescriptor: MetricDescriptor): Aggregator {
56+
switch (metricDescriptor.metricKind) {
5257
case MetricKind.COUNTER:
5358
return new CounterSumAggregator();
5459
case MetricKind.OBSERVER:

packages/opentelemetry-metrics/test/Meter.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
MetricRecord,
2929
Aggregator,
3030
MetricObservable,
31+
MetricDescriptor,
3132
} from '../src';
3233
import * as types from '@opentelemetry/api';
3334
import { NoopLogger, hrTime, hrTimeToNanoseconds } from '@opentelemetry/core';
@@ -566,7 +567,7 @@ class CustomBatcher extends Batcher {
566567
process(record: MetricRecord): void {
567568
throw new Error('process method not implemented.');
568569
}
569-
aggregatorFor(metricKind: MetricKind): Aggregator {
570+
aggregatorFor(metricKind: MetricDescriptor): Aggregator {
570571
throw new Error('aggregatorFor method not implemented.');
571572
}
572573
}

0 commit comments

Comments
 (0)