2016-12-04 14:09:37 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
const events = require('./events');
|
2018-05-23 11:24:24 +02:00
|
|
|
const {
|
|
|
|
FEATURE_CREATED,
|
|
|
|
FEATURE_UPDATED,
|
|
|
|
FEATURE_ARCHIVED,
|
|
|
|
FEATURE_REVIVED,
|
|
|
|
} = require('./event-type');
|
2016-11-30 23:41:57 +01:00
|
|
|
|
2018-11-28 15:50:49 +01:00
|
|
|
exports.startMonitoring = (
|
|
|
|
enable,
|
|
|
|
eventBus,
|
|
|
|
eventStore,
|
|
|
|
clientMetricsStore
|
|
|
|
) => {
|
2016-11-30 23:41:57 +01:00
|
|
|
if (!enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const client = require('prom-client');
|
2017-08-23 11:27:58 +02:00
|
|
|
const gcStats = require('prometheus-gc-stats');
|
2016-12-04 14:09:37 +01:00
|
|
|
|
2017-06-28 14:21:05 +02:00
|
|
|
client.collectDefaultMetrics();
|
2017-08-23 11:27:58 +02:00
|
|
|
gcStats()();
|
2017-06-28 14:21:05 +02:00
|
|
|
|
|
|
|
const requestDuration = new client.Summary({
|
|
|
|
name: 'http_request_duration_milliseconds',
|
|
|
|
help: 'App response time',
|
|
|
|
labelNames: ['path', 'method', 'status'],
|
|
|
|
percentiles: [0.1, 0.5, 0.9, 0.99],
|
|
|
|
});
|
2018-05-23 11:24:24 +02:00
|
|
|
const featureToggleUpdateTotal = new client.Counter({
|
|
|
|
name: 'feature_toggle_update_total',
|
|
|
|
help: 'Number of times a toggle has been updated',
|
|
|
|
labelNames: ['toggle'],
|
|
|
|
});
|
2018-11-28 15:50:49 +01:00
|
|
|
const featureToggleUsageTotal = new client.Counter({
|
|
|
|
name: 'feature_toggle_usage_total',
|
|
|
|
help: 'Number of times a feature toggle has been used',
|
|
|
|
labelNames: ['toggle', 'active'],
|
|
|
|
});
|
2016-12-01 17:43:08 +01:00
|
|
|
|
2016-12-04 14:09:37 +01:00
|
|
|
eventBus.on(events.REQUEST_TIME, ({ path, method, time, statusCode }) => {
|
2016-12-01 18:10:25 +01:00
|
|
|
requestDuration.labels(path, method, statusCode).observe(time);
|
2016-12-01 17:43:08 +01:00
|
|
|
});
|
2018-05-23 11:24:24 +02:00
|
|
|
|
|
|
|
eventStore.on(FEATURE_CREATED, ({ data }) => {
|
|
|
|
featureToggleUpdateTotal.labels(data.name).inc();
|
|
|
|
});
|
|
|
|
eventStore.on(FEATURE_UPDATED, ({ data }) => {
|
|
|
|
featureToggleUpdateTotal.labels(data.name).inc();
|
|
|
|
});
|
|
|
|
eventStore.on(FEATURE_ARCHIVED, ({ data }) => {
|
|
|
|
featureToggleUpdateTotal.labels(data.name).inc();
|
|
|
|
});
|
|
|
|
eventStore.on(FEATURE_REVIVED, ({ data }) => {
|
|
|
|
featureToggleUpdateTotal.labels(data.name).inc();
|
|
|
|
});
|
2018-11-28 15:50:49 +01:00
|
|
|
|
|
|
|
clientMetricsStore.on('metrics', m => {
|
|
|
|
for (const [feature, { yes, no }] of Object.entries(m.bucket.toggles)) {
|
|
|
|
featureToggleUsageTotal.labels(feature, true).inc(yes);
|
|
|
|
featureToggleUsageTotal.labels(feature, false).inc(no);
|
|
|
|
}
|
|
|
|
});
|
2016-12-04 14:09:37 +01:00
|
|
|
};
|