-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
78 lines (68 loc) · 1.6 KB
/
index.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
'use strict';
/**
* Wraps Prometheus client - https://github.com/siimon/prom-client
* Goal: use metrics in any file of your app without harm
*/
var assert = require('assert');
var client = require('prom-client');
var _ = require('lodash');
var references = {};
var namespace = '';
module.exports = {
/**
* Sets a namespace for all the metrics created after its call
* @param ns
*/
setNamespace: function(ns) {
assert(_.isString(ns));
namespace = ns;
},
/**
* What to expose in /metrics in your server
*/
getMetrics: function() {
return client.register.metrics();
},
/**
* Get metric by its name
* @param name
* @returns {*}
*/
get: function (name) {
return references[name];
},
createCounter: function (name, help, labelNames = []) {
references[name] = new client.Counter({
name: namespace + '_' + name,
help,
labelNames
});
return references[name];
},
createGauge: function (name, help, labelNames = []) {
references[name] = new client.Gauge({
name: namespace + '_' + name,
help,
labelNames
});
return references[name];
},
createHistogram: function (name, help, params, labelNames = []) {
references[name] = new client.Histogram({
name: namespace + '_' + name,
help,
...params,
labelNames
});
return references[name];
},
createSummary: function (name, help, params, labelNames = []) {
references[name] = new client.Summary({
name: namespace + '_' + name,
help,
...params,
labelNames
});
return references[name];
}
};