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 b57098d commit d339cf4
Show file tree
Hide file tree
Showing 2 changed files with 42 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

0 comments on commit d339cf4

Please sign in to comment.