-
Notifications
You must be signed in to change notification settings - Fork 378
/
gauge.js
173 lines (153 loc) · 4.24 KB
/
gauge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Gauge metric
*/
'use strict';
const util = require('util');
const {
setValue,
setValueDelta,
getLabels,
hashObject,
isObject,
removeLabels,
} = require('./util');
const { validateLabel } = require('./validation');
const { Metric } = require('./metric');
class Gauge extends Metric {
constructor(config) {
super(config);
this.type = 'gauge';
}
/**
* Set a gauge to a value
* @param {object} labels - Object with labels and their values
* @param {Number} value - Value to set the gauge to, must be positive
* @returns {void}
*/
set(labels, value) {
value = getValueArg(labels, value);
labels = getLabelArg(labels);
set(this, labels, value);
}
/**
* Reset gauge
* @returns {void}
*/
reset() {
this.hashMap = {};
if (this.labelNames.length === 0) {
setValue(this.hashMap, 0, {});
}
}
/**
* Increment a gauge value
* @param {object} labels - Object with labels where key is the label key and value is label value. Can only be one level deep
* @param {Number} value - Value to increment - if omitted, increment with 1
* @returns {void}
*/
inc(labels, value) {
value = getValueArg(labels, value);
labels = getLabelArg(labels);
if (value === undefined) value = 1;
setDelta(this, labels, value);
}
/**
* Decrement a gauge value
* @param {object} labels - Object with labels where key is the label key and value is label value. Can only be one level deep
* @param {Number} value - Value to decrement - if omitted, decrement with 1
* @returns {void}
*/
dec(labels, value) {
value = getValueArg(labels, value);
labels = getLabelArg(labels);
if (value === undefined) value = 1;
setDelta(this, labels, -value);
}
/**
* Set the gauge to current unix epoch
* @param {object} labels - Object with labels where key is the label key and value is label value. Can only be one level deep
* @returns {void}
*/
setToCurrentTime(labels) {
const now = Date.now() / 1000;
if (labels === undefined) {
this.set(now);
} else {
this.set(labels, now);
}
}
/**
* Start a timer
* @param {object} labels - Object with labels where key is the label key and value is label value. Can only be one level deep
* @returns {function} - Invoke this function to set the duration in seconds since you started the timer.
* @example
* var done = gauge.startTimer();
* makeXHRRequest(function(err, response) {
* done(); //Duration of the request will be saved
* });
*/
startTimer(labels) {
const start = process.hrtime();
return endLabels => {
const delta = process.hrtime(start);
const value = delta[0] + delta[1] / 1e9;
this.set(Object.assign({}, labels, endLabels), value);
return value;
};
}
async get() {
if (this.collect) {
const v = this.collect();
if (v instanceof Promise) await v;
}
return {
help: this.help,
name: this.name,
type: this.type,
values: Object.values(this.hashMap),
aggregator: this.aggregator,
};
}
_getValue(labels) {
const hash = hashObject(labels || {});
return this.hashMap[hash] ? this.hashMap[hash].value : 0;
}
labels(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
return {
inc: this.inc.bind(this, labels),
dec: this.dec.bind(this, labels),
set: this.set.bind(this, labels),
setToCurrentTime: this.setToCurrentTime.bind(this, labels),
startTimer: this.startTimer.bind(this, labels),
};
}
remove(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
}
}
function set(gauge, labels, value) {
if (typeof value !== 'number') {
throw new TypeError(`Value is not a valid number: ${util.format(value)}`);
}
validateLabel(gauge.labelNames, labels);
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 : {};
}
function getValueArg(labels, value) {
return isObject(labels) ? value : labels;
}
module.exports = Gauge;