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:
parent
713da6e2fe
commit
aac12fbee8
@ -48,6 +48,14 @@ class FeatureToggleStore {
|
|||||||
.map(this.rowToFeature);
|
.map(this.rowToFeature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count() {
|
||||||
|
return this.db
|
||||||
|
.count('*')
|
||||||
|
.from(TABLE)
|
||||||
|
.where({ archived: 0 })
|
||||||
|
.then(res => Number(res[0].count));
|
||||||
|
}
|
||||||
|
|
||||||
getFeature(name) {
|
getFeature(name) {
|
||||||
return this.db
|
return this.db
|
||||||
.first(FEATURE_COLUMNS)
|
.first(FEATURE_COLUMNS)
|
||||||
|
@ -7,12 +7,15 @@ const {
|
|||||||
FEATURE_ARCHIVED,
|
FEATURE_ARCHIVED,
|
||||||
FEATURE_REVIVED,
|
FEATURE_REVIVED,
|
||||||
} = require('./event-type');
|
} = require('./event-type');
|
||||||
|
const { version } = require('./routes/api-def');
|
||||||
|
const THREE_HOURS = 3 * 60 * 60 * 1000;
|
||||||
|
|
||||||
exports.startMonitoring = (
|
exports.startMonitoring = (
|
||||||
enable,
|
enable,
|
||||||
eventBus,
|
eventBus,
|
||||||
eventStore,
|
eventStore,
|
||||||
clientMetricsStore
|
clientMetricsStore,
|
||||||
|
featureToggleStore
|
||||||
) => {
|
) => {
|
||||||
if (!enable) {
|
if (!enable) {
|
||||||
return;
|
return;
|
||||||
@ -44,6 +47,20 @@ exports.startMonitoring = (
|
|||||||
help: 'Number of times a feature toggle has been used',
|
help: 'Number of times a feature toggle has been used',
|
||||||
labelNames: ['toggle', 'active', 'appName'],
|
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 }) => {
|
eventBus.on(events.REQUEST_TIME, ({ path, method, time, statusCode }) => {
|
||||||
requestDuration.labels(path, method, statusCode).observe(time);
|
requestDuration.labels(path, method, statusCode).observe(time);
|
||||||
|
@ -11,7 +11,16 @@ const { startMonitoring } = require('./metrics');
|
|||||||
const { register: prometheusRegister } = require('prom-client');
|
const { register: prometheusRegister } = require('prom-client');
|
||||||
|
|
||||||
test.before(() => {
|
test.before(() => {
|
||||||
startMonitoring(true, eventBus, eventStore, clientMetricsStore);
|
const featureToggleStore = {
|
||||||
|
count: () => 123,
|
||||||
|
};
|
||||||
|
startMonitoring(
|
||||||
|
true,
|
||||||
|
eventBus,
|
||||||
|
eventStore,
|
||||||
|
clientMetricsStore,
|
||||||
|
featureToggleStore
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should collect metrics for requests', t => {
|
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/
|
/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/);
|
||||||
|
});
|
||||||
|
@ -33,7 +33,8 @@ async function createApp(options) {
|
|||||||
options.serverMetrics,
|
options.serverMetrics,
|
||||||
eventBus,
|
eventBus,
|
||||||
stores.eventStore,
|
stores.eventStore,
|
||||||
stores.clientMetricsStore
|
stores.clientMetricsStore,
|
||||||
|
stores.featureToggleStore
|
||||||
);
|
);
|
||||||
|
|
||||||
if (typeof config.eventHook === 'function') {
|
if (typeof config.eventHook === 'function') {
|
||||||
|
Loading…
Reference in New Issue
Block a user