2016-12-04 14:09:37 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-04-14 22:29:11 +02:00
|
|
|
const client = require('prom-client');
|
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');
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2020-02-28 14:50:32 +01:00
|
|
|
const THREE_HOURS = 3 * 60 * 60 * 1000;
|
2016-11-30 23:41:57 +01:00
|
|
|
|
2020-08-03 13:34:10 +02:00
|
|
|
exports.startMonitoring = ({ serverMetrics, eventBus, stores, version }) => {
|
|
|
|
if (!serverMetrics) {
|
2016-11-30 23:41:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:34:10 +02:00
|
|
|
const { eventStore, clientMetricsStore, featureToggleStore } = stores;
|
|
|
|
|
2017-06-28 14:21:05 +02:00
|
|
|
client.collectDefaultMetrics();
|
|
|
|
|
|
|
|
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],
|
|
|
|
});
|
2019-08-04 11:10:51 +02:00
|
|
|
const dbDuration = new client.Summary({
|
|
|
|
name: 'db_query_duration_seconds',
|
|
|
|
help: 'DB query duration time',
|
|
|
|
labelNames: ['store', 'action'],
|
|
|
|
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',
|
2019-12-18 15:13:42 +01:00
|
|
|
labelNames: ['toggle', 'active', 'appName'],
|
2018-11-28 15:50:49 +01:00
|
|
|
});
|
2020-02-28 14:50:32 +01:00
|
|
|
const featureTogglesTotal = new client.Gauge({
|
|
|
|
name: 'feature_toggles_total',
|
|
|
|
help: 'Number of feature toggles',
|
|
|
|
labelNames: ['version'],
|
|
|
|
});
|
|
|
|
|
|
|
|
async function collectFeatureToggleMetrics() {
|
|
|
|
featureTogglesTotal.reset();
|
|
|
|
const togglesCount = await featureToggleStore.count();
|
|
|
|
featureTogglesTotal.labels(version).set(togglesCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
collectFeatureToggleMetrics();
|
|
|
|
setInterval(() => collectFeatureToggleMetrics(), THREE_HOURS);
|
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
|
|
|
|
2019-08-04 11:10:51 +02:00
|
|
|
eventBus.on(events.DB_TIME, ({ store, action, time }) => {
|
|
|
|
dbDuration.labels(store, action).observe(time);
|
|
|
|
});
|
|
|
|
|
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 => {
|
2020-04-14 22:29:11 +02:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2018-11-28 15:50:49 +01:00
|
|
|
for (const [feature, { yes, no }] of Object.entries(m.bucket.toggles)) {
|
2019-12-18 15:13:42 +01:00
|
|
|
featureToggleUsageTotal.labels(feature, true, m.appName).inc(yes);
|
|
|
|
featureToggleUsageTotal.labels(feature, false, m.appName).inc(no);
|
2018-11-28 15:50:49 +01:00
|
|
|
}
|
|
|
|
});
|
2016-12-04 14:09:37 +01:00
|
|
|
};
|