2020-06-09 02:16:00 +08:00
|
|
|
'use strict'
|
|
|
|
|
2020-06-09 15:40:24 +08:00
|
|
|
const http = require('http');
|
2020-06-09 02:16:00 +08:00
|
|
|
const { Counter, Histogram, register } = require('prom-client');
|
|
|
|
const split = require('split2');
|
|
|
|
const { Transform } = require('stream');
|
|
|
|
const DEV_ENVS = ['test', 'development'];
|
|
|
|
|
|
|
|
const requestCounter = new Counter({
|
|
|
|
name: 'maps_api_requests_total',
|
|
|
|
help: 'MAPS API requests total'
|
|
|
|
});
|
|
|
|
|
|
|
|
const requestOkCounter = new Counter({
|
|
|
|
name: 'maps_api_requests_ok_total',
|
|
|
|
help: 'MAPS API requests ok total'
|
|
|
|
});
|
|
|
|
|
|
|
|
const requestErrorCounter = new Counter({
|
|
|
|
name: 'maps_api_requests_errors_total',
|
|
|
|
help: 'MAPS API requests errors total'
|
|
|
|
});
|
|
|
|
|
|
|
|
const responseTimeHistogram = new Histogram({
|
|
|
|
name: 'maps_api_response_time_total',
|
|
|
|
help: 'MAPS API response time total'
|
|
|
|
});
|
|
|
|
|
|
|
|
const userRequestCounter = new Counter({
|
|
|
|
name: 'maps_api_requests',
|
|
|
|
help: 'MAPS API requests per user',
|
|
|
|
labelNames: ['user', 'http_code']
|
|
|
|
});
|
|
|
|
|
|
|
|
const userRequestOkCounter = new Counter({
|
|
|
|
name: 'maps_api_requests_ok',
|
|
|
|
help: 'MAPS API requests per user with success HTTP code',
|
|
|
|
labelNames: ['user', 'http_code']
|
|
|
|
});
|
|
|
|
|
|
|
|
const userRequestErrorCounter = new Counter({
|
|
|
|
name: 'maps_api_requests_errors',
|
|
|
|
help: 'MAPS API requests per user with error HTTP code',
|
|
|
|
labelNames: ['user', 'http_code']
|
|
|
|
});
|
|
|
|
|
|
|
|
const userResponseTimeHistogram = new Histogram({
|
|
|
|
name: 'maps_api_response_time',
|
|
|
|
help: 'MAPS API response time total',
|
|
|
|
labelNames: ['user']
|
|
|
|
});
|
|
|
|
|
2020-06-09 15:30:11 +08:00
|
|
|
module.exports = function metricsCollector () {
|
2020-06-09 02:16:00 +08:00
|
|
|
return new Transform({
|
|
|
|
transform (chunk, enc, callback) {
|
|
|
|
let entry;
|
|
|
|
|
|
|
|
try {
|
|
|
|
entry = JSON.parse(chunk);
|
|
|
|
const { level, time } = entry;
|
|
|
|
|
|
|
|
if (level === undefined && time === undefined) {
|
|
|
|
throw new Error('Entry log is not valid');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (DEV_ENVS.includes(process.env.NODE_ENV)) {
|
|
|
|
this.push(chunk);
|
|
|
|
}
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { request, response, stats } = entry;
|
|
|
|
|
|
|
|
if (request === undefined || response === undefined || stats === undefined) {
|
2020-06-09 16:00:54 +08:00
|
|
|
this.push(chunk);
|
2020-06-09 02:16:00 +08:00
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { statusCode, headers } = response;
|
|
|
|
const { 'carto-user': user } = headers;
|
|
|
|
|
|
|
|
requestCounter.inc();
|
2020-06-19 16:18:06 +08:00
|
|
|
|
|
|
|
if (statusCode !== undefined && user !== undefined) {
|
|
|
|
userRequestCounter.labels(user, `${statusCode}`).inc();
|
|
|
|
}
|
2020-06-09 02:16:00 +08:00
|
|
|
|
|
|
|
if (statusCode >= 200 && statusCode < 400) {
|
|
|
|
requestOkCounter.inc();
|
2020-06-19 16:18:06 +08:00
|
|
|
if (user !== undefined) {
|
|
|
|
userRequestOkCounter.labels(user, `${statusCode}`).inc();
|
|
|
|
}
|
|
|
|
} else if (statusCode >= 400) {
|
2020-06-09 02:16:00 +08:00
|
|
|
requestErrorCounter.inc();
|
2020-06-19 16:18:06 +08:00
|
|
|
if (user !== undefined) {
|
|
|
|
userRequestErrorCounter.labels(user, `${statusCode}`).inc();
|
|
|
|
}
|
2020-06-09 02:16:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const { response: responseTime } = stats;
|
|
|
|
|
|
|
|
if (Number.isFinite(responseTime)) {
|
|
|
|
responseTimeHistogram.observe(responseTime);
|
|
|
|
userResponseTimeHistogram.labels(user).observe(responseTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.push(chunk);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:35:08 +08:00
|
|
|
const port = process.env.PORT || 9145;
|
2020-06-09 02:16:00 +08:00
|
|
|
|
2020-06-09 15:40:24 +08:00
|
|
|
http
|
|
|
|
.createServer((req, res) => {
|
|
|
|
res.writeHead(200, { 'Content-Type': register.contentType });
|
|
|
|
res.end(register.metrics());
|
|
|
|
})
|
2020-06-09 15:52:56 +08:00
|
|
|
.listen(port)
|
|
|
|
.unref();
|