Skip to content

Commit 40ca4f0

Browse files
fix: Track connection time and reset connection metric (#58)
* fix: Track connection time and reset connection metric * Fix metric * Remove change in bootstrap * Add explanatory comment * Rename metric
1 parent f4640e2 commit 40ca4f0

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

Diff for: operator/src/config.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Config {
1414
pub dns_zone: String,
1515
pub extension_name: String,
1616
pub api_key_salt: String,
17-
pub dcu_per_frame: HashMap<String, f64>,
17+
pub dcu_per_second: HashMap<String, f64>,
1818
pub metrics_delay: Duration,
1919
pub prometheus_url: String,
2020
}
@@ -24,7 +24,9 @@ impl Config {
2424
let dns_zone = env::var("DNS_ZONE").unwrap_or("demeter.run".into());
2525
let extension_name = env::var("EXTENSION_NAME").unwrap_or("ogmios-m1".into());
2626
let api_key_salt = env::var("API_KEY_SALT").unwrap_or("ogmios-salt".into());
27-
let dcu_per_frame = env::var("DCU_PER_FRAME")
27+
28+
// This will be deprecated soon. Naming is like this for compatibility
29+
let dcu_per_second = env::var("DCU_PER_FRAME")
2830
.expect("DCU_PER_FRAME must be set")
2931
.split(',')
3032
.map(|pair| {
@@ -49,7 +51,7 @@ impl Config {
4951
dns_zone,
5052
extension_name,
5153
api_key_salt,
52-
dcu_per_frame,
54+
dcu_per_second,
5355
metrics_delay,
5456
prometheus_url,
5557
}

Diff for: operator/src/metrics.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,30 @@ pub async fn run_metrics_collector(state: Arc<State>) {
117117
tokio::time::sleep(config.metrics_delay).await;
118118

119119
let end = Utc::now();
120-
let start = (end - last_execution).num_seconds();
120+
let interval = (end - last_execution).num_seconds();
121121

122122
last_execution = end;
123123

124124
let query = format!(
125-
"sum by (consumer, route, tier) (increase(ogmios_proxy_ws_total_frame[{start}s] @ {}))",
125+
"sum by (consumer, route, tier) (avg_over_time(ogmios_proxy_total_connections[{interval}s] @ {}))",
126126
end.timestamp_millis() / 1000
127127
);
128128

129-
let result = client
129+
let response = match client
130130
.get(format!("{}/query?query={query}", config.prometheus_url))
131131
.send()
132-
.await;
133-
134-
if let Err(err) = result {
135-
error!(error = err.to_string(), "error to make prometheus request");
136-
state
137-
.metrics
138-
.metrics_failure(&Error::HttpError(err.to_string()));
139-
continue;
140-
}
132+
.await
133+
{
134+
Ok(response) => response,
135+
Err(err) => {
136+
error!(error = err.to_string(), "error to make prometheus request");
137+
state
138+
.metrics
139+
.metrics_failure(&Error::HttpError(err.to_string()));
140+
continue;
141+
}
142+
};
141143

142-
let response = result.unwrap();
143144
let status = response.status();
144145
if status.is_client_error() || status.is_server_error() {
145146
error!(status = status.to_string(), "request status code fail");
@@ -176,25 +177,27 @@ pub async fn run_metrics_collector(state: Arc<State>) {
176177
let network_captures = network_captures.unwrap();
177178
let network = network_captures.get(1).unwrap().as_str();
178179

179-
let dcu_per_frame = config.dcu_per_frame.get(network);
180-
if dcu_per_frame.is_none() {
180+
let dcu_per_second = config.dcu_per_second.get(network);
181+
if dcu_per_second.is_none() {
181182
let error = Error::ConfigError(format!(
182-
"dcu_per_frame not configured to {} network",
183+
"dcu_per_second not configured to {} network",
183184
network
184185
));
185186
error!(error = error.to_string());
186187
state.metrics.metrics_failure(&error);
187188
continue;
188189
}
189-
let dcu_per_frame = dcu_per_frame.unwrap();
190190

191-
let dcu = result.value * dcu_per_frame;
192-
state.metrics.count_dcu_consumed(project, network, dcu);
191+
let dcu_per_second = dcu_per_second.unwrap();
192+
let total_exec_time = result.value * (interval as f64);
193193

194+
let dcu = total_exec_time * dcu_per_second;
195+
196+
state.metrics.count_dcu_consumed(project, network, dcu);
194197
if let Some(tier) = result.metric.tier {
195198
state
196199
.metrics
197-
.count_usage(project, resource_name, &tier, result.value);
200+
.count_usage(project, resource_name, &tier, total_exec_time);
198201
}
199202
}
200203
}

Diff for: proxy/src/metrics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Metrics {
3131

3232
let ws_total_connection = IntGaugeVec::new(
3333
opts!(
34-
"ogmios_proxy_ws_total_connection",
34+
"ogmios_proxy_total_connections",
3535
"total of websocket connection",
3636
),
3737
&["namespace", "instance", "route", "consumer", "tier"],

0 commit comments

Comments
 (0)