@vtex/diagnostics-nodejs
Version:
Diagnostics library for Node.js applications
56 lines • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Gauge = void 0;
exports.NewGauge = NewGauge;
const options_1 = require("./options");
class Gauge {
constructor(gauge) {
this.lastValues = new Map();
this.gauge = gauge;
}
add(value, attributes, context) {
this.gauge.add(value, this.convertAttributes(attributes), context);
this.updateLastValue(value, attributes, true);
}
subtract(value, attributes, context) {
this.gauge.add(-value, this.convertAttributes(attributes), context);
this.updateLastValue(-value, attributes, true);
}
set(value, attributes, context) {
const attrKey = this.getAttributesKey(attributes || {});
const lastValue = this.lastValues.get(attrKey) || 0;
const delta = value - lastValue;
this.gauge.add(delta, this.convertAttributes(attributes), context);
this.lastValues.set(attrKey, value);
}
updateLastValue(value, attributes, isIncrement = false) {
const attrKey = this.getAttributesKey(attributes || {});
const currentValue = this.lastValues.get(attrKey) || 0;
const newValue = isIncrement ? currentValue + value : value;
this.lastValues.set(attrKey, newValue);
}
getAttributesKey(attributes) {
return JSON.stringify(attributes);
}
convertAttributes(attrs) {
if (!attrs)
return undefined;
const result = {};
for (const [key, value] of Object.entries(attrs)) {
if (value !== undefined && value !== null) {
result[key] = value;
}
}
return result;
}
}
exports.Gauge = Gauge;
function NewGauge(meter, name, opts = []) {
const config = (0, options_1.newConfig)(opts);
const gauge = meter.createUpDownCounter(name, {
description: config.description,
unit: config.unit,
});
return new Gauge(gauge);
}
//# sourceMappingURL=gauge.js.map