Repository navigation
Expand file tree
/
Copy pathgauge.js
More file actions
194 lines (171 loc) · 4.3 KB
/
Copy pathgauge.js
File metadata and controls
194 lines (171 loc) · 4.3 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Gauge metric
*/
'use strict';
const util = require('util');
const type = 'gauge';
const {
setValue,
getLabels,
hashObject,
isObject,
removeLabels,
} = require('./util');
const { validateLabel } = require('./validation');
const { Metric } = require('./metric');
class Gauge extends Metric {
/**
* 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) {
if (!isObject(labels)) {
return set.call(this, null)(labels, value);
}
return set.call(this, labels)(value);
}
/**
* Reset gauge
* @returns {void}
*/
reset() {
return reset.call(this);
}
/**
* 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) {
inc.call(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) {
dec.call(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) {
return setToCurrentTime.call(this, labels)();
}
/**
* 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) {
return startTimer.call(this, labels)();
}
async get() {
if (this.collect) {
const v = this.collect();
if (v instanceof Promise) await v;
}
return {
help: this.help,
name: this.name,
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() {
const labels = getLabels(this.labelNames, arguments);
validateLabel(this.labelNames, labels);
return {
inc: inc.call(this, labels),
dec: dec.call(this, labels),
set: set.call(this, labels),
setToCurrentTime: setToCurrentTime.call(this, labels),
startTimer: startTimer.call(this, labels),
};
}
remove() {
const labels = getLabels(this.labelNames, arguments);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
}
}
function setToCurrentTime(labels) {
return () => {
const now = Date.now() / 1000;
if (labels === undefined) {
this.set(now);
} else {
this.set(labels, now);
}
};
}
function startTimer(startLabels) {
return () => {
const start = process.hrtime();
return endLabels => {
const delta = process.hrtime(start);
this.set(
Object.assign({}, startLabels, endLabels),
delta[0] + delta[1] / 1e9,
);
};
};
}
function dec(labels) {
return value => {
const data = convertLabelsAndValues(labels, value);
this.set(data.labels, this._getValue(data.labels) - (data.value || 1));
};
}
function inc(labels) {
return value => {
const data = convertLabelsAndValues(labels, value);
this.set(data.labels, this._getValue(data.labels) + (data.value || 1));
};
}
function set(labels) {
return value => {
if (typeof value !== 'number') {
throw new TypeError(`Value is not a valid number: ${util.format(value)}`);
}
labels = labels || {};
validateLabel(this.labelNames, labels);
this.hashMap = setValue(this.hashMap, value, labels);
};
}
function reset() {
this.hashMap = {};
if (this.labelNames.length === 0) {
this.hashMap = setValue({}, 0, {});
}
}
function convertLabelsAndValues(labels, value) {
if (!isObject(labels)) {
return {
value: labels,
labels: {},
};
}
return {
labels,
value,
};
}
module.exports = Gauge;