diff --git a/operator/src/config.rs b/operator/src/config.rs index 5f095cd..4f0ad96 100644 --- a/operator/src/config.rs +++ b/operator/src/config.rs @@ -14,7 +14,7 @@ pub struct Config { pub dns_zone: String, pub extension_name: String, pub api_key_salt: String, - pub dcu_per_frame: HashMap, + pub dcu_per_second: HashMap, pub metrics_delay: Duration, pub prometheus_url: String, } @@ -24,7 +24,9 @@ impl Config { let dns_zone = env::var("DNS_ZONE").unwrap_or("demeter.run".into()); let extension_name = env::var("EXTENSION_NAME").unwrap_or("ogmios-m1".into()); let api_key_salt = env::var("API_KEY_SALT").unwrap_or("ogmios-salt".into()); - let dcu_per_frame = env::var("DCU_PER_FRAME") + + // This will be deprecated soon. Naming is like this for compatibility + let dcu_per_second = env::var("DCU_PER_FRAME") .expect("DCU_PER_FRAME must be set") .split(',') .map(|pair| { @@ -49,7 +51,7 @@ impl Config { dns_zone, extension_name, api_key_salt, - dcu_per_frame, + dcu_per_second, metrics_delay, prometheus_url, } diff --git a/operator/src/metrics.rs b/operator/src/metrics.rs index 896207d..8192986 100644 --- a/operator/src/metrics.rs +++ b/operator/src/metrics.rs @@ -117,29 +117,30 @@ pub async fn run_metrics_collector(state: Arc) { tokio::time::sleep(config.metrics_delay).await; let end = Utc::now(); - let start = (end - last_execution).num_seconds(); + let interval = (end - last_execution).num_seconds(); last_execution = end; let query = format!( - "sum by (consumer, route, tier) (increase(ogmios_proxy_ws_total_frame[{start}s] @ {}))", + "sum by (consumer, route, tier) (avg_over_time(ogmios_proxy_total_connections[{interval}s] @ {}))", end.timestamp_millis() / 1000 ); - let result = client + let response = match client .get(format!("{}/query?query={query}", config.prometheus_url)) .send() - .await; - - if let Err(err) = result { - error!(error = err.to_string(), "error to make prometheus request"); - state - .metrics - .metrics_failure(&Error::HttpError(err.to_string())); - continue; - } + .await + { + Ok(response) => response, + Err(err) => { + error!(error = err.to_string(), "error to make prometheus request"); + state + .metrics + .metrics_failure(&Error::HttpError(err.to_string())); + continue; + } + }; - let response = result.unwrap(); let status = response.status(); if status.is_client_error() || status.is_server_error() { error!(status = status.to_string(), "request status code fail"); @@ -176,25 +177,27 @@ pub async fn run_metrics_collector(state: Arc) { let network_captures = network_captures.unwrap(); let network = network_captures.get(1).unwrap().as_str(); - let dcu_per_frame = config.dcu_per_frame.get(network); - if dcu_per_frame.is_none() { + let dcu_per_second = config.dcu_per_second.get(network); + if dcu_per_second.is_none() { let error = Error::ConfigError(format!( - "dcu_per_frame not configured to {} network", + "dcu_per_second not configured to {} network", network )); error!(error = error.to_string()); state.metrics.metrics_failure(&error); continue; } - let dcu_per_frame = dcu_per_frame.unwrap(); - let dcu = result.value * dcu_per_frame; - state.metrics.count_dcu_consumed(project, network, dcu); + let dcu_per_second = dcu_per_second.unwrap(); + let total_exec_time = result.value * (interval as f64); + let dcu = total_exec_time * dcu_per_second; + + state.metrics.count_dcu_consumed(project, network, dcu); if let Some(tier) = result.metric.tier { state .metrics - .count_usage(project, resource_name, &tier, result.value); + .count_usage(project, resource_name, &tier, total_exec_time); } } } diff --git a/proxy/src/metrics.rs b/proxy/src/metrics.rs index 0b32f87..dfc86e5 100644 --- a/proxy/src/metrics.rs +++ b/proxy/src/metrics.rs @@ -31,7 +31,7 @@ impl Metrics { let ws_total_connection = IntGaugeVec::new( opts!( - "ogmios_proxy_ws_total_connection", + "ogmios_proxy_total_connections", "total of websocket connection", ), &["namespace", "instance", "route", "consumer", "tier"],