This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
prom_wrapper.js
168 lines (149 loc) · 3.98 KB
/
prom_wrapper.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
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const prom = require('prom-client')
const registry = require('prom-client').register
const metrics = new Map()
const optsKey = function(opts) {
let keys = Object.keys(opts)
if (keys.length === 0) {
return ''
}
keys = keys.sort()
let hash = ''
for (const key of Array.from(keys)) {
if (hash.length) {
hash += ','
}
hash += `${key}:${opts[key]}`
}
return hash
}
const extendOpts = function(opts, labelNames) {
for (const label of Array.from(labelNames)) {
if (!opts[label]) {
opts[label] = ''
}
}
return opts
}
const optsAsArgs = function(opts, labelNames) {
const args = []
for (const label of Array.from(labelNames)) {
args.push(opts[label] || '')
}
return args
}
const PromWrapper = {
ttlInMinutes: 0,
registry,
metric(type, name) {
return metrics.get(name) || new MetricWrapper(type, name)
},
collectDefaultMetrics: prom.collectDefaultMetrics
}
class MetricWrapper {
constructor(type, name) {
metrics.set(name, this)
this.name = name
this.instances = new Map()
this.lastAccess = new Date()
this.metric = (() => {
switch (type) {
case 'counter':
return new prom.Counter({
name,
help: name,
labelNames: ['status', 'method', 'path']
})
case 'summary':
return new prom.Summary({
name,
help: name,
maxAgeSeconds: 60,
ageBuckets: 10,
labelNames: ['path', 'status_code', 'method', 'collection', 'query']
})
case 'gauge':
return new prom.Gauge({
name,
help: name,
labelNames: ['host', 'status']
})
}
})()
}
inc(opts, value) {
return this._execMethod('inc', opts, value)
}
observe(opts, value) {
return this._execMethod('observe', opts, value)
}
set(opts, value) {
return this._execMethod('set', opts, value)
}
sweep() {
const thresh = new Date(Date.now() - 1000 * 60 * PromWrapper.ttlInMinutes)
this.instances.forEach((instance, key) => {
if (thresh > instance.time) {
if (process.env.DEBUG_METRICS) {
console.log(
'Sweeping stale metric instance',
this.name,
{ opts: instance.opts },
key
)
}
return this.metric.remove(
...Array.from(optsAsArgs(instance.opts, this.metric.labelNames) || [])
)
}
})
if (thresh > this.lastAccess) {
if (process.env.DEBUG_METRICS) {
console.log('Sweeping stale metric', this.name, thresh, this.lastAccess)
}
metrics.delete(this.name)
return registry.removeSingleMetric(this.name)
}
}
_execMethod(method, opts, value) {
opts = extendOpts(opts, this.metric.labelNames)
const key = optsKey(opts)
if (key !== '') {
this.instances.set(key, { time: new Date(), opts })
}
this.lastAccess = new Date()
return this.metric[method](opts, value)
}
}
let sweepingInterval
PromWrapper.setupSweeping = function() {
if (sweepingInterval) {
clearInterval(sweepingInterval)
}
if (!PromWrapper.ttlInMinutes) {
if (process.env.DEBUG_METRICS) {
console.log('Not registering sweep method -- empty ttl')
}
return
}
if (process.env.DEBUG_METRICS) {
console.log('Registering sweep method')
}
sweepingInterval = setInterval(function() {
if (process.env.DEBUG_METRICS) {
console.log('Sweeping metrics')
}
return metrics.forEach((metric, key) => {
return metric.sweep()
})
}, 60000)
const Metrics = require('./index')
Metrics.registerDestructor(() => clearInterval(sweepingInterval))
}
module.exports = PromWrapper