-
Notifications
You must be signed in to change notification settings - Fork 821
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: direct calling of metric instruments #507
Changes from 4 commits
48e3faa
1758fd0
ced8cdf
48349c6
5442f7d
c6ccc14
fa78b80
1445c07
4dbcbe0
9a19964
3c1e6ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,7 +121,8 @@ export abstract class Metric<T extends BaseHandle> implements types.Metric<T> { | |
} | ||
|
||
/** This is a SDK implementation of Counter Metric. */ | ||
export class CounterMetric extends Metric<CounterHandle> { | ||
export class CounterMetric extends Metric<CounterHandle> | ||
implements Pick<types.MetricUtils, 'add'> { | ||
constructor( | ||
name: string, | ||
options: MetricOptions, | ||
|
@@ -145,10 +146,20 @@ export class CounterMetric extends Metric<CounterHandle> { | |
this._onUpdate | ||
); | ||
} | ||
|
||
/** | ||
* Adds the given value to the current value. Values cannot be negative. | ||
* @param value the value to add. | ||
* @param labelSet the canonicalized LabelSet used to associate with this metric's handle. | ||
*/ | ||
add(value: number, labelSet: types.LabelSet) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we should include this on the top level API in types package. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I thought about it, do you think it makes sense to add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be a little bit confused with measure's record function? WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm makes sense. I am ok with current approach for now. Let's wait for others to comment on this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could add something like this to the Metric type: export interface Metric<T> {
...
add: T extends CounterHandle ? CounterHandle["add"] : never;
set: T extends GaugeHandle ? GaugeHandle["set"] : never;
record: T extends MeasureHandle ? MeasureHandle["record"] : never;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dyladan Thanks for your advice. I didn't make your suggestion work.. but it helps me find out Pick interface that enables what we need here. Please take a look and let me know if it works. 😁 |
||
this.getHandle(labelSet).add(value); | ||
} | ||
} | ||
|
||
/** This is a SDK implementation of Gauge Metric. */ | ||
export class GaugeMetric extends Metric<GaugeHandle> { | ||
export class GaugeMetric extends Metric<GaugeHandle> | ||
implements Pick<types.MetricUtils, 'set'> { | ||
constructor( | ||
name: string, | ||
options: MetricOptions, | ||
|
@@ -172,4 +183,13 @@ export class GaugeMetric extends Metric<GaugeHandle> { | |
this._onUpdate | ||
); | ||
} | ||
|
||
/** | ||
* Sets the given value. Values can be negative. | ||
* @param value the new value. | ||
* @param labelSet the canonicalized LabelSet used to associate with this metric's handle. | ||
*/ | ||
set(value: number, labelSet: types.LabelSet) { | ||
this.getHandle(labelSet).set(value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -99,6 +99,23 @@ export interface Metric<T> { | |||
setCallback(fn: () => void): void; | ||||
} | ||||
|
||||
export interface MetricUtils { | ||||
/** | ||||
* Adds the given value to the current value. Values cannot be negative. | ||||
*/ | ||||
add(value: number, labelSet: LabelSet): void; | ||||
/** | ||||
* Sets the given value. Values can be negative. | ||||
*/ | ||||
|
||||
set(value: number, labelSet: LabelSet): void; | ||||
/** | ||||
* Records the given value to this measure. | ||||
*/ | ||||
|
||||
record(value: number, labelSet: LabelSet): void; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the overload versions that take distributed context and span context? See here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, fixed it, thanks. |
||||
} | ||||
|
||||
/** | ||||
* key-value pairs passed by the user. | ||||
*/ | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is in Utils.ts, accidentally added here, just clear it up.