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 1 commit
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 @@ -16,17 +16,27 @@

import * as api from '@opentelemetry/api';
import * as metrics from '@opentelemetry/api-metrics-wip';
import { ValueType } from '@opentelemetry/api-metrics-wip';
legendecas marked this conversation as resolved.
Show resolved Hide resolved
import { InstrumentDescriptor } from './InstrumentDescriptor';
import { WritableMetricStorage } from './state/WritableMetricStorage';

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have a separate private property when it can be accessed by the private descriptor already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of reducing the property lookups in the path of counter.add. It is probably trivial, never mind. Updated.

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

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._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);
}
}
Expand All @@ -39,7 +49,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 +66,7 @@ export class Counter extends SyncInstrument implements metrics.Counter {
return;
}

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

Expand All @@ -68,6 +78,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