Skip to content

Commit

Permalink
feat(instrumentation): added synchronous gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonb committed Mar 15, 2024
1 parent 3a426e8 commit 974bf61
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export { MeterProvider } from './metrics/MeterProvider';
export {
ValueType,
Counter,
Gauge,
Histogram,
MetricOptions,
Observable,
Expand Down
11 changes: 11 additions & 0 deletions api/src/metrics/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {
BatchObservableCallback,
Counter,
Gauge,
Histogram,
MetricAttributes,
MetricOptions,
Expand Down Expand Up @@ -45,6 +46,16 @@ export interface MeterOptions {
* for the exported metric are deferred.
*/
export interface Meter {
/**
* Creates and returns a new `Gauge`.
* @param name the name of the metric.
* @param [options] the metric options.
*/
createGauge<AttributesTypes extends MetricAttributes = MetricAttributes>(
name: string,
options?: MetricOptions
): Gauge<AttributesTypes>;

/**
* Creates and returns a new `Histogram`.
* @param name the name of the metric.
Expand Down
9 changes: 9 additions & 0 deletions api/src/metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ export interface UpDownCounter<
add(value: number, attributes?: AttributesTypes, context?: Context): void;
}

export interface Gauge<
AttributesTypes extends MetricAttributes = MetricAttributes,
> {
/**
* Records a measurement. Value of the measurement must not be negative.
*/
record(value: number, attributes?: AttributesTypes, context?: Context): void;
}

export interface Histogram<
AttributesTypes extends MetricAttributes = MetricAttributes,
> {
Expand Down
17 changes: 15 additions & 2 deletions api/src/metrics/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import { Meter } from './Meter';
import {
BatchObservableCallback,
Counter,
Gauge,
Histogram,
MetricAttributes,
MetricOptions,
Observable,
ObservableCallback,
ObservableCounter,
ObservableGauge,
ObservableUpDownCounter,
UpDownCounter,
MetricAttributes,
Observable,
} from './Metric';

/**
Expand All @@ -36,6 +37,13 @@ import {
export class NoopMeter implements Meter {
constructor() {}

/**
* @see {@link Meter.createGauge}
*/
createGauge(_name: string, _options?: MetricOptions): Histogram {
return NOOP_GAUGE_METRIC;
}

/**
* @see {@link Meter.createHistogram}
*/
Expand Down Expand Up @@ -114,6 +122,10 @@ export class NoopUpDownCounterMetric
add(_value: number, _attributes: MetricAttributes): void {}
}

export class NoopGaugeMetric extends NoopMetric implements Gauge {
record(_value: number, _attributes: MetricAttributes): void {}
}

export class NoopHistogramMetric extends NoopMetric implements Histogram {
record(_value: number, _attributes: MetricAttributes): void {}
}
Expand All @@ -140,6 +152,7 @@ export const NOOP_METER = new NoopMeter();

// Synchronous instruments
export const NOOP_COUNTER_METRIC = new NoopCounterMetric();
export const NOOP_GAUGE_METRIC = new NoopGaugeMetric();
export const NOOP_HISTOGRAM_METRIC = new NoopHistogramMetric();
export const NOOP_UP_DOWN_COUNTER_METRIC = new NoopUpDownCounterMetric();

Expand Down
12 changes: 12 additions & 0 deletions api/test/common/noop-implementations/noop-meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC,
NOOP_UP_DOWN_COUNTER_METRIC,
createNoopMeter,
NOOP_GAUGE_METRIC,
} from '../../../src/metrics/NoopMeter';
import { NoopMeterProvider } from '../../../src/metrics/NoopMeterProvider';

Expand Down Expand Up @@ -116,6 +117,17 @@ describe('NoopMeter', () => {
);
});

it('gauge should not crash', () => {
const meter = new NoopMeterProvider().getMeter('test-noop');
const observableGauge = meter.createGauge('some-name');

// ensure the correct noop const is returned
assert.strictEqual(observableGauge, NOOP_GAUGE_METRIC);

const gaugeWithOptions = meter.createGauge('some-name', options);
assert.strictEqual(gaugeWithOptions, NOOP_GAUGE_METRIC);
});

it('observable up down counter should not crash', () => {
const meter = new NoopMeterProvider().getMeter('test-noop');
const observableUpDownCounter =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const DeltaTemporalitySelector: AggregationTemporalitySelector = (
switch (instrumentType) {
case InstrumentType.COUNTER:
case InstrumentType.OBSERVABLE_COUNTER:
case InstrumentType.GAUGE:
case InstrumentType.HISTOGRAM:
case InstrumentType.OBSERVABLE_GAUGE:
return AggregationTemporality.DELTA;
Expand All @@ -57,6 +58,7 @@ export const LowMemoryTemporalitySelector: AggregationTemporalitySelector = (
case InstrumentType.COUNTER:
case InstrumentType.HISTOGRAM:
return AggregationTemporality.DELTA;
case InstrumentType.GAUGE:
case InstrumentType.UP_DOWN_COUNTER:
case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER:
case InstrumentType.OBSERVABLE_COUNTER:
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-metrics/src/InstrumentDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk-metrics/src/Instruments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ValueType,
UpDownCounter,
Counter,
Gauge,
Histogram,
Observable,
ObservableCallback,
Expand Down Expand Up @@ -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.
*/
Expand Down
19 changes: 19 additions & 0 deletions packages/sdk-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ import {
ObservableUpDownCounter,
BatchObservableCallback,
Observable,
Attributes,
Gauge,
} from '@opentelemetry/api';
import {
createInstrumentDescriptor,
InstrumentType,
} from './InstrumentDescriptor';
import {
CounterInstrument,
GaugeInstrument,
HistogramInstrument,
ObservableCounterInstrument,
ObservableGaugeInstrument,
Expand All @@ -46,6 +49,22 @@ import { MeterSharedState } from './state/MeterSharedState';
export class Meter implements IMeter {
constructor(private _meterSharedState: MeterSharedState) {}

/**
* Create a {@link Gauge} instrument.
*/
createGauge<AttributesTypes extends Attributes = Attributes>(
name: string,
options?: MetricOptions
): Gauge<AttributesTypes> {
const descriptor = createInstrumentDescriptor(
name,
InstrumentType.GAUGE,
options
);
const storage = this._meterSharedState.registerMetricStorage(descriptor);
return new GaugeInstrument(storage, descriptor);
}

/**
* Create a {@link Histogram} instrument.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-metrics/src/aggregator/Histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export class HistogramAggregator implements Aggregator<HistogramAccumulation> {

// 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;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-metrics/src/view/Aggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-metrics/test/export/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const instrumentTypes = [
InstrumentType.UP_DOWN_COUNTER,
InstrumentType.OBSERVABLE_UP_DOWN_COUNTER,
InstrumentType.HISTOGRAM,
InstrumentType.GAUGE,
InstrumentType.OBSERVABLE_GAUGE,
];

Expand Down

0 comments on commit 974bf61

Please sign in to comment.