Skip to content

Commit ea980b5

Browse files
committed
fixup! feat(instrumentation): added synchronous gauge
1 parent e35eb41 commit ea980b5

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

api/src/metrics/Metric.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export interface Gauge<
102102
AttributesTypes extends MetricAttributes = MetricAttributes,
103103
> {
104104
/**
105-
* Records a measurement. Value of the measurement must not be negative.
105+
* Records a measurement.
106106
*/
107107
record(value: number, attributes?: AttributesTypes, context?: Context): void;
108108
}

packages/sdk-metrics/test/Instruments.test.ts

+41-6
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ import * as sinon from 'sinon';
1919
import { InstrumentationScope } from '@opentelemetry/core';
2020
import { Resource } from '@opentelemetry/resources';
2121
import {
22-
InstrumentType,
23-
MeterProvider,
24-
MetricReader,
2522
DataPoint,
2623
DataPointType,
2724
Histogram,
25+
InstrumentType,
26+
MeterProvider,
2827
MetricDescriptor,
28+
MetricReader,
2929
} from '../src';
3030
import {
3131
TestDeltaMetricReader,
3232
TestMetricReader,
3333
} from './export/TestMetricReader';
3434
import {
35-
assertMetricData,
3635
assertDataPoint,
37-
commonValues,
36+
assertMetricData,
3837
commonAttributes,
39-
defaultResource,
38+
commonValues,
4039
defaultInstrumentationScope,
40+
defaultResource,
4141
} from './util';
4242
import { ObservableResult, ValueType } from '@opentelemetry/api';
4343

@@ -764,6 +764,41 @@ describe('Instruments', () => {
764764
});
765765
});
766766
});
767+
768+
describe('Gauge', () => {
769+
it('should record common values and attributes without exceptions', async () => {
770+
const { meter } = setup();
771+
const gauge = meter.createGauge('test');
772+
773+
for (const values of commonValues) {
774+
for (const attributes of commonAttributes) {
775+
gauge.record(values, attributes);
776+
}
777+
}
778+
});
779+
780+
it('should record values', async () => {
781+
const { meter, cumulativeReader } = setup();
782+
const gauge = meter.createGauge('test');
783+
784+
gauge.record(1, { foo: 'bar' });
785+
gauge.record(-1);
786+
787+
await validateExport(cumulativeReader, {
788+
dataPointType: DataPointType.GAUGE,
789+
dataPoints: [
790+
{
791+
attributes: { foo: 'bar' },
792+
value: 1,
793+
},
794+
{
795+
attributes: {},
796+
value: -1,
797+
},
798+
],
799+
});
800+
});
801+
});
767802
});
768803

769804
function setup() {

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

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as assert from 'assert';
1919
import * as sinon from 'sinon';
2020
import {
2121
CounterInstrument,
22+
GaugeInstrument,
2223
HistogramInstrument,
2324
ObservableCounterInstrument,
2425
ObservableGaugeInstrument,
@@ -88,6 +89,18 @@ describe('Meter', () => {
8889
});
8990
});
9091

92+
describe('createGauge', () => {
93+
testWithNames('Gauge', name => {
94+
const meterSharedState = new MeterSharedState(
95+
new MeterProviderSharedState(defaultResource),
96+
defaultInstrumentationScope
97+
);
98+
const meter = new Meter(meterSharedState);
99+
const Gauge = meter.createGauge(name);
100+
assert(Gauge instanceof GaugeInstrument);
101+
});
102+
});
103+
91104
describe('createObservableCounter', () => {
92105
testWithNames('ObservableCounter', name => {
93106
const meterSharedState = new MeterSharedState(

0 commit comments

Comments
 (0)