-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (79 loc) · 2.46 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
/* eslint-disable no-restricted-syntax */
'use strict';
import ProcessMetrics from '@metrics/process';
import Consumer from '@metrics/prometheus-consumer';
import abslog from 'abslog';
import Guard from '@metrics/guard';
import fp from 'fastify-plugin';
export default fp((fastify, opts, done) => {
const {
client,
pathname = '/metrics',
logger,
guard = {},
consumer = {},
} = opts;
let { metrics = [] } = opts;
const log = abslog(logger);
const mConsumer = new Consumer({ ...consumer, client, logger: log });
const mProcess = new ProcessMetrics();
const mGuard = new Guard(guard);
mConsumer.on('error', err => {
if (err)
log.error(
'an error occurred when piping metrics into the prometheus consumer module',
err,
);
});
mProcess.on('drop', metric => {
log.trace(`Process metric "${metric.name}" dropped`);
});
mProcess.on('collect:start', () => {
log.trace('Started collecting process metrics');
});
mProcess.on('collect:end', () => {
log.trace('Stopped collecting process metrics');
});
mProcess.on('error', err => {
if (err) log.error('Process metric stream error', err);
});
mGuard.on('warn', info => {
log.warn(
`${info} is creating a growing number of metric permutations. Metrics will begin being dropped if the increase continues.`,
);
});
mGuard.on('drop', metric => {
log.error(
`${metric.name} has created too many permutations. Metric has been dropped.`,
);
});
mGuard.on('error', err => {
if (err)
log.error(
'an error occurred when piping metrics into the @metrics/guard module',
err,
);
});
if (!Array.isArray(metrics)) metrics = [metrics];
for (const stream of metrics) {
stream.on('error', err => {
log.error(
'an error occurred in the @metrics/client module',
err,
);
});
stream.pipe(mGuard);
}
mProcess.pipe(mGuard);
mGuard.pipe(mConsumer);
mProcess.start();
fastify.get(pathname, async (request, reply) => {
const mObj = await mConsumer.registry.metrics();
reply
.type(mConsumer.registry.contentType)
.send(mObj);
});
done();
}, {
name: 'fastify-metrics-js-prometheus',
});