Skip to content

Commit 758e26a

Browse files
committed
lib: detach counter from gauge
1 parent 62d69bd commit 758e26a

3 files changed

Lines changed: 48 additions & 16 deletions

File tree

doc/api/metrics.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ added: REPLACEME
345345

346346
The underlying metric instance used for reporting.
347347

348+
#### `counter.value`
349+
350+
<!-- YAML
351+
added: REPLACEME
352+
-->
353+
354+
* {number}
355+
356+
The current value of the counter.
357+
348358
#### `counter.increment([n[, meta]])`
349359

350360
<!-- YAML
@@ -389,16 +399,6 @@ errorCount.decrement(2, { errorType: 'timeout' }); // Decrement by 2 with metada
389399
errorCount.decrement({ errorType: 'timeout' }); // Decrement by 1 with metadata
390400
```
391401

392-
#### `counter.value`
393-
394-
<!-- YAML
395-
added: REPLACEME
396-
-->
397-
398-
* {number}
399-
400-
The current value of the counter.
401-
402402
### Class: `Gauge`
403403

404404
<!-- YAML

lib/metrics.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,37 @@ class Gauge {
262262
/**
263263
* An increasing or decreasing value.
264264
*/
265-
class Counter extends Gauge {
265+
class Counter {
266+
#metric;
267+
#value;
268+
269+
/**
270+
* @param {Metric} metric The metric to report to.
271+
*/
272+
constructor(metric) {
273+
if (!(metric instanceof Metric)) {
274+
throw new ERR_INVALID_ARG_TYPE('metric', ['Metric'], metric);
275+
}
276+
this.#metric = metric;
277+
this.#value = 0;
278+
}
279+
280+
/**
281+
* The metric to report to.
282+
* @property {Metric} metric
283+
*/
284+
get metric() {
285+
return this.#metric;
286+
}
287+
288+
/**
289+
* The value of the gauge.
290+
* @property {number} value
291+
*/
292+
get value() {
293+
return this.#value;
294+
}
295+
266296
/**
267297
* Increment the counter. Negative values invert to positive.
268298
* @param {number} [n] The amount to increment the counter by. Defaults to 1.
@@ -276,7 +306,8 @@ class Counter extends Gauge {
276306
n = 1;
277307
}
278308

279-
this.reset(this.value + n, meta);
309+
this.#value += n;
310+
this.metric.report(n, meta);
280311
}
281312

282313
/**
@@ -292,7 +323,8 @@ class Counter extends Gauge {
292323
n = 1;
293324
}
294325

295-
this.reset(this.value + -n, meta);
326+
this.#value -= n;
327+
this.metric.report(-n, meta);
296328
}
297329
}
298330

test/parallel/test-metrics-counter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ assert.strictEqual(metric.channelName, 'metrics:counter:test');
1919

2020
const messages = [
2121
[1, { base: 'test' }],
22-
[124, { base: 'test', meta: 'extra' }],
23-
[123, { base: 'test' }],
24-
[0, { base: 'test', meta: 'extra' }],
22+
[123, { base: 'test', meta: 'extra' }],
23+
[-1, { base: 'test' }],
24+
[-123, { base: 'test', meta: 'extra' }],
2525
];
2626

2727
subscribe(metric.channelName, common.mustCall((report) => {

0 commit comments

Comments
 (0)