Skip to content

Commit 62d69bd

Browse files
committed
lib: eliminate gauge applyDelta
1 parent bf2351c commit 62d69bd

3 files changed

Lines changed: 23 additions & 46 deletions

File tree

doc/api/metrics.md

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ import { memoryUsage } from 'node:process';
115115
const memory = gauge('memory.usage.bytes');
116116

117117
memory.reset(memoryUsage().heapUsed);
118-
memory.applyDelta(1024); // Add 1024 to current value
119118
```
120119

121120
### `metrics.timer(name[, meta])`
@@ -450,26 +449,6 @@ memory.reset(memoryUsage().heapUsed); // Set to current memory usage
450449
memory.reset(1024, { source: 'system' }); // Set to 1024 with metadata
451450
```
452451

453-
#### `gauge.applyDelta(delta[, meta])`
454-
455-
<!-- YAML
456-
added: REPLACEME
457-
-->
458-
459-
* `delta` {number} The amount to add to the current value.
460-
* `meta` {Object} Additional metadata for this report.
461-
462-
Adds a delta to the current value and reports the new value.
463-
464-
```mjs
465-
import { gauge } from 'node:metrics';
466-
467-
const cpuUsage = gauge('cpu.usage.percent');
468-
469-
cpuUsage.applyDelta(5); // Increase by 5
470-
cpuUsage.applyDelta(-2, { source: 'system' }); // Decrease by 2 with metadata
471-
```
472-
473452
### Class: `Timer`
474453

475454
* Extends: {metrics.Gauge}

lib/metrics.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,34 @@ class Metric {
218218
* reset() is called, or when applyDelta() is called with a non-zero value.
219219
*/
220220
class Gauge {
221+
#metric;
222+
#value;
223+
224+
/**
225+
* @param {Metric} metric The metric to report to.
226+
*/
227+
constructor(metric) {
228+
if (!(metric instanceof Metric)) {
229+
throw new ERR_INVALID_ARG_TYPE('metric', ['Metric'], metric);
230+
}
231+
this.#metric = metric;
232+
this.#value = 0;
233+
}
234+
221235
/**
222236
* The metric to report to.
223237
* @property {Metric} metric
224238
*/
239+
get metric() {
240+
return this.#metric;
241+
}
225242

226243
/**
227244
* The value of the gauge.
228245
* @property {number} value
229246
*/
230-
231-
/**
232-
* @param {Metric} metric The metric to report to.
233-
*/
234-
constructor(metric) {
235-
if (!(metric instanceof Metric)) {
236-
throw new ERR_INVALID_ARG_TYPE('metric', ['Metric'], metric);
237-
}
238-
this.metric = metric;
239-
this.value = 0;
247+
get value() {
248+
return this.#value;
240249
}
241250

242251
/**
@@ -245,18 +254,9 @@ class Gauge {
245254
* @param {object} [meta] Additional metadata to include with the report.
246255
*/
247256
reset(value = 0, meta) {
248-
this.value = value;
257+
this.#value = value;
249258
this.metric.report(value, meta);
250259
}
251-
252-
/**
253-
* Apply a delta to the gauge.
254-
* @param {number} value The delta to apply to the gauge.
255-
* @param {object} [meta] Additional metadata to include with the report.
256-
*/
257-
applyDelta(value, meta) {
258-
this.reset(this.value + value, meta);
259-
}
260260
}
261261

262262
/**
@@ -276,7 +276,7 @@ class Counter extends Gauge {
276276
n = 1;
277277
}
278278

279-
this.applyDelta(n, meta);
279+
this.reset(this.value + n, meta);
280280
}
281281

282282
/**
@@ -292,7 +292,7 @@ class Counter extends Gauge {
292292
n = 1;
293293
}
294294

295-
this.applyDelta(-n, meta);
295+
this.reset(this.value + -n, meta);
296296
}
297297
}
298298

test/parallel/test-metrics-gauge.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ assert.strictEqual(metric.channelName, 'metrics:gauge:test');
1919

2020
const messages = [
2121
[123, { base: 'test', meta: 'first' }],
22-
[357, { base: 'test', meta: 'second' }],
2322
[0, { base: 'test' }],
2423
];
2524

@@ -32,8 +31,7 @@ subscribe(metric.channelName, common.mustCall((report) => {
3231
const [value, meta] = messages.shift();
3332
assert.strictEqual(report.value, value);
3433
assert.deepStrictEqual(report.meta, meta);
35-
}, 3));
34+
}, 2));
3635

3736
testGauge.reset(123, { meta: 'first' });
38-
testGauge.applyDelta(234, { meta: 'second' });
3937
testGauge.reset();

0 commit comments

Comments
 (0)