From 8e6bcafa24cae135ff532afed3af9cc7954aee8b Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Thu, 1 Dec 2016 17:43:08 +0100 Subject: [PATCH] Server Metrics: add response time and status codes --- lib/app.js | 9 +++++++++ lib/events.js | 2 ++ lib/metrics.js | 14 ++++++++++++++ lib/routes/backstage.js | 1 + lib/server-impl.js | 2 +- package.json | 1 + 6 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/app.js b/lib/app.js index 937f8046bc..77bb326e96 100644 --- a/lib/app.js +++ b/lib/app.js @@ -5,12 +5,15 @@ const favicon = require('serve-favicon'); const bodyParser = require('body-parser'); const cookieParser = require('cookie-parser'); const validator = require('express-validator'); +const responseTime = require('response-time'); const log4js = require('log4js'); const logger = require('./logger'); const routes = require('./routes'); const path = require('path'); const errorHandler = require('errorhandler'); +const {REQUEST_TIME, REQUEST_STATUS} = require('./events'); + module.exports = function (config) { const app = express(); @@ -26,7 +29,13 @@ module.exports = function (config) { app.use(favicon(path.join(publicFolder, 'favicon.ico'))); } + app.use(responseTime((req, res, time) => { + config.eventBus.emit(REQUEST_TIME, req.path, req.method, time); + config.eventBus.emit(REQUEST_STATUS, req.path, req.method, res.statusCode); + })); + app.use(validator([])); + if (publicFolder) { app.use(baseUriPath, express.static(publicFolder)); } diff --git a/lib/events.js b/lib/events.js index 9a813220a2..e5b97aad4c 100644 --- a/lib/events.js +++ b/lib/events.js @@ -3,4 +3,6 @@ module.exports = { TOGGLES_CREATE: 'toggles:create', CLIENT_REGISTER: 'client:register', CLIENT_METRICS: 'toggles:metrics', + REQUEST_TIME: 'request_time', + REQUEST_STATUS: 'request_status' } \ No newline at end of file diff --git a/lib/metrics.js b/lib/metrics.js index f5c51dac70..afec639d3b 100644 --- a/lib/metrics.js +++ b/lib/metrics.js @@ -7,6 +7,12 @@ exports.startMonitoring = (enable, eventBus) => { const client = require('prom-client'); const toggleFetch = new client.Counter('toggles_fetch_counter', 'Number of fetch toggles request'); + + const requestDuration = new client.Summary('http_request_duration_milliseconds', 'App response time', ['uri', 'method'], { + percentiles: [0.1, 0.5, 0.9, 0.99], + }); + const requestCount = new client.Counter('http_requests_total', 'HTTP request duration', ['uri', 'method', 'status']); + const clientRegister = new client.Counter('client_register_counter', 'Number client register requests'); const clientMetrics = new client.Counter('client_metrics_counter', 'Number client metrics requests'); @@ -21,4 +27,12 @@ exports.startMonitoring = (enable, eventBus) => { eventBus.on(events.CLIENT_METRICS, () => { clientMetrics.inc(); }); + + eventBus.on(events.REQUEST_TIME, (uri, method, time) => { + requestDuration.labels(uri, method).observe(time); + }); + + eventBus.on(events.REQUEST_STATUS, (uri, method, status) => { + requestCount.labels(uri, method, status).inc(); + }); }; \ No newline at end of file diff --git a/lib/routes/backstage.js b/lib/routes/backstage.js index e8edabb757..e317d8558f 100644 --- a/lib/routes/backstage.js +++ b/lib/routes/backstage.js @@ -3,6 +3,7 @@ const prometheusRegister = require('prom-client/lib/register'); module.exports = function (app, config) { + console.log({config}); if(config.serverMetrics) { app.get('/internal-backstage/prometheus', (req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); diff --git a/lib/server-impl.js b/lib/server-impl.js index c4ec627c35..c970538b7b 100644 --- a/lib/server-impl.js +++ b/lib/server-impl.js @@ -17,8 +17,8 @@ function createApp (options) { const eventBus = new EventEmitter(); const config = { - prometheusPath: options.prometheusPath, baseUriPath: options.baseUriPath, + serverMetrics: options.serverMetrics, port: options.port, publicFolder: options.publicFolder, stores, diff --git a/package.json b/package.json index 0cc8ab0957..cc20e67f2a 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "parse-database-url": "^0.3.0", "pg": "^6.1.0", "prom-client": "^6.1.2", + "response-time": "^2.3.2", "serve-favicon": "^2.3.0", "unleash-frontend": "github:unleash/unleash-frontend", "yallist": "^2.0.0"