diff --git a/api/src/metrics/Metric.ts b/api/src/metrics/Metric.ts index cbaf2e1e0c4..b2a1b7326b8 100644 --- a/api/src/metrics/Metric.ts +++ b/api/src/metrics/Metric.ts @@ -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; } diff --git a/packages/sdk-metrics/test/Instruments.test.ts b/packages/sdk-metrics/test/Instruments.test.ts index 71c69913a39..e02a393397d 100644 --- a/packages/sdk-metrics/test/Instruments.test.ts +++ b/packages/sdk-metrics/test/Instruments.test.ts @@ -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'; @@ -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() { diff --git a/packages/sdk-metrics/test/Meter.test.ts b/packages/sdk-metrics/test/Meter.test.ts index e40d6aa5076..875f2028622 100644 --- a/packages/sdk-metrics/test/Meter.test.ts +++ b/packages/sdk-metrics/test/Meter.test.ts @@ -19,6 +19,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import { CounterInstrument, + GaugeInstrument, HistogramInstrument, ObservableCounterInstrument, ObservableGaugeInstrument, @@ -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(