Skip to content

Commit

Permalink
fixup! feat(instrumentation): added synchronous gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonb committed Mar 31, 2024
1 parent e35eb41 commit ea980b5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/src/metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export interface Gauge<
AttributesTypes extends MetricAttributes = MetricAttributes,
> {
/**
* Records a measurement. Value of the measurement must not be negative.
* Records a measurement.
*/
record(value: number, attributes?: AttributesTypes, context?: Context): void;
}
Expand Down
47 changes: 41 additions & 6 deletions packages/sdk-metrics/test/Instruments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ import * as sinon from 'sinon';
import { InstrumentationScope } from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import {
InstrumentType,
MeterProvider,
MetricReader,
DataPoint,
DataPointType,
Histogram,
InstrumentType,
MeterProvider,
MetricDescriptor,
MetricReader,
} from '../src';
import {
TestDeltaMetricReader,
TestMetricReader,
} from './export/TestMetricReader';
import {
assertMetricData,
assertDataPoint,
commonValues,
assertMetricData,
commonAttributes,
defaultResource,
commonValues,
defaultInstrumentationScope,
defaultResource,
} from './util';
import { ObservableResult, ValueType } from '@opentelemetry/api';

Expand Down Expand Up @@ -764,6 +764,41 @@ describe('Instruments', () => {
});
});
});

describe('Gauge', () => {
it('should record common values and attributes without exceptions', async () => {
const { meter } = setup();
const gauge = meter.createGauge('test');

for (const values of commonValues) {
for (const attributes of commonAttributes) {
gauge.record(values, attributes);
}
}
});

it('should record values', async () => {
const { meter, cumulativeReader } = setup();
const gauge = meter.createGauge('test');

gauge.record(1, { foo: 'bar' });
gauge.record(-1);

await validateExport(cumulativeReader, {
dataPointType: DataPointType.GAUGE,
dataPoints: [
{
attributes: { foo: 'bar' },
value: 1,
},
{
attributes: {},
value: -1,
},
],
});
});
});
});

function setup() {
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as assert from 'assert';
import * as sinon from 'sinon';
import {
CounterInstrument,
GaugeInstrument,
HistogramInstrument,
ObservableCounterInstrument,
ObservableGaugeInstrument,
Expand Down Expand Up @@ -88,6 +89,18 @@ describe('Meter', () => {
});
});

describe('createGauge', () => {
testWithNames('Gauge', name => {
const meterSharedState = new MeterSharedState(
new MeterProviderSharedState(defaultResource),
defaultInstrumentationScope
);
const meter = new Meter(meterSharedState);
const Gauge = meter.createGauge(name);
assert(Gauge instanceof GaugeInstrument);
});
});

describe('createObservableCounter', () => {
testWithNames('ObservableCounter', name => {
const meterSharedState = new MeterSharedState(
Expand Down

0 comments on commit ea980b5

Please sign in to comment.