mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	Server Metrics: add response time and status codes
This commit is contained in:
		
							parent
							
								
									84e0810d64
								
							
						
					
					
						commit
						8e6bcafa24
					
				@ -5,12 +5,15 @@ const favicon = require('serve-favicon');
 | 
				
			|||||||
const bodyParser = require('body-parser');
 | 
					const bodyParser = require('body-parser');
 | 
				
			||||||
const cookieParser = require('cookie-parser');
 | 
					const cookieParser = require('cookie-parser');
 | 
				
			||||||
const validator = require('express-validator');
 | 
					const validator = require('express-validator');
 | 
				
			||||||
 | 
					const responseTime = require('response-time');
 | 
				
			||||||
const log4js = require('log4js');
 | 
					const log4js = require('log4js');
 | 
				
			||||||
const logger = require('./logger');
 | 
					const logger = require('./logger');
 | 
				
			||||||
const routes = require('./routes');
 | 
					const routes = require('./routes');
 | 
				
			||||||
const path = require('path');
 | 
					const path = require('path');
 | 
				
			||||||
const errorHandler = require('errorhandler');
 | 
					const errorHandler = require('errorhandler');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const {REQUEST_TIME, REQUEST_STATUS} = require('./events');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = function (config) {
 | 
					module.exports = function (config) {
 | 
				
			||||||
    const app = express();
 | 
					    const app = express();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -26,7 +29,13 @@ module.exports = function (config) {
 | 
				
			|||||||
        app.use(favicon(path.join(publicFolder, 'favicon.ico')));
 | 
					        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([]));
 | 
					    app.use(validator([]));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (publicFolder) {
 | 
					    if (publicFolder) {
 | 
				
			||||||
        app.use(baseUriPath, express.static(publicFolder));
 | 
					        app.use(baseUriPath, express.static(publicFolder));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -3,4 +3,6 @@ module.exports =  {
 | 
				
			|||||||
    TOGGLES_CREATE: 'toggles:create',
 | 
					    TOGGLES_CREATE: 'toggles:create',
 | 
				
			||||||
    CLIENT_REGISTER: 'client:register',
 | 
					    CLIENT_REGISTER: 'client:register',
 | 
				
			||||||
    CLIENT_METRICS: 'toggles:metrics',
 | 
					    CLIENT_METRICS: 'toggles:metrics',
 | 
				
			||||||
 | 
					    REQUEST_TIME: 'request_time',
 | 
				
			||||||
 | 
					    REQUEST_STATUS: 'request_status'
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -7,6 +7,12 @@ exports.startMonitoring = (enable, eventBus) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const client = require('prom-client');
 | 
					    const client = require('prom-client');
 | 
				
			||||||
    const toggleFetch = new client.Counter('toggles_fetch_counter', 'Number of fetch toggles request');
 | 
					    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 clientRegister = new client.Counter('client_register_counter', 'Number client register requests');
 | 
				
			||||||
    const clientMetrics = new client.Counter('client_metrics_counter', 'Number client metrics 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, () => {
 | 
					    eventBus.on(events.CLIENT_METRICS, () => {
 | 
				
			||||||
        clientMetrics.inc();
 | 
					        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();
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@ -3,6 +3,7 @@
 | 
				
			|||||||
const prometheusRegister = require('prom-client/lib/register');
 | 
					const prometheusRegister = require('prom-client/lib/register');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = function (app, config) {
 | 
					module.exports = function (app, config) {
 | 
				
			||||||
 | 
					    console.log({config});
 | 
				
			||||||
    if(config.serverMetrics) {
 | 
					    if(config.serverMetrics) {
 | 
				
			||||||
        app.get('/internal-backstage/prometheus', (req, res) => {
 | 
					        app.get('/internal-backstage/prometheus', (req, res) => {
 | 
				
			||||||
            res.writeHead(200, { 'Content-Type': 'text/plain' });
 | 
					            res.writeHead(200, { 'Content-Type': 'text/plain' });
 | 
				
			||||||
 | 
				
			|||||||
@ -17,8 +17,8 @@ function createApp (options) {
 | 
				
			|||||||
    const eventBus = new EventEmitter();
 | 
					    const eventBus = new EventEmitter();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const config = {
 | 
					    const config = {
 | 
				
			||||||
        prometheusPath: options.prometheusPath,
 | 
					 | 
				
			||||||
        baseUriPath: options.baseUriPath,
 | 
					        baseUriPath: options.baseUriPath,
 | 
				
			||||||
 | 
					        serverMetrics: options.serverMetrics,
 | 
				
			||||||
        port: options.port,
 | 
					        port: options.port,
 | 
				
			||||||
        publicFolder: options.publicFolder,
 | 
					        publicFolder: options.publicFolder,
 | 
				
			||||||
        stores,
 | 
					        stores,
 | 
				
			||||||
 | 
				
			|||||||
@ -72,6 +72,7 @@
 | 
				
			|||||||
    "parse-database-url": "^0.3.0",
 | 
					    "parse-database-url": "^0.3.0",
 | 
				
			||||||
    "pg": "^6.1.0",
 | 
					    "pg": "^6.1.0",
 | 
				
			||||||
    "prom-client": "^6.1.2",
 | 
					    "prom-client": "^6.1.2",
 | 
				
			||||||
 | 
					    "response-time": "^2.3.2",
 | 
				
			||||||
    "serve-favicon": "^2.3.0",
 | 
					    "serve-favicon": "^2.3.0",
 | 
				
			||||||
    "unleash-frontend": "github:unleash/unleash-frontend",
 | 
					    "unleash-frontend": "github:unleash/unleash-frontend",
 | 
				
			||||||
    "yallist": "^2.0.0"
 | 
					    "yallist": "^2.0.0"
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user