-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
174 lines (125 loc) · 3.83 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
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
"use strict";
const prometheus = require('prom-client');
const onFinished = require('on-finished');
const path = require('path');
class Middleware {
constructor(prometheus) {
this.prometheus = prometheus;
}
report() {
return function(req, res) {
return res.contentType("text/plain")
.send(prometheus.register.metrics());
}
}
_getMetric(metricName, expected) {
let metric;
if (metricName instanceof expected) {
metric = metricName;
} else {
metric = this.prometheus.metric(metricName);
}
if(!(metric instanceof expected)) {
return false;
}
return metric;
}
heapUsage(metricNameUsed, metricNameTotal) {
const metricUsed = this._getMetric(metricNameUsed, prometheus.Gauge)
if(!(metricUsed instanceof prometheus.Gauge)) {
throw new Error('heapUsage requires a Gauge metric');
}
const metricTotal = this._getMetric(metricNameTotal, prometheus.Gauge)
if(!(metricTotal instanceof prometheus.Gauge)) {
throw new Error('heapUsage requires a Gauge metric');
}
if (metricTotal === metricUsed) {
throw new Error('heapUsage requires two different Gauge metrics');
}
return function(req, res, next) {
const heap = process.memoryUsage();
metricUsed.set(heap.heapUsed);
metricTotal.set(heap.heapTotal);
next();
}
}
requestDuration(metricName) {
const metric = this._getMetric(metricName, prometheus.Histogram)
if(!(metric instanceof prometheus.Histogram)) {
throw new Error('requestDuration requires a Histogram metric');
}
return function(req, res, next) {
const labels = {}
if (metric.labelNames.indexOf('http_status') !== -1) {
labels.http_status = 0;
}
if (metric.labelNames.indexOf('path') !== -1) {
labels.path = req.path;
}
if (metric.labelNames.indexOf('route') !== -1) {
labels.route = null;
}
if (metric.labelNames.indexOf('http_method') !== -1) {
labels.http_method = req.method;
}
const timeRequest = metric.startTimer(labels);
onFinished(res, () => {
if (labels.hasOwnProperty('http_status')) {
labels['http_status'] = res.statusCode;
}
if (labels.hasOwnProperty('route')) {
labels['route'] = req.route ? path.join(req.app.mountpath, req.route.path) : null;
}
timeRequest();
});
next();
}
}
}
class Prometheus {
constructor(prefix) {
this.setPrefix(prefix);
this.metrics = {}
this.middlewares = new Middleware(this);
}
setPrefix(prefix) {
if (!prefix || !prefix.length) return this.prefix = '';
prefix = `${prefix.replace(/_$/,'')}_`;
return this.prefix = prefix;
}
newCounter(name, description, labels) {
return this._build(prometheus.Counter, name, description, labels);
}
newHistogram(name, description, labels, opts) {
return this._build(prometheus.Histogram, name, description, labels, opts);
}
newGauge(name, description, labels) {
return this._build(prometheus.Gauge, name, description, labels)
}
metric(name) {
const argName = this._name(name);
if (!this._exists(name)) {
throw new Error(`Unknown metric '${name}'`)
}
return this.metrics[argName];
}
_name(name) {
return this.prefix + name;
}
_exists(name) {
name = this._name(name);
return !!this.metrics[name];
}
_build(proto, name, description, labels, opts) {
const nameArg = this._name(name);
if(this._exists(name)) {
throw new Error(`Requested creation of duplicate metric '${name}'`)
}
const args = [null, nameArg, description, labels, opts]
return this.metrics[nameArg] = new (Function.prototype.bind.apply(proto, args))
}
middleware() {
return this.middlewares;
}
}
module.exports = Prometheus;