diff --git a/packages/sdk-metrics/src/InstrumentDescriptor.ts b/packages/sdk-metrics/src/InstrumentDescriptor.ts index b8797e2c4dc..ef09dda8bb5 100644 --- a/packages/sdk-metrics/src/InstrumentDescriptor.ts +++ b/packages/sdk-metrics/src/InstrumentDescriptor.ts @@ -23,6 +23,7 @@ import { equalsCaseInsensitive } from './utils'; */ export enum InstrumentType { COUNTER = 'COUNTER', + GAUGE = 'GAUGE', HISTOGRAM = 'HISTOGRAM', UP_DOWN_COUNTER = 'UP_DOWN_COUNTER', OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER', diff --git a/packages/sdk-metrics/src/Instruments.ts b/packages/sdk-metrics/src/Instruments.ts index f665952f059..ce6a8c429a6 100644 --- a/packages/sdk-metrics/src/Instruments.ts +++ b/packages/sdk-metrics/src/Instruments.ts @@ -22,6 +22,7 @@ import { ValueType, UpDownCounter, Counter, + Gauge, Histogram, Observable, ObservableCallback, @@ -110,6 +111,18 @@ export class CounterInstrument extends SyncInstrument implements Counter { } } +/** + * The class implements {@link Gauge} interface. + */ +export class GaugeInstrument extends SyncInstrument implements Gauge { + /** + * Records a measurement. + */ + record(value: number, attributes?: MetricAttributes, ctx?: Context): void { + this._record(value, attributes, ctx); + } +} + /** * The class implements {@link Histogram} interface. */ diff --git a/packages/sdk-metrics/src/Meter.ts b/packages/sdk-metrics/src/Meter.ts index b3d69d0dfd6..1188ab55543 100644 --- a/packages/sdk-metrics/src/Meter.ts +++ b/packages/sdk-metrics/src/Meter.ts @@ -25,6 +25,8 @@ import { ObservableUpDownCounter, BatchObservableCallback, Observable, + Attributes, + Gauge, } from '@opentelemetry/api'; import { createInstrumentDescriptor, @@ -32,6 +34,7 @@ import { } from './InstrumentDescriptor'; import { CounterInstrument, + GaugeInstrument, HistogramInstrument, ObservableCounterInstrument, ObservableGaugeInstrument, @@ -46,6 +49,22 @@ import { MeterSharedState } from './state/MeterSharedState'; export class Meter implements IMeter { constructor(private _meterSharedState: MeterSharedState) {} + /** + * Create a {@link Gauge} instrument. + */ + createGauge( + name: string, + options?: MetricOptions + ): Gauge { + const descriptor = createInstrumentDescriptor( + name, + InstrumentType.GAUGE, + options + ); + const storage = this._meterSharedState.registerMetricStorage(descriptor); + return new GaugeInstrument(storage, descriptor); + } + /** * Create a {@link Histogram} instrument. */ diff --git a/packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts b/packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts index a0c438ee173..9eb296fb27f 100644 --- a/packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts +++ b/packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts @@ -580,6 +580,7 @@ export class ExponentialHistogramAggregator // determine if instrument allows negative values. const allowsNegativeValues = + descriptor.type === InstrumentType.GAUGE || descriptor.type === InstrumentType.UP_DOWN_COUNTER || descriptor.type === InstrumentType.OBSERVABLE_GAUGE || descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER; diff --git a/packages/sdk-metrics/src/aggregator/Histogram.ts b/packages/sdk-metrics/src/aggregator/Histogram.ts index 94cf9060393..5c9390b81f6 100644 --- a/packages/sdk-metrics/src/aggregator/Histogram.ts +++ b/packages/sdk-metrics/src/aggregator/Histogram.ts @@ -231,6 +231,7 @@ export class HistogramAggregator implements Aggregator { // determine if instrument allows negative values. const allowsNegativeValues = + descriptor.type === InstrumentType.GAUGE || descriptor.type === InstrumentType.UP_DOWN_COUNTER || descriptor.type === InstrumentType.OBSERVABLE_GAUGE || descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER; diff --git a/packages/sdk-metrics/src/view/Aggregation.ts b/packages/sdk-metrics/src/view/Aggregation.ts index 2f74063ca2b..f34bd1231a3 100644 --- a/packages/sdk-metrics/src/view/Aggregation.ts +++ b/packages/sdk-metrics/src/view/Aggregation.ts @@ -182,6 +182,7 @@ export class DefaultAggregation extends Aggregation { case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER: { return SUM_AGGREGATION; } + case InstrumentType.GAUGE: case InstrumentType.OBSERVABLE_GAUGE: { return LAST_VALUE_AGGREGATION; } diff --git a/packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts b/packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts index 6409f6aa04a..258f0df00f5 100644 --- a/packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts +++ b/packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts @@ -160,6 +160,7 @@ describe('ConsoleMetricExporter', () => { switch (instrumentType) { case InstrumentType.COUNTER: case InstrumentType.OBSERVABLE_COUNTER: + case InstrumentType.GAUGE: case InstrumentType.HISTOGRAM: case InstrumentType.OBSERVABLE_GAUGE: return AggregationTemporality.DELTA; diff --git a/packages/sdk-metrics/test/export/utils.ts b/packages/sdk-metrics/test/export/utils.ts index a5decfae41e..a10021ee905 100644 --- a/packages/sdk-metrics/test/export/utils.ts +++ b/packages/sdk-metrics/test/export/utils.ts @@ -29,6 +29,7 @@ const instrumentTypes = [ InstrumentType.UP_DOWN_COUNTER, InstrumentType.OBSERVABLE_UP_DOWN_COUNTER, InstrumentType.HISTOGRAM, + InstrumentType.GAUGE, InstrumentType.OBSERVABLE_GAUGE, ];