Skip to content

Commit a30df7e

Browse files
committed
feat(instrumentation): added synchronous gauge
1 parent 63d74cd commit a30df7e

File tree

14 files changed

+88
-2
lines changed

14 files changed

+88
-2
lines changed

api/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export { MeterProvider } from './metrics/MeterProvider';
4343
export {
4444
ValueType,
4545
Counter,
46+
Gauge,
4647
Histogram,
4748
MetricOptions,
4849
Observable,

api/src/metrics/Meter.ts

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import {
1818
BatchObservableCallback,
1919
Counter,
20+
Gauge,
2021
Histogram,
2122
MetricAttributes,
2223
MetricOptions,
@@ -45,6 +46,16 @@ export interface MeterOptions {
4546
* for the exported metric are deferred.
4647
*/
4748
export interface Meter {
49+
/**
50+
* Creates and returns a new `Gauge`.
51+
* @param name the name of the metric.
52+
* @param [options] the metric options.
53+
*/
54+
createGauge<AttributesTypes extends MetricAttributes = MetricAttributes>(
55+
name: string,
56+
options?: MetricOptions
57+
): Gauge<AttributesTypes>;
58+
4859
/**
4960
* Creates and returns a new `Histogram`.
5061
* @param name the name of the metric.

api/src/metrics/Metric.ts

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ export interface UpDownCounter<
9898
add(value: number, attributes?: AttributesTypes, context?: Context): void;
9999
}
100100

101+
export interface Gauge<
102+
AttributesTypes extends MetricAttributes = MetricAttributes,
103+
> {
104+
/**
105+
* Records a measurement. Value of the measurement must not be negative.
106+
*/
107+
record(value: number, attributes?: AttributesTypes, context?: Context): void;
108+
}
109+
101110
export interface Histogram<
102111
AttributesTypes extends MetricAttributes = MetricAttributes,
103112
> {

api/src/metrics/NoopMeter.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ import { Meter } from './Meter';
1818
import {
1919
BatchObservableCallback,
2020
Counter,
21+
Gauge,
2122
Histogram,
23+
MetricAttributes,
2224
MetricOptions,
25+
Observable,
2326
ObservableCallback,
2427
ObservableCounter,
2528
ObservableGauge,
2629
ObservableUpDownCounter,
2730
UpDownCounter,
28-
MetricAttributes,
29-
Observable,
3031
} from './Metric';
3132

3233
/**
@@ -36,6 +37,13 @@ import {
3637
export class NoopMeter implements Meter {
3738
constructor() {}
3839

40+
/**
41+
* @see {@link Meter.createGauge}
42+
*/
43+
createGauge(_name: string, _options?: MetricOptions): Histogram {
44+
return NOOP_GAUGE_METRIC;
45+
}
46+
3947
/**
4048
* @see {@link Meter.createHistogram}
4149
*/
@@ -114,6 +122,10 @@ export class NoopUpDownCounterMetric
114122
add(_value: number, _attributes: MetricAttributes): void {}
115123
}
116124

125+
export class NoopGaugeMetric extends NoopMetric implements Gauge {
126+
record(_value: number, _attributes: MetricAttributes): void {}
127+
}
128+
117129
export class NoopHistogramMetric extends NoopMetric implements Histogram {
118130
record(_value: number, _attributes: MetricAttributes): void {}
119131
}
@@ -140,6 +152,7 @@ export const NOOP_METER = new NoopMeter();
140152

141153
// Synchronous instruments
142154
export const NOOP_COUNTER_METRIC = new NoopCounterMetric();
155+
export const NOOP_GAUGE_METRIC = new NoopGaugeMetric();
143156
export const NOOP_HISTOGRAM_METRIC = new NoopHistogramMetric();
144157
export const NOOP_UP_DOWN_COUNTER_METRIC = new NoopUpDownCounterMetric();
145158

api/test/common/noop-implementations/noop-meter.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC,
2525
NOOP_UP_DOWN_COUNTER_METRIC,
2626
createNoopMeter,
27+
NOOP_GAUGE_METRIC,
2728
} from '../../../src/metrics/NoopMeter';
2829
import { NoopMeterProvider } from '../../../src/metrics/NoopMeterProvider';
2930

@@ -116,6 +117,17 @@ describe('NoopMeter', () => {
116117
);
117118
});
118119

120+
it('gauge should not crash', () => {
121+
const meter = new NoopMeterProvider().getMeter('test-noop');
122+
const observableGauge = meter.createGauge('some-name');
123+
124+
// ensure the correct noop const is returned
125+
assert.strictEqual(observableGauge, NOOP_GAUGE_METRIC);
126+
127+
const gaugeWithOptions = meter.createGauge('some-name', options);
128+
assert.strictEqual(gaugeWithOptions, NOOP_GAUGE_METRIC);
129+
});
130+
119131
it('observable up down counter should not crash', () => {
120132
const meter = new NoopMeterProvider().getMeter('test-noop');
121133
const observableUpDownCounter =

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const DeltaTemporalitySelector: AggregationTemporalitySelector = (
4141
switch (instrumentType) {
4242
case InstrumentType.COUNTER:
4343
case InstrumentType.OBSERVABLE_COUNTER:
44+
case InstrumentType.GAUGE:
4445
case InstrumentType.HISTOGRAM:
4546
case InstrumentType.OBSERVABLE_GAUGE:
4647
return AggregationTemporality.DELTA;
@@ -57,6 +58,7 @@ export const LowMemoryTemporalitySelector: AggregationTemporalitySelector = (
5758
case InstrumentType.COUNTER:
5859
case InstrumentType.HISTOGRAM:
5960
return AggregationTemporality.DELTA;
61+
case InstrumentType.GAUGE:
6062
case InstrumentType.UP_DOWN_COUNTER:
6163
case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER:
6264
case InstrumentType.OBSERVABLE_COUNTER:

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)