Skip to content

Commit 091b8fa

Browse files
fix: Track connection time and reset connection metric
1 parent 74dd8c7 commit 091b8fa

File tree

7 files changed

+42
-37
lines changed

7 files changed

+42
-37
lines changed

bootstrap/feature/main.tf

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ variable "api_key_salt" {
2828
default = "ogmios-salt"
2929
}
3030

31-
variable "dcu_per_frame" {
31+
variable "dcu_per_second" {
3232
type = map(string)
3333
default = {
34-
"mainnet" = "10"
35-
"preprod" = "5"
36-
"preview" = "5"
34+
"mainnet" = "1"
35+
"preprod" = "1"
36+
"preview" = "1"
37+
"vector-testnet" = "1"
3738
}
3839
}
3940

bootstrap/feature/operator.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ resource "kubernetes_deployment_v1" "ogmios_operator" {
6767
}
6868

6969
env {
70-
name = "DCU_PER_FRAME"
71-
value = "mainnet=${var.dcu_per_frame["mainnet"]},preprod=${var.dcu_per_frame["preprod"]},preview=${var.dcu_per_frame["preview"]},vector-testnet=${var.dcu_per_frame["vector-testnet"]}"
70+
name = "DCU_PER_SECOND"
71+
value = "mainnet=${var.dcu_per_second["mainnet"]},preprod=${var.dcu_per_second["preprod"]},preview=${var.dcu_per_second["preview"]},vector-testnet=${var.dcu_per_second["vector-testnet"]}"
7272
}
7373

7474
env {

bootstrap/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module "ogmios_v1_feature" {
2121
metrics_delay = var.metrics_delay
2222
extension_name = var.extension_name
2323
api_key_salt = var.api_key_salt
24-
dcu_per_frame = var.dcu_per_frame
24+
dcu_per_second = var.dcu_per_second
2525
}
2626

2727
module "ogmios_v1_proxy" {

bootstrap/variables.tf

+5-4
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ variable "api_key_salt" {
3434
default = "ogmios-salt"
3535
}
3636

37-
variable "dcu_per_frame" {
37+
variable "dcu_per_second" {
3838
type = map(string)
3939
default = {
40-
"mainnet" = "10"
41-
"preprod" = "5"
42-
"preview" = "5"
40+
"mainnet" = "1"
41+
"preprod" = "1"
42+
"preview" = "1"
43+
"vector-testnet" = "1"
4344
}
4445
}
4546

operator/src/config.rs

+5-5
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,14 +24,14 @@ 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")
28-
.expect("DCU_PER_FRAME must be set")
27+
let dcu_per_second = env::var("DCU_PER_SECOND")
28+
.expect("DCU_PER_SECOND must be set")
2929
.split(',')
3030
.map(|pair| {
3131
let parts: Vec<&str> = pair.split('=').collect();
3232
let dcu = parts[1]
3333
.parse::<f64>()
34-
.expect("DCU_PER_FRAME must be NETWORK=NUMBER");
34+
.expect("DCU_PER_SECOND must be NETWORK=NUMBER");
3535

3636
(parts[0].into(), dcu)
3737
})
@@ -49,7 +49,7 @@ impl Config {
4949
dns_zone,
5050
extension_name,
5151
api_key_salt,
52-
dcu_per_frame,
52+
dcu_per_second,
5353
metrics_delay,
5454
prometheus_url,
5555
}

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_frame[{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_package 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
}

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_connection",
3535
"total of websocket connection",
3636
),
3737
&["namespace", "instance", "route", "consumer", "tier"],

0 commit comments

Comments
 (0)