1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

Add prometheus compatible feature metrics endpoint

This commit is contained in:
Benjamin Ludewig 2018-11-28 15:50:49 +01:00 committed by Ivar Conradi Østhus
parent 8f6a9318ba
commit 822d718a9b
3 changed files with 45 additions and 3 deletions

View File

@ -8,7 +8,12 @@ const {
FEATURE_REVIVED, FEATURE_REVIVED,
} = require('./event-type'); } = require('./event-type');
exports.startMonitoring = (enable, eventBus, eventStore) => { exports.startMonitoring = (
enable,
eventBus,
eventStore,
clientMetricsStore
) => {
if (!enable) { if (!enable) {
return; return;
} }
@ -30,6 +35,11 @@ exports.startMonitoring = (enable, eventBus, eventStore) => {
help: 'Number of times a toggle has been updated', help: 'Number of times a toggle has been updated',
labelNames: ['toggle'], labelNames: ['toggle'],
}); });
const featureToggleUsageTotal = new client.Counter({
name: 'feature_toggle_usage_total',
help: 'Number of times a feature toggle has been used',
labelNames: ['toggle', 'active'],
});
eventBus.on(events.REQUEST_TIME, ({ path, method, time, statusCode }) => { eventBus.on(events.REQUEST_TIME, ({ path, method, time, statusCode }) => {
requestDuration.labels(path, method, statusCode).observe(time); requestDuration.labels(path, method, statusCode).observe(time);
@ -47,4 +57,11 @@ exports.startMonitoring = (enable, eventBus, eventStore) => {
eventStore.on(FEATURE_REVIVED, ({ data }) => { eventStore.on(FEATURE_REVIVED, ({ data }) => {
featureToggleUpdateTotal.labels(data.name).inc(); featureToggleUpdateTotal.labels(data.name).inc();
}); });
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);
}
});
}; };

View File

@ -4,13 +4,14 @@ const { test } = require('ava');
const { EventEmitter } = require('events'); const { EventEmitter } = require('events');
const eventBus = new EventEmitter(); const eventBus = new EventEmitter();
const eventStore = new EventEmitter(); const eventStore = new EventEmitter();
const clientMetricsStore = new EventEmitter();
const { REQUEST_TIME } = require('./events'); const { REQUEST_TIME } = require('./events');
const { FEATURE_UPDATED } = require('./event-type'); const { FEATURE_UPDATED } = require('./event-type');
const { startMonitoring } = require('./metrics'); const { startMonitoring } = require('./metrics');
const { register: prometheusRegister } = require('prom-client'); const { register: prometheusRegister } = require('prom-client');
test.before(() => { test.before(() => {
startMonitoring(true, eventBus, eventStore); startMonitoring(true, eventBus, eventStore, clientMetricsStore);
}); });
test('should collect metrics for requests', t => { test('should collect metrics for requests', t => {
@ -36,3 +37,22 @@ test('should collect metrics for updated toggles', t => {
const metrics = prometheusRegister.metrics(); const metrics = prometheusRegister.metrics();
t.regex(metrics, /feature_toggle_update_total{toggle="TestToggle"} 1/); t.regex(metrics, /feature_toggle_update_total{toggle="TestToggle"} 1/);
}); });
test('should collect metrics for client metric reports', t => {
clientMetricsStore.emit('metrics', {
bucket: {
toggles: {
TestToggle: {
yes: 10,
no: 5,
},
},
},
});
const metrics = prometheusRegister.metrics();
t.regex(
metrics,
/feature_toggle_usage_total{toggle="TestToggle",active="true"} 10\nfeature_toggle_usage_total{toggle="TestToggle",active="false"} 5/
);
});

View File

@ -26,7 +26,12 @@ function createApp(options) {
); );
const app = getApp(config); const app = getApp(config);
startMonitoring(options.serverMetrics, eventBus, stores.eventStore); startMonitoring(
options.serverMetrics,
eventBus,
stores.eventStore,
stores.clientMetricsStore
);
const server = app.listen({ port: options.port, host: options.host }, () => const server = app.listen({ port: options.port, host: options.host }, () =>
logger.info(`Unleash started on port ${server.address().port}`) logger.info(`Unleash started on port ${server.address().port}`)