mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
Simplify metrics: We only need timings as it includes counts for free
This commit is contained in:
parent
e075917136
commit
471518b41d
@ -12,7 +12,7 @@ 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');
|
const {REQUEST_TIME} = require('./events');
|
||||||
|
|
||||||
module.exports = function (config) {
|
module.exports = function (config) {
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -30,8 +30,8 @@ module.exports = function (config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.use(responseTime((req, res, time) => {
|
app.use(responseTime((req, res, time) => {
|
||||||
config.eventBus.emit(REQUEST_TIME, req.path, req.method, time);
|
const timingInfo = { path: req.path, method: req.method, statusCode: res.statusCode, time };
|
||||||
config.eventBus.emit(REQUEST_STATUS, req.path, req.method, res.statusCode);
|
config.eventBus.emit(REQUEST_TIME, timingInfo);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
app.use(validator([]));
|
app.use(validator([]));
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
TOGGLES_FETCH: 'toggles:fetch',
|
|
||||||
TOGGLES_CREATE: 'toggles:create',
|
|
||||||
CLIENT_REGISTER: 'client:register',
|
|
||||||
CLIENT_METRICS: 'toggles:metrics',
|
|
||||||
REQUEST_TIME: 'request_time',
|
REQUEST_TIME: 'request_time',
|
||||||
REQUEST_STATUS: 'request_status'
|
|
||||||
}
|
}
|
@ -6,33 +6,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 requestDuration = new client.Summary('http_request_duration_milliseconds', 'App response time', ['uri', 'method'], {
|
const requestDuration = new client.Summary('http_request_duration_milliseconds', 'App response time', ['path', 'method', 'status'], {
|
||||||
percentiles: [0.1, 0.5, 0.9, 0.99],
|
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');
|
eventBus.on(events.REQUEST_TIME, ({path, method, time, statusCode}) => {
|
||||||
const clientMetrics = new client.Counter('client_metrics_counter', 'Number client metrics requests');
|
requestDuration.labels(path, method, statusCode).observe(time);
|
||||||
|
|
||||||
eventBus.on(events.TOGGLES_FETCH, () => {
|
|
||||||
toggleFetch.inc();
|
|
||||||
});
|
|
||||||
|
|
||||||
eventBus.on(events.CLIENT_REGISTER, () => {
|
|
||||||
clientRegister.inc();
|
|
||||||
});
|
|
||||||
|
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
16
lib/metrics.test.js
Normal file
16
lib/metrics.test.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
const test = require('ava');
|
||||||
|
const { EventEmitter } = require('events');
|
||||||
|
const eventBus = new EventEmitter();
|
||||||
|
const { REQUEST_TIME } = require('./events');
|
||||||
|
const { startMonitoring } = require('./metrics');
|
||||||
|
const prometheusRegister = require('prom-client/lib/register');
|
||||||
|
|
||||||
|
test('should collect metrics for requests', t => {
|
||||||
|
startMonitoring(true, eventBus);
|
||||||
|
eventBus.emit(REQUEST_TIME, { path: 'somePath', method: 'GET', statusCode: 200, time: 1337 });
|
||||||
|
|
||||||
|
const metrics = prometheusRegister.metrics();
|
||||||
|
t.regex(metrics, /http_request_duration_milliseconds{quantile="0.99",status="200",method="GET",path="somePath"} 1337/)
|
||||||
|
|
||||||
|
});
|
@ -8,8 +8,6 @@ const ValidationError = require('../error/validation-error.js');
|
|||||||
const validateRequest = require('../error/validate-request');
|
const validateRequest = require('../error/validate-request');
|
||||||
const extractUser = require('../extract-user');
|
const extractUser = require('../extract-user');
|
||||||
|
|
||||||
const { TOGGLES_FETCH } = require('../events');
|
|
||||||
|
|
||||||
const legacyFeatureMapper = require('../data-helper/legacy-feature-mapper');
|
const legacyFeatureMapper = require('../data-helper/legacy-feature-mapper');
|
||||||
const version = 1;
|
const version = 1;
|
||||||
|
|
||||||
@ -39,11 +37,8 @@ const handleErrors = (req, res, error) => {
|
|||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
const { featureToggleStore, eventStore } = config.stores;
|
const { featureToggleStore, eventStore } = config.stores;
|
||||||
const { eventBus } = config;
|
|
||||||
|
|
||||||
app.get('/features', (req, res) => {
|
app.get('/features', (req, res) => {
|
||||||
eventBus.emit(TOGGLES_FETCH);
|
|
||||||
|
|
||||||
featureToggleStore.getFeatures()
|
featureToggleStore.getFeatures()
|
||||||
.then((features) => features.map(legacyFeatureMapper.addOldFields))
|
.then((features) => features.map(legacyFeatureMapper.addOldFields))
|
||||||
.then(features => res.json({ version, features }));
|
.then(features => res.json({ version, features }));
|
||||||
|
@ -4,7 +4,6 @@ const logger = require('../logger');
|
|||||||
const ClientMetrics = require('../client-metrics');
|
const ClientMetrics = require('../client-metrics');
|
||||||
const joi = require('joi');
|
const joi = require('joi');
|
||||||
const { clientMetricsSchema, clientRegisterSchema } = require('./metrics-schema');
|
const { clientMetricsSchema, clientRegisterSchema } = require('./metrics-schema');
|
||||||
const { CLIENT_REGISTER, CLIENT_METRICS } = require('../events');
|
|
||||||
const { catchLogAndSendErrorResponse } = require('./route-utils');
|
const { catchLogAndSendErrorResponse } = require('./route-utils');
|
||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
@ -14,8 +13,6 @@ module.exports = function (app, config) {
|
|||||||
clientInstanceStore,
|
clientInstanceStore,
|
||||||
} = config.stores;
|
} = config.stores;
|
||||||
|
|
||||||
const { eventBus } = config;
|
|
||||||
|
|
||||||
const metrics = new ClientMetrics(clientMetricsStore);
|
const metrics = new ClientMetrics(clientMetricsStore);
|
||||||
|
|
||||||
app.get('/client/seen-toggles', (req, res) => {
|
app.get('/client/seen-toggles', (req, res) => {
|
||||||
@ -36,8 +33,6 @@ module.exports = function (app, config) {
|
|||||||
return res.status(400).json(err);
|
return res.status(400).json(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
eventBus.emit(CLIENT_METRICS);
|
|
||||||
|
|
||||||
clientMetricsStore
|
clientMetricsStore
|
||||||
.insert(cleaned)
|
.insert(cleaned)
|
||||||
.then(() => clientInstanceStore.insert({
|
.then(() => clientInstanceStore.insert({
|
||||||
@ -60,8 +55,6 @@ module.exports = function (app, config) {
|
|||||||
return res.status(400).json(err);
|
return res.status(400).json(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
eventBus.emit(CLIENT_REGISTER);
|
|
||||||
|
|
||||||
clientStrategyStore
|
clientStrategyStore
|
||||||
.insert(cleaned.appName, cleaned.strategies)
|
.insert(cleaned.appName, cleaned.strategies)
|
||||||
.then(() => clientInstanceStore.insert({
|
.then(() => clientInstanceStore.insert({
|
||||||
|
Loading…
Reference in New Issue
Block a user