Skip to content

Commit 0bafd46

Browse files
committed
feat(instrumentation): added synchronous gauge
1 parent 475ba67 commit 0bafd46

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-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:

0 commit comments

Comments
 (0)