Skip to content

Commit ad0d35c

Browse files
committed
feat(monitoring): Add more metrics to monitoring
1 parent 5bfae88 commit ad0d35c

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

server/config/default.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ module.exports = {
4040
github: {},
4141
slack: {},
4242
mailjet: {},
43+
monitoring: {
44+
metricsPath: '/metrics',
45+
},
4346
};

server/src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ const manifest = {
4242
}, {
4343
plugin: './users',
4444
}, {
45-
plugin: './monitoring',
45+
plugin: {
46+
register: './monitoring',
47+
options: config.monitoring,
48+
},
4649
}, {
4750
plugin: {
4851
register: './login',

server/src/monitoring/index.js

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
const client = require('prom-client');
22
const Boom = require('boom');
33

4-
const requestCounter = new client.Counter({
5-
name: 'http_requests_total',
6-
help: 'HTTP Requests per seconds',
7-
labelNames: ['method', 'path'],
8-
});
9-
const { collectDefaultMetrics } = client;
10-
collectDefaultMetrics(5000);
4+
const metrics = {
5+
// This allows to measure request response time repartition by quantiles
6+
httpRequestDurationMilliseconds: new client.Summary({
7+
name: 'http_request_duration_milliseconds',
8+
help: 'Response time in milliseconds by quantiles.',
9+
labelNames: ['method', 'path', 'status'],
10+
}),
11+
// This allows to rank request response time in 3 buckets: 0-500, 500-1500 and 1500-+Inf (used for Apdex)
12+
httpRequestBucketMilliseconds: new client.Histogram({
13+
name: 'http_request_bucket_milliseconds',
14+
help: 'Bucketed response time in milliseconds.',
15+
buckets: [500, 1500],
16+
labelNames: ['method', 'path', 'status'],
17+
}),
18+
// Total request counter to measure usage of each routes
19+
httpRequestsTotal: new client.Counter({
20+
name: 'http_requests_total',
21+
help: 'Paths taken in the app.',
22+
labelNames: ['path', 'method'],
23+
}),
24+
// Total server error counter (crashes and timeouts)
25+
httpRequestsErrorTotal: new client.Counter({
26+
name: 'http_server_error_total',
27+
help: 'Error codes returned.',
28+
labelNames: ['path', 'method'],
29+
}),
30+
// Total and Type of error counter
31+
httpRequestsErrorTotalByType: new client.Counter({
32+
name: 'http_error_by_type',
33+
help: 'Number of errors by error code',
34+
labelNames: ['path', 'method', 'code'],
35+
}),
36+
};
1137

1238
exports.register = (server, config, next) => {
39+
client.collectDefaultMetrics(5000);
1340
const { User } = server.plugins.users.models;
1441

1542
server.auth.strategy('metrics', 'basic', {
@@ -26,17 +53,30 @@ exports.register = (server, config, next) => {
2653

2754
server.route({
2855
method: 'GET',
29-
path: '/metrics',
56+
path: config.metricsPath,
3057
config: { auth: 'metrics' },
3158
handler(req, res) {
3259
res(client.register.metrics())
3360
.type(client.register.contentType);
3461
},
3562
});
3663

37-
server.on({ name: 'request-internal', filter: 'handler' }, (req) => {
38-
if (!/\/(assets|favicon|metrics)/.test(req.path)) {
39-
requestCounter.labels(req.method, req.route.path).inc();
64+
server.on({ name: 'request-internal', filter: 'received' }, (req) => {
65+
if (req.path !== config.metricsPath && req.path.startsWith('/api')) {
66+
metrics.httpRequestsTotal.inc({ path: req.path, method: req.method });
67+
}
68+
});
69+
70+
server.on('response', (req) => {
71+
if (req.path !== config.metricsPath && req.path.startsWith('/api')) {
72+
const time = req.info.responded - req.info.received;
73+
metrics.httpRequestDurationMilliseconds.labels(req.method, req.path, req.response.statusCode).observe(time);
74+
metrics.httpRequestBucketMilliseconds.labels(req.method, req.path, req.response.statusCode).observe(time);
75+
76+
if (req.response.statusCode >= 400) {
77+
metrics.httpRequestsErrorTotal.inc({ path: req.path, method: req.method });
78+
metrics.httpRequestsErrorTotalByType.inc({ path: req.path, method: req.method, code: req.response.statusCode });
79+
}
4080
}
4181
});
4282

0 commit comments

Comments
 (0)