diff --git a/lib/gauge.js b/lib/gauge.js index e995c88c..1540abb4 100644 --- a/lib/gauge.js +++ b/lib/gauge.js @@ -8,6 +8,7 @@ const type = 'gauge'; const { setValue, + setValueDelta, getLabels, hashObject, isObject, @@ -50,7 +51,7 @@ class Gauge extends Metric { value = getValueArg(labels, value); labels = getLabelArg(labels); if (value === undefined) value = 1; - set(this, labels, this._getValue(labels) + value); + setDelta(this, labels, value); } /** @@ -63,7 +64,7 @@ class Gauge extends Metric { value = getValueArg(labels, value); labels = getLabelArg(labels); if (value === undefined) value = 1; - set(this, labels, this._getValue(labels) - value); + setDelta(this, labels, -value); } /** @@ -147,6 +148,16 @@ function set(gauge, labels, value) { setValue(gauge.hashMap, value, labels); } +function setDelta(gauge, labels, delta) { + if (typeof delta !== 'number') { + throw new TypeError(`Delta is not a valid number: ${util.format(delta)}`); + } + + validateLabel(gauge.labelNames, labels); + const hash = hashObject(labels); + setValueDelta(gauge.hashMap, delta, labels, hash); +} + function getLabelArg(labels) { return isObject(labels) ? labels : {}; } diff --git a/lib/util.js b/lib/util.js index f9c53d05..dd626998 100644 --- a/lib/util.js +++ b/lib/util.js @@ -28,6 +28,21 @@ exports.setValue = function setValue(hashMap, value, labels) { return hashMap; }; +exports.setValueDelta = function setValueDelta( + hashMap, + deltaValue, + labels, + hash = '', +) { + const value = typeof deltaValue === 'number' ? deltaValue : 0; + if (hashMap[hash]) { + hashMap[hash].value += value; + } else { + hashMap[hash] = { value, labels }; + } + return hashMap; +}; + exports.getLabels = function (labelNames, args) { if (typeof args[0] === 'object') { return args[0];