Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk-metrics-base): add ValueType support for sync instruments #2776

Merged
merged 3 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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