Begin prometheus integration

Add a dependency on `prom-client` and emit a basic latency metric for
testing purposes.  Add a new configuration file for enabling/disabling
prometheus exporter and configuring the listen address.
This commit is contained in:
Calvin Montgomery 2017-07-16 22:35:33 -07:00
parent dd770137e5
commit c7bec6251e
10 changed files with 222 additions and 1 deletions

View file

@ -10,7 +10,8 @@ import morgan from 'morgan';
import csrf from './csrf';
import * as HTTPStatus from './httpstatus';
import { CSRFError, HTTPError } from '../errors';
import counters from "../counters";
import counters from '../counters';
import { Summary } from 'prom-client';
const LOGGER = require('@calzoneman/jsli')('webserver');
@ -27,6 +28,29 @@ function initializeLog(app) {
}));
}
function initPrometheus(app) {
const latency = new Summary({
name: 'cytube_http_req_latency',
help: 'HTTP Request latency from execution of the first middleware '
+ 'until the "finish" event on the response object.',
labelNames: ['method', 'statusCode']
});
app.use((req, res, next) => {
const startTime = process.hrtime();
res.on('finish', () => {
try {
const diff = process.hrtime(startTime);
const diffMs = diff[0]*1e3 + diff[1]*1e-6;
latency.labels(req.method, res.statusCode).observe(diffMs);
} catch (error) {
LOGGER.error('Failed to record HTTP Prometheus metrics: %s', error.stack);
}
});
next();
});
}
/**
* Redirects a request to HTTPS if the server supports it
*/
@ -133,6 +157,7 @@ module.exports = {
init: function (app, webConfig, ioConfig, clusterClient, channelIndex, session) {
const chanPath = Config.get('channel-path');
initPrometheus(app);
app.use((req, res, next) => {
counters.add("http:request", 1);
next();