Skip to content

Commit

Permalink
feat(sdk-metrics-base): add ValueType support in sync instruments
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Feb 10, 2022
1 parent ba177fa commit 2e9dda7
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,101 @@

import * as api from '@opentelemetry/api';
import * as metrics from '@opentelemetry/api-metrics-wip';
import { ValueType } from '@opentelemetry/api-metrics-wip';
import { InstrumentDescriptor } from './InstrumentDescriptor';
import { WritableMetricStorage } from './state/WritableMetricStorage';

export class SyncInstrument {
constructor(private _writableMetricStorage: WritableMetricStorage, private _descriptor: InstrumentDescriptor) { }
class SyncIntInstrument {
private _valueType: ValueType;

constructor(private _writableMetricStorage: WritableMetricStorage, private _descriptor: InstrumentDescriptor) {
this._valueType = _descriptor.valueType;
}

getName(): string {
return this._descriptor.name;
}

protected _record(value: number, attributes: metrics.Attributes = {}, context: api.Context = api.context.active()) {
if (this._valueType === ValueType.INT && !Number.isInteger(value)) {
api.diag.warn(
`INT value type cannot accept a floating-point value for ${this._descriptor.name}, ignoring the fractional digits.`
);
value = Math.trunc(value);
}
this._writableMetricStorage.record(value, attributes, context);
}
}

export class SyncDoubleInstrument {
constructor(private _writableMetricStorage: WritableMetricStorage, private _descriptor: InstrumentDescriptor) {}

getName(): string {
return this._descriptor.name;
}

aggregate(value: number, attributes: metrics.Attributes = {}, context: api.Context = api.context.active()) {
protected _record(value: number, attributes: metrics.Attributes = {}, context: api.Context = api.context.active()) {
this._writableMetricStorage.record(value, attributes, context);
}
}

/**
* The class implements {@link metrics.UpDownCounter} interface.
*/
export class UpDownCounter extends SyncInstrument implements metrics.UpDownCounter {
export class DoubleUpDownCounter extends SyncDoubleInstrument implements metrics.UpDownCounter {
/**
* Increment value of counter by the input. Inputs may be negative.
*/
add(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
this._record(value, attributes, ctx);
}
}

/**
* The class implements {@link metrics.Counter} interface.
*/
export class DoubleCounter extends SyncDoubleInstrument implements metrics.Counter {
/**
* Increment value of counter by the input. Inputs may not be negative.
*/
add(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
if (value < 0) {
api.diag.warn(`negative value provided to counter ${this.getName()}: ${value}`);
return;
}

this._record(value, attributes, ctx);
}
}

/**
* The class implements {@link metrics.Histogram} interface.
*/
export class DoubleHistogram extends SyncDoubleInstrument implements metrics.Histogram {
/**
* Records a measurement. Value of the measurement must not be negative.
*/
record(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
this._record(value, attributes, ctx);
}
}

/**
* The class implements {@link metrics.UpDownCounter} interface.
*/
export class IntUpDownCounter extends SyncIntInstrument implements metrics.UpDownCounter {
/**
* Increment value of counter by the input. Inputs may be negative.
*/
add(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
this.aggregate(value, attributes, ctx);
this._record(value, attributes, ctx);
}
}

/**
* The class implements {@link metrics.Counter} interface.
*/
export class Counter extends SyncInstrument implements metrics.Counter {
export class IntCounter extends SyncIntInstrument implements metrics.Counter {
/**
* Increment value of counter by the input. Inputs may not be negative.
*/
Expand All @@ -56,18 +120,18 @@ export class Counter extends SyncInstrument implements metrics.Counter {
return;
}

this.aggregate(value, attributes, ctx);
this._record(value, attributes, ctx);
}
}

/**
* The class implements {@link metrics.Histogram} interface.
*/
export class Histogram extends SyncInstrument implements metrics.Histogram {
export class IntHistogram extends SyncIntInstrument implements metrics.Histogram {
/**
* Records a measurement. Value of the measurement must not be negative.
*/
record(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
this.aggregate(value, attributes, ctx);
this._record(value, attributes, ctx);
}
}
28 changes: 24 additions & 4 deletions experimental/packages/opentelemetry-sdk-metrics-base/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
import * as metrics from '@opentelemetry/api-metrics-wip';
import { InstrumentationLibrary } from '@opentelemetry/core';
import { createInstrumentDescriptor, InstrumentDescriptor, InstrumentType } from './InstrumentDescriptor';
import { Counter, Histogram, UpDownCounter } from './Instruments';
import {
IntCounter,
DoubleCounter,
IntHistogram,
DoubleHistogram,
IntUpDownCounter,
DoubleUpDownCounter,
} from './Instruments';
import { MeterProviderSharedState } from './state/MeterProviderSharedState';
import { MultiMetricStorage } from './state/MultiWritableMetricStorage';
import { SyncMetricStorage } from './state/SyncMetricStorage';
Expand All @@ -27,6 +34,7 @@ import { isNotNullish } from './utils';
import { MetricCollectorHandle } from './state/MetricCollector';
import { HrTime } from '@opentelemetry/api';
import { AsyncMetricStorage } from './state/AsyncMetricStorage';
import { ValueType } from '@opentelemetry/api-metrics-wip';

/**
* This class implements the {@link metrics.Meter} interface.
Expand All @@ -44,7 +52,11 @@ export class Meter implements metrics.Meter {
createHistogram(name: string, options?: metrics.HistogramOptions): metrics.Histogram {
const descriptor = createInstrumentDescriptor(name, InstrumentType.HISTOGRAM, options);
const storage = this._registerMetricStorage(descriptor);
return new Histogram(storage, descriptor);
if (descriptor.valueType === ValueType.INT) {
return new IntHistogram(storage, descriptor);
} else {
return new DoubleHistogram(storage, descriptor);
}
}

/**
Expand All @@ -53,7 +65,11 @@ export class Meter implements metrics.Meter {
createCounter(name: string, options?: metrics.CounterOptions): metrics.Counter {
const descriptor = createInstrumentDescriptor(name, InstrumentType.COUNTER, options);
const storage = this._registerMetricStorage(descriptor);
return new Counter(storage, descriptor);
if (descriptor.valueType === ValueType.INT) {
return new IntCounter(storage, descriptor);
} else {
return new DoubleCounter(storage, descriptor);
}
}

/**
Expand All @@ -62,7 +78,11 @@ export class Meter implements metrics.Meter {
createUpDownCounter(name: string, options?: metrics.UpDownCounterOptions): metrics.UpDownCounter {
const descriptor = createInstrumentDescriptor(name, InstrumentType.UP_DOWN_COUNTER, options);
const storage = this._registerMetricStorage(descriptor);
return new UpDownCounter(storage, descriptor);
if (descriptor.valueType === ValueType.INT) {
return new IntUpDownCounter(storage, descriptor);
} else {
return new DoubleUpDownCounter(storage, descriptor);
}
}

/**
Expand Down
Loading

0 comments on commit 2e9dda7

Please sign in to comment.