From a7ed7557ec43c19755ae05aaedf9f0f5edaff3ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 8 Sep 2022 11:01:27 +0200 Subject: [PATCH] fix: add env and project labels to feature updated metrics. (#2043) --- src/lib/metrics.test.ts | 3 +- src/lib/metrics.ts | 77 ++++++++++++++++++++++++++++++++--------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/lib/metrics.test.ts b/src/lib/metrics.test.ts index a8688873d7..7d0e16e700 100644 --- a/src/lib/metrics.test.ts +++ b/src/lib/metrics.test.ts @@ -57,12 +57,13 @@ test('should collect metrics for requests', async () => { test('should collect metrics for updated toggles', async () => { stores.eventStore.emit(FEATURE_UPDATED, { featureName: 'TestToggle', + project: 'default', data: { name: 'TestToggle' }, }); const metrics = await prometheusRegister.metrics(); expect(metrics).toMatch( - /feature_toggle_update_total{toggle="TestToggle"} 1/, + /feature_toggle_update_total{toggle="TestToggle",project="default",environment="default"} 1/, ); }); diff --git a/src/lib/metrics.ts b/src/lib/metrics.ts index b1af637573..be569136c4 100644 --- a/src/lib/metrics.ts +++ b/src/lib/metrics.ts @@ -10,6 +10,10 @@ import { FEATURE_STRATEGY_ADD, FEATURE_STRATEGY_REMOVE, FEATURE_STRATEGY_UPDATE, + FEATURE_ENVIRONMENT_ENABLED, + FEATURE_ENVIRONMENT_DISABLED, + FEATURE_VARIANTS_UPDATED, + FEATURE_METADATA_UPDATED, FEATURE_UPDATED, CLIENT_METRICS, CLIENT_REGISTER, @@ -68,8 +72,8 @@ export default class MetricsMonitor { }); const featureToggleUpdateTotal = new client.Counter({ name: 'feature_toggle_update_total', - help: 'Number of times a toggle has been updated', - labelNames: ['toggle'], + help: 'Number of times a toggle has been updated. Environment label would be "n/a" when it is not available, e.g. when a feature toggle is created.', + labelNames: ['toggle', 'project', 'environment'], }); const featureToggleUsageTotal = new client.Counter({ name: 'feature_toggle_usage_total', @@ -148,26 +152,65 @@ export default class MetricsMonitor { dbDuration.labels(store, action).observe(time); }); - eventStore.on(FEATURE_CREATED, ({ featureName }) => { - featureToggleUpdateTotal.labels(featureName).inc(); + eventStore.on(FEATURE_CREATED, ({ featureName, project }) => { + featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc(); }); - eventStore.on(FEATURE_UPDATED, ({ featureName }) => { - featureToggleUpdateTotal.labels(featureName).inc(); + eventStore.on(FEATURE_VARIANTS_UPDATED, ({ featureName, project }) => { + featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc(); }); - eventStore.on(FEATURE_STRATEGY_ADD, ({ featureName }) => { - featureToggleUpdateTotal.labels(featureName).inc(); + eventStore.on(FEATURE_METADATA_UPDATED, ({ featureName, project }) => { + featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc(); }); - eventStore.on(FEATURE_STRATEGY_REMOVE, ({ featureName }) => { - featureToggleUpdateTotal.labels(featureName).inc(); + eventStore.on(FEATURE_UPDATED, ({ featureName, project }) => { + featureToggleUpdateTotal + .labels(featureName, project, 'default') + .inc(); }); - eventStore.on(FEATURE_STRATEGY_UPDATE, ({ featureName }) => { - featureToggleUpdateTotal.labels(featureName).inc(); + eventStore.on( + FEATURE_STRATEGY_ADD, + ({ featureName, project, environment }) => { + featureToggleUpdateTotal + .labels(featureName, project, environment) + .inc(); + }, + ); + eventStore.on( + FEATURE_STRATEGY_REMOVE, + ({ featureName, project, environment }) => { + featureToggleUpdateTotal + .labels(featureName, project, environment) + .inc(); + }, + ); + eventStore.on( + FEATURE_STRATEGY_UPDATE, + ({ featureName, project, environment }) => { + featureToggleUpdateTotal + .labels(featureName, project, environment) + .inc(); + }, + ); + eventStore.on( + FEATURE_ENVIRONMENT_DISABLED, + ({ featureName, project, environment }) => { + featureToggleUpdateTotal + .labels(featureName, project, environment) + .inc(); + }, + ); + eventStore.on( + FEATURE_ENVIRONMENT_ENABLED, + ({ featureName, project, environment }) => { + featureToggleUpdateTotal + .labels(featureName, project, environment) + .inc(); + }, + ); + eventStore.on(FEATURE_ARCHIVED, ({ featureName, project }) => { + featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc(); }); - eventStore.on(FEATURE_ARCHIVED, ({ featureName }) => { - featureToggleUpdateTotal.labels(featureName).inc(); - }); - eventStore.on(FEATURE_REVIVED, ({ featureName }) => { - featureToggleUpdateTotal.labels(featureName).inc(); + eventStore.on(FEATURE_REVIVED, ({ featureName, project }) => { + featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc(); }); eventBus.on(CLIENT_METRICS, (m) => {