Skip to content

Commit

Permalink
feat(instrumentation): added synchronous gauge to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonb committed Mar 23, 2024
1 parent f9a0d3e commit 3aaf51e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 0 deletions.
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 3aaf51e

Please sign in to comment.