From 00aabbef3061e9f92144d454f31bac8f41764770 Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Wed, 18 Sep 2024 13:58:55 +0200 Subject: [PATCH] feat: added feature_toggle_usage_total counter (#532) --- server/src/metrics/client_metrics.rs | 14 +++++++++++++- server/src/prom_metrics.rs | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/server/src/metrics/client_metrics.rs b/server/src/metrics/client_metrics.rs index e6fe7369..c5c71613 100644 --- a/server/src/metrics/client_metrics.rs +++ b/server/src/metrics/client_metrics.rs @@ -4,7 +4,7 @@ use chrono::{DateTime, Utc}; use dashmap::DashMap; use iter_tools::Itertools; use lazy_static::lazy_static; -use prometheus::{register_histogram, Histogram}; +use prometheus::{register_histogram, register_int_counter_vec, Histogram, IntCounterVec}; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, @@ -25,6 +25,12 @@ lazy_static! { vec![1000.0, 10000.0, 20000.0, 50000.0, 75000.0, 100000.0, 250000.0, 500000.0, 1000000.0] ) .unwrap(); + pub static ref FEATURE_TOGGLE_USAGE_TOTAL: IntCounterVec = register_int_counter_vec!( + "feature_toggle_usage_total", + "Number of times a feature flag has been used", + &["appName", "toggle", "active"] + ) + .unwrap(); } #[derive(Debug, PartialEq, Eq, Hash, Clone)] @@ -329,6 +335,12 @@ impl MetricsCache { pub fn sink_metrics(&self, metrics: &[ClientMetricsEnv]) { debug!("Sinking {} metrics", metrics.len()); for metric in metrics.iter() { + FEATURE_TOGGLE_USAGE_TOTAL + .with_label_values(&[&metric.app_name, &metric.feature_name, "true"]) + .inc_by(metric.yes as u64); + FEATURE_TOGGLE_USAGE_TOTAL + .with_label_values(&[&metric.app_name, &metric.feature_name, "false"]) + .inc_by(metric.no as u64); self.metrics .entry(MetricsKey { app_name: metric.app_name.clone(), diff --git a/server/src/prom_metrics.rs b/server/src/prom_metrics.rs index 74773fca..615aa404 100644 --- a/server/src/prom_metrics.rs +++ b/server/src/prom_metrics.rs @@ -138,6 +138,11 @@ fn register_custom_metrics(registry: &prometheus::Registry) { crate::http::unleash_client::UPSTREAM_VERSION.clone(), )) .unwrap(); + registry + .register(Box::new( + crate::metrics::client_metrics::FEATURE_TOGGLE_USAGE_TOTAL.clone(), + )) + .unwrap(); } #[cfg(test)]