-
Notifications
You must be signed in to change notification settings - Fork 1
/
with-metrics.js
51 lines (45 loc) · 1.16 KB
/
with-metrics.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
import {
register as globalMetricsRegistry,
Counter, Summary, Gauge,
} from 'prom-client'
const trackHafasClientCachingMetrics = (cachedHafasClient, opt = {}) => {
const {
metricsRegistry,
} = {
metricsRegistry: globalMetricsRegistry,
...opt,
}
if ('function' !== typeof cachedHafasClient.on) {
throw new Error('cachedHafasClient does not seem to be compatible')
}
const hits = new Counter({
name: 'cached_hafas_client_hits_total',
help: 'cached-hafas-client: nr. of cache hits',
registers: [metricsRegistry],
labelNames: ['method'],
})
const trackHit = (method) => {
hits.inc({method})
}
const misses = new Counter({
name: 'cached_hafas_client_misses_total',
help: 'cached-hafas-client: nr. of cache misses',
registers: [metricsRegistry],
labelNames: ['method'],
})
const trackMiss = (method) => {
misses.inc({method})
}
cachedHafasClient.on('hit', trackHit)
cachedHafasClient.on('miss', trackMiss)
const stopTracking = () => {
cachedHafasClient.removeListener('hit', trackHit)
cachedHafasClient.removeListener('miss', trackMiss)
}
return {
stopTracking,
}
}
export {
trackHafasClientCachingMetrics as trackCachingMetrics,
}