-
Couldn't load subscription status.
- Fork 0
add methods to gauge-track the in-flight count of functions and promises #46
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -112,6 +112,21 @@ export class MetricsGatherer { | |||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // increment/decrement a gauge during the runtime of a function | ||||||||||||||||||
| public async run(name: string, func: () => any, labels: LabelSet = {}) { | ||||||||||||||||||
| this.inc(name, 1, labels); | ||||||||||||||||||
| await func(); | ||||||||||||||||||
| this.inc(name, -1, labels); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // increment/decrement a gauge during the runtime of a promise (wait for it to resolve) | ||||||||||||||||||
| public resolve(name: string, promise: Promise<any>, labels: LabelSet = {}) { | ||||||||||||||||||
| this.inc(name, 1, labels); | ||||||||||||||||||
| promise.finally(() => { | ||||||||||||||||||
| this.inc(name, -1, labels); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+125
to
+127
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. This is better as
Suggested change
because it should also handle promises that don't support |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // decrement a gauge | ||||||||||||||||||
| public dec(name: string, val: number = 1, labels: LabelSet = {}) { | ||||||||||||||||||
| try { | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -44,6 +44,34 @@ describe('Gauge', () => { | |||||||||||||||
| output = metrics.output(); | ||||||||||||||||
| expect(/undescribed_gauge 1/.test(output)).to.be.true; | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| it('should track runtime of functions properly', () => { | ||||||||||||||||
| let output: string; | ||||||||||||||||
|
|
||||||||||||||||
| const func = async () => { | ||||||||||||||||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||||||||||||||||
| }; | ||||||||||||||||
| metrics.run('func_running_gauge', func); | ||||||||||||||||
| output = metrics.output(); | ||||||||||||||||
| expect(/func_running_gauge 1/.test(output)).to.be.true; | ||||||||||||||||
| setTimeout(() => { | ||||||||||||||||
| output = metrics.output(); | ||||||||||||||||
| expect(/func_running_gauge 0/.test(output)).to.be.true; | ||||||||||||||||
| }, 200); | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| it('should track runtime of promises properly', () => { | ||||||||||||||||
| let output: string; | ||||||||||||||||
|
|
||||||||||||||||
| const promise = new Promise(resolve => setTimeout(resolve, 100)); | ||||||||||||||||
| metrics.resolve('promise_running_gauge', promise); | ||||||||||||||||
| output = metrics.output(); | ||||||||||||||||
| expect(/promise_running_gauge 1/.test(output)).to.be.true; | ||||||||||||||||
| setTimeout(() => { | ||||||||||||||||
| output = metrics.output(); | ||||||||||||||||
| expect(/promise_running_gauge 0/.test(output)).to.be.true; | ||||||||||||||||
| }, 200); | ||||||||||||||||
|
Comment on lines
+70
to
+73
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 change this to
Suggested change
as it's clearer and also will mean that if the |
||||||||||||||||
| }); | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| describe('Counter', () => { | ||||||||||||||||
|
|
||||||||||||||||
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.
Matching the other function this should be
and make sure we don't drop decrements