Skip to content

Commit

Permalink
feat(sdk-metrics): added synchronous gauge to SDK (#4565)
Browse files Browse the repository at this point in the history
* feat(instrumentation): added synchronous gauge to SDK

* fixup! feat(instrumentation): added synchronous gauge to SDK

* fixup! feat(instrumentation): added synchronous gauge to SDK

* fixup! feat(instrumentation): added synchronous gauge to SDK

* fixup! feat(instrumentation): added synchronous gauge to SDK

* fixup! feat(instrumentation): added synchronous gauge to SDK
  • Loading branch information
clintonb committed Apr 2, 2024
1 parent d66e1d7 commit 928796d
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and AnyValueMap types [#4575](https://github.com/open-telemetry/opentelemetry-js

### :rocket: (Enhancement)

* feat(metrics): added synchronous gauge to SDK [#4565](https://github.com/open-telemetry/opentelemetry-js/pull/4565) @clintonb
* feat(opentelemetry-instrumentation-xhr): optionally ignore network events [#4571](https://github.com/open-telemetry/opentelemetry-js/pull/4571/) @mustafahaddara
* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson
* perf(instrumentation-http): remove obvious temp allocations [#4576](https://github.com/open-telemetry/opentelemetry-js/pull/4576) @Samuron
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 @@ -36,6 +36,7 @@ import {
AsyncWritableMetricStorage,
WritableMetricStorage,
} from './state/WritableMetricStorage';
import { Gauge } from './types';

export class SyncInstrument {
constructor(
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
20 changes: 20 additions & 0 deletions packages/sdk-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,47 @@ import {
ObservableUpDownCounter,
BatchObservableCallback,
Observable,
Attributes,
} from '@opentelemetry/api';
import {
createInstrumentDescriptor,
InstrumentType,
} from './InstrumentDescriptor';
import {
CounterInstrument,
GaugeInstrument,
HistogramInstrument,
ObservableCounterInstrument,
ObservableGaugeInstrument,
ObservableUpDownCounterInstrument,
UpDownCounterInstrument,
} from './Instruments';
import { MeterSharedState } from './state/MeterSharedState';
import { Gauge } from './types';

/**
* This class implements the {@link IMeter} interface.
*/
export class Meter implements IMeter {
constructor(private _meterSharedState: MeterSharedState) {}

/**
* Create a {@link Gauge} instrument.
* @experimental
*/
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
16 changes: 16 additions & 0 deletions packages/sdk-metrics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Context, MetricAttributes } from '@opentelemetry/api';

export type CommonReaderOptions = {
timeoutMillis?: number;
Expand All @@ -23,3 +24,18 @@ export type CollectionOptions = CommonReaderOptions;
export type ShutdownOptions = CommonReaderOptions;

export type ForceFlushOptions = CommonReaderOptions;

/**
* @experimental
*
* This is intentionally not using the API's type as it's only available from @opentelemetry/api 1.9.0 and up.
* In SDK 2.0 we'll be able to bump the minimum API version and remove this workaround.
*/
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;
}
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
13 changes: 13 additions & 0 deletions packages/sdk-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as assert from 'assert';
import * as sinon from 'sinon';
import {
CounterInstrument,
GaugeInstrument,
HistogramInstrument,
ObservableCounterInstrument,
ObservableGaugeInstrument,
Expand Down Expand Up @@ -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(
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 928796d

Please sign in to comment.