1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

fix: metrics for toggle count and version (#565)

This commit is contained in:
Ivar Conradi Østhus 2020-02-28 14:50:32 +01:00 committed by GitHub
parent 713da6e2fe
commit aac12fbee8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 3 deletions

View File

@ -48,6 +48,14 @@ class FeatureToggleStore {
.map(this.rowToFeature);
}
count() {
return this.db
.count('*')
.from(TABLE)
.where({ archived: 0 })
.then(res => Number(res[0].count));
}
getFeature(name) {
return this.db
.first(FEATURE_COLUMNS)

View File

@ -7,12 +7,15 @@ const {
FEATURE_ARCHIVED,
FEATURE_REVIVED,
} = require('./event-type');
const { version } = require('./routes/api-def');
const THREE_HOURS = 3 * 60 * 60 * 1000;
exports.startMonitoring = (
enable,
eventBus,
eventStore,
clientMetricsStore
clientMetricsStore,
featureToggleStore
) => {
if (!enable) {
return;
@ -44,6 +47,20 @@ exports.startMonitoring = (
help: 'Number of times a feature toggle has been used',
labelNames: ['toggle', 'active', 'appName'],
});
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);
eventBus.on(events.REQUEST_TIME, ({ path, method, time, statusCode }) => {
requestDuration.labels(path, method, statusCode).observe(time);

View File

@ -11,7 +11,16 @@ const { startMonitoring } = require('./metrics');
const { register: prometheusRegister } = require('prom-client');
test.before(() => {
startMonitoring(true, eventBus, eventStore, clientMetricsStore);
const featureToggleStore = {
count: () => 123,
};
startMonitoring(
true,
eventBus,
eventStore,
clientMetricsStore,
featureToggleStore
);
});
test('should collect metrics for requests', t => {
@ -70,3 +79,8 @@ test('should collect metrics for db query timings', t => {
/db_query_duration_seconds{quantile="0\.99",store="foo",action="bar"} 0.1337/
);
});
test('should collect metrics for feature toggle size', t => {
const metrics = prometheusRegister.metrics();
t.regex(metrics, /feature_toggles_total{version="(.*)"} 123/);
});

View File

@ -33,7 +33,8 @@ async function createApp(options) {
options.serverMetrics,
eventBus,
stores.eventStore,
stores.clientMetricsStore
stores.clientMetricsStore,
stores.featureToggleStore
);
if (typeof config.eventHook === 'function') {