Skip to content

Commit

Permalink
feat(sdk-metrics-base): add ValueType support for sync instruments (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas authored Feb 14, 2022
1 parent e4aa9eb commit fd62fa4
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import { InstrumentDescriptor } from './InstrumentDescriptor';
import { WritableMetricStorage } from './state/WritableMetricStorage';

export class SyncInstrument {
constructor(private _writableMetricStorage: WritableMetricStorage, private _descriptor: InstrumentDescriptor) { }
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()) {
if (this._descriptor.valueType === metrics.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);
}
}
Expand All @@ -39,7 +45,7 @@ export class UpDownCounter extends SyncInstrument implements metrics.UpDownCount
* 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);
}
}

Expand All @@ -56,7 +62,7 @@ export class Counter extends SyncInstrument implements metrics.Counter {
return;
}

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

Expand All @@ -68,6 +74,6 @@ export class Histogram extends SyncInstrument 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);
}
}
Loading

0 comments on commit fd62fa4

Please sign in to comment.