Skip to content

Commit 66d780f

Browse files
chore: Add usage metric
1 parent 5c86353 commit 66d780f

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

operator/src/metrics.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{get_config, Error, OgmiosPort, State};
1212
#[derive(Clone)]
1313
pub struct Metrics {
1414
pub dcu: IntCounterVec,
15+
pub usage: IntCounterVec,
1516
pub reconcile_failures: IntCounterVec,
1617
pub metrics_failures: IntCounterVec,
1718
}
@@ -24,6 +25,12 @@ impl Default for Metrics {
2425
)
2526
.unwrap();
2627

28+
let usage = IntCounterVec::new(
29+
opts!("usage", "Feature usage",),
30+
&["feature", "project", "resource_name", "tier"],
31+
)
32+
.unwrap();
33+
2734
let reconcile_failures = IntCounterVec::new(
2835
opts!(
2936
"ogmios_operator_reconciliation_errors_total",
@@ -44,6 +51,7 @@ impl Default for Metrics {
4451

4552
Metrics {
4653
reconcile_failures,
54+
usage,
4755
dcu,
4856
metrics_failures,
4957
}
@@ -55,6 +63,7 @@ impl Metrics {
5563
registry.register(Box::new(self.dcu.clone()))?;
5664
registry.register(Box::new(self.metrics_failures.clone()))?;
5765
registry.register(Box::new(self.reconcile_failures.clone()))?;
66+
registry.register(Box::new(self.usage.clone()))?;
5867

5968
Ok(self)
6069
}
@@ -82,6 +91,15 @@ impl Metrics {
8291
.with_label_values(&[project, &service, &service_type, tenancy])
8392
.inc_by(dcu);
8493
}
94+
95+
pub fn count_usage(&self, project: &str, resource_name: &str, tier: &str, value: f64) {
96+
let feature = &OgmiosPort::kind(&());
97+
let value: u64 = value.ceil() as u64;
98+
99+
self.usage
100+
.with_label_values(&[feature, project, resource_name, tier])
101+
.inc_by(value);
102+
}
85103
}
86104

87105
#[instrument("metrics collector run", skip_all)]
@@ -91,7 +109,7 @@ pub async fn run_metrics_collector(state: Arc<State>) {
91109

92110
let config = get_config();
93111
let client = reqwest::Client::builder().build().unwrap();
94-
let project_regex = Regex::new(r"prj-(.+)\..+").unwrap();
112+
let project_regex = Regex::new(r"prj-(.+)\.(.+)$").unwrap();
95113
let network_regex = Regex::new(r"([\w-]+)-.+").unwrap();
96114
let mut last_execution = Utc::now();
97115

@@ -104,7 +122,7 @@ pub async fn run_metrics_collector(state: Arc<State>) {
104122
last_execution = end;
105123

106124
let query = format!(
107-
"sum by (consumer, route) (increase(ogmios_proxy_ws_total_frame[{start}s] @ {}))",
125+
"sum by (consumer, route, tier) (increase(ogmios_proxy_ws_total_frame[{start}s] @ {}))",
108126
end.timestamp_millis() / 1000
109127
);
110128

@@ -148,6 +166,7 @@ pub async fn run_metrics_collector(state: Arc<State>) {
148166
}
149167
let project_captures = project_captures.unwrap();
150168
let project = project_captures.get(1).unwrap().as_str();
169+
let resource_name = project_captures.get(2).unwrap().as_str();
151170

152171
let route = result.metric.route.unwrap();
153172
let network_captures = network_regex.captures(&route);
@@ -171,6 +190,12 @@ pub async fn run_metrics_collector(state: Arc<State>) {
171190

172191
let dcu = result.value * dcu_per_frame;
173192
state.metrics.count_dcu_consumed(project, network, dcu);
193+
194+
if let Some(tier) = result.metric.tier {
195+
state
196+
.metrics
197+
.count_usage(project, resource_name, &tier, result.value);
198+
}
174199
}
175200
}
176201
});
@@ -180,6 +205,7 @@ pub async fn run_metrics_collector(state: Arc<State>) {
180205
struct PrometheusDataResultMetric {
181206
consumer: Option<String>,
182207
route: Option<String>,
208+
tier: Option<String>,
183209
}
184210

185211
#[derive(Debug, Deserialize)]

proxy/src/metrics.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl Metrics {
2525
pub fn try_new(registry: Registry) -> Result<Self, Box<dyn Error>> {
2626
let ws_total_frame = IntCounterVec::new(
2727
opts!("ogmios_proxy_ws_total_frame", "total of websocket frame",),
28-
&["namespace", "instance", "route", "consumer"],
28+
&["namespace", "instance", "route", "consumer", "tier"],
2929
)
3030
.unwrap();
3131

@@ -34,7 +34,7 @@ impl Metrics {
3434
"ogmios_proxy_ws_total_connection",
3535
"total of websocket connection",
3636
),
37-
&["namespace", "instance", "route", "consumer"],
37+
&["namespace", "instance", "route", "consumer", "tier"],
3838
)
3939
.unwrap();
4040

@@ -47,6 +47,7 @@ impl Metrics {
4747
"status_code",
4848
"protocol",
4949
"consumer",
50+
"tier",
5051
],
5152
)
5253
.unwrap();
@@ -72,14 +73,15 @@ impl Metrics {
7273
.consumer
7374
.as_ref()
7475
.unwrap_or(&Consumer::default())
75-
.to_string();
76+
.clone();
7677

7778
self.ws_total_frame
7879
.with_label_values(&[
7980
&proxy_req.namespace,
8081
&proxy_req.instance,
8182
&proxy_req.host,
82-
&consumer,
83+
&consumer.to_string(),
84+
&consumer.tier,
8385
])
8486
.inc()
8587
}
@@ -89,14 +91,15 @@ impl Metrics {
8991
.consumer
9092
.as_ref()
9193
.unwrap_or(&Consumer::default())
92-
.to_string();
94+
.clone();
9395

9496
self.ws_total_connection
9597
.with_label_values(&[
9698
&proxy_req.namespace,
9799
&proxy_req.instance,
98100
&proxy_req.host,
99-
&consumer,
101+
&consumer.to_string(),
102+
&consumer.tier,
100103
])
101104
.inc()
102105
}
@@ -106,14 +109,15 @@ impl Metrics {
106109
.consumer
107110
.as_ref()
108111
.unwrap_or(&Consumer::default())
109-
.to_string();
112+
.clone();
110113

111114
self.ws_total_connection
112115
.with_label_values(&[
113116
&proxy_req.namespace,
114117
&proxy_req.instance,
115118
&proxy_req.host,
116-
&consumer,
119+
&consumer.to_string(),
120+
&consumer.tier,
117121
])
118122
.dec()
119123
}
@@ -123,7 +127,7 @@ impl Metrics {
123127
.consumer
124128
.as_ref()
125129
.unwrap_or(&Consumer::default())
126-
.to_string();
130+
.clone();
127131

128132
self.http_total_request
129133
.with_label_values(&[
@@ -132,7 +136,8 @@ impl Metrics {
132136
&proxy_req.host,
133137
&status_code.as_u16().to_string(),
134138
&proxy_req.protocol.to_string(),
135-
&consumer,
139+
&consumer.to_string(),
140+
&consumer.tier,
136141
])
137142
.inc()
138143
}

0 commit comments

Comments
 (0)