Skip to content

Commit

Permalink
fix: Track connection time and reset connection metric (#58)
Browse files Browse the repository at this point in the history
* fix: Track connection time and reset connection metric

* Fix metric

* Remove change in bootstrap

* Add explanatory comment

* Rename metric
  • Loading branch information
gonzalezzfelipe authored Jul 10, 2024
1 parent f4640e2 commit 40ca4f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
8 changes: 5 additions & 3 deletions operator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, f64>,
pub dcu_per_second: HashMap<String, f64>,
pub metrics_delay: Duration,
pub prometheus_url: String,
}
Expand All @@ -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| {
Expand All @@ -49,7 +51,7 @@ impl Config {
dns_zone,
extension_name,
api_key_salt,
dcu_per_frame,
dcu_per_second,
metrics_delay,
prometheus_url,
}
Expand Down
43 changes: 23 additions & 20 deletions operator/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,30 @@ pub async fn run_metrics_collector(state: Arc<State>) {
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");
Expand Down Expand Up @@ -176,25 +177,27 @@ pub async fn run_metrics_collector(state: Arc<State>) {
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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion proxy/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 40ca4f0

Please sign in to comment.