Skip to content

Commit 475ba67

Browse files
committed
feat(instrumentation): added synchronous gauge to SDK
1 parent aabd1a9 commit 475ba67

File tree

8 files changed

+38
-0
lines changed

8 files changed

+38
-0
lines changed

packages/sdk-metrics/src/InstrumentDescriptor.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { equalsCaseInsensitive } from './utils';
2323
*/
2424
export enum InstrumentType {
2525
COUNTER = 'COUNTER',
26+
GAUGE = 'GAUGE',
2627
HISTOGRAM = 'HISTOGRAM',
2728
UP_DOWN_COUNTER = 'UP_DOWN_COUNTER',
2829
OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER',

packages/sdk-metrics/src/Instruments.ts

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
ValueType,
2323
UpDownCounter,
2424
Counter,
25+
Gauge,
2526
Histogram,
2627
Observable,
2728
ObservableCallback,
@@ -110,6 +111,18 @@ export class CounterInstrument extends SyncInstrument implements Counter {
110111
}
111112
}
112113

114+
/**
115+
* The class implements {@link Gauge} interface.
116+
*/
117+
export class GaugeInstrument extends SyncInstrument implements Gauge {
118+
/**
119+
* Records a measurement.
120+
*/
121+
record(value: number, attributes?: MetricAttributes, ctx?: Context): void {
122+
this._record(value, attributes, ctx);
123+
}
124+
}
125+
113126
/**
114127
* The class implements {@link Histogram} interface.
115128
*/

packages/sdk-metrics/src/Meter.ts

+19
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ import {
2525
ObservableUpDownCounter,
2626
BatchObservableCallback,
2727
Observable,
28+
Attributes,
29+
Gauge,
2830
} from '@opentelemetry/api';
2931
import {
3032
createInstrumentDescriptor,
3133
InstrumentType,
3234
} from './InstrumentDescriptor';
3335
import {
3436
CounterInstrument,
37+
GaugeInstrument,
3538
HistogramInstrument,
3639
ObservableCounterInstrument,
3740
ObservableGaugeInstrument,
@@ -46,6 +49,22 @@ import { MeterSharedState } from './state/MeterSharedState';
4649
export class Meter implements IMeter {
4750
constructor(private _meterSharedState: MeterSharedState) {}
4851

52+
/**
53+
* Create a {@link Gauge} instrument.
54+
*/
55+
createGauge<AttributesTypes extends Attributes = Attributes>(
56+
name: string,
57+
options?: MetricOptions
58+
): Gauge<AttributesTypes> {
59+
const descriptor = createInstrumentDescriptor(
60+
name,
61+
InstrumentType.GAUGE,
62+
options
63+
);
64+
const storage = this._meterSharedState.registerMetricStorage(descriptor);
65+
return new GaugeInstrument(storage, descriptor);
66+
}
67+
4968
/**
5069
* Create a {@link Histogram} instrument.
5170
*/

packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts

+1
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ export class ExponentialHistogramAggregator
580580

581581
// determine if instrument allows negative values.
582582
const allowsNegativeValues =
583+
descriptor.type === InstrumentType.GAUGE ||
583584
descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
584585
descriptor.type === InstrumentType.OBSERVABLE_GAUGE ||
585586
descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER;

packages/sdk-metrics/src/aggregator/Histogram.ts

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ export class HistogramAggregator implements Aggregator<HistogramAccumulation> {
231231

232232
// determine if instrument allows negative values.
233233
const allowsNegativeValues =
234+
descriptor.type === InstrumentType.GAUGE ||
234235
descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
235236
descriptor.type === InstrumentType.OBSERVABLE_GAUGE ||
236237
descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER;

packages/sdk-metrics/src/view/Aggregation.ts

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export class DefaultAggregation extends Aggregation {
182182
case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER: {
183183
return SUM_AGGREGATION;
184184
}
185+
case InstrumentType.GAUGE:
185186
case InstrumentType.OBSERVABLE_GAUGE: {
186187
return LAST_VALUE_AGGREGATION;
187188
}

packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ describe('ConsoleMetricExporter', () => {
160160
switch (instrumentType) {
161161
case InstrumentType.COUNTER:
162162
case InstrumentType.OBSERVABLE_COUNTER:
163+
case InstrumentType.GAUGE:
163164
case InstrumentType.HISTOGRAM:
164165
case InstrumentType.OBSERVABLE_GAUGE:
165166
return AggregationTemporality.DELTA;

packages/sdk-metrics/test/export/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const instrumentTypes = [
2929
InstrumentType.UP_DOWN_COUNTER,
3030
InstrumentType.OBSERVABLE_UP_DOWN_COUNTER,
3131
InstrumentType.HISTOGRAM,
32+
InstrumentType.GAUGE,
3233
InstrumentType.OBSERVABLE_GAUGE,
3334
];
3435

0 commit comments

Comments
 (0)