From 8b38e32f4bb3343512864c00bf5b814509e964f4 Mon Sep 17 00:00:00 2001 From: gonzalezzfelipe Date: Wed, 5 Jun 2024 17:53:54 -0300 Subject: [PATCH] chore: Add usage metric --- operator/src/metrics.rs | 29 +++++++++++++++++++++++++++-- proxy/src/main.rs | 12 ++++++++---- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/operator/src/metrics.rs b/operator/src/metrics.rs index 8f92000..5498994 100644 --- a/operator/src/metrics.rs +++ b/operator/src/metrics.rs @@ -15,6 +15,7 @@ use crate::{get_config, Config, Error, KupoPort, State}; #[derive(Clone)] pub struct Metrics { pub dcu: IntCounterVec, + pub usage: IntCounterVec, pub reconcile_failures: IntCounterVec, pub metrics_failures: IntCounterVec, } @@ -27,6 +28,12 @@ impl Default for Metrics { ) .unwrap(); + let usage = IntCounterVec::new( + opts!("usage", "Feature usage",), + &["feature", "project", "resource_name", "tier"], + ) + .unwrap(); + let reconcile_failures = IntCounterVec::new( opts!( "kupo_operator_crd_reconciliation_errors_total", @@ -47,6 +54,7 @@ impl Default for Metrics { Metrics { dcu, + usage, reconcile_failures, metrics_failures, } @@ -58,6 +66,7 @@ impl Metrics { registry.register(Box::new(self.reconcile_failures.clone()))?; registry.register(Box::new(self.metrics_failures.clone()))?; registry.register(Box::new(self.dcu.clone()))?; + registry.register(Box::new(self.usage.clone()))?; Ok(self) } @@ -85,6 +94,15 @@ impl Metrics { .with_label_values(&[project, &service, &service_type, tenancy]) .inc_by(dcu); } + + pub fn count_usage(&self, project: &str, resource_name: &str, tier: &str, value: f64) { + let feature = &KupoPort::kind(&()); + let value: u64 = value.ceil() as u64; + + self.usage + .with_label_values(&[feature, project, resource_name, tier]) + .inc_by(value); + } } async fn api_get_metrics( @@ -180,7 +198,7 @@ pub fn run_metrics_collector(state: Arc) { info!("collecting metrics running"); let config = get_config(); - let project_regex = Regex::new(r"prj-(.+)\..+").unwrap(); + let project_regex = Regex::new(r"prj-(.+)\.(.+)$").unwrap(); let network_regex = Regex::new(r"kupo-([\w-]+)-.+").unwrap(); let mut last_execution = Utc::now(); @@ -193,7 +211,7 @@ pub fn run_metrics_collector(state: Arc) { last_execution = end; let query = format!( - "sum by (consumer, exported_instance) (increase(kupo_proxy_http_total_request{{status_code!~\"401|429|503\"}}[{start}s] @ {}))", + "sum by (consumer, exported_instance, tier) (increase(kupo_proxy_http_total_request{{status_code!~\"401|429|503\"}}[{start}s] @ {}))", end.timestamp_millis() / 1000 ); @@ -209,6 +227,7 @@ pub fn run_metrics_collector(state: Arc) { if result.value == 0.0 || result.metric.consumer.is_none() || result.metric.exported_instance.is_none() + || result.metric.tier.is_none() { continue; } @@ -221,6 +240,7 @@ pub fn run_metrics_collector(state: Arc) { } let project_captures = project_captures.unwrap(); let project = project_captures.get(1).unwrap().as_str(); + let resource_name = project_captures.get(2).unwrap().as_str(); let instance = result.metric.exported_instance.unwrap(); let network_captures = network_regex.captures(&instance); @@ -230,6 +250,7 @@ pub fn run_metrics_collector(state: Arc) { } let network_captures = network_captures.unwrap(); let network = network_captures.get(1).unwrap().as_str(); + let tier = result.metric.tier.unwrap(); let dcu_per_request = config.dcu_per_request.get(network); if dcu_per_request.is_none() { @@ -245,6 +266,9 @@ pub fn run_metrics_collector(state: Arc) { let dcu = result.value * dcu_per_request; state.metrics.count_dcu_consumed(project, network, dcu); + state + .metrics + .count_usage(project, resource_name, &tier, result.value); } } }); @@ -322,6 +346,7 @@ pub fn run_kong_metrics_collector(state: Arc) { struct PrometheusDataResultMetric { consumer: Option, exported_instance: Option, + tier: Option, } #[derive(Debug, Deserialize)] diff --git a/proxy/src/main.rs b/proxy/src/main.rs index 1a2c394..d1b27ec 100644 --- a/proxy/src/main.rs +++ b/proxy/src/main.rs @@ -164,7 +164,7 @@ impl Metrics { pub fn new() -> Self { let http_total_request = register_int_counter_vec!( opts!("kupo_proxy_http_total_request", "Total http request",), - &["consumer", "namespace", "instance", "status_code",] + &["consumer", "namespace", "instance", "status_code", "tier"] ) .unwrap(); @@ -178,10 +178,14 @@ impl Metrics { instance: &str, status: &u16, ) { - let consumer = &consumer.to_string(); - self.http_total_request - .with_label_values(&[consumer, namespace, instance, &status.to_string()]) + .with_label_values(&[ + &consumer.to_string(), + namespace, + instance, + &status.to_string(), + &consumer.tier, + ]) .inc() } }