Skip to content

Commit

Permalink
chore: Add usage metric (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezzfelipe authored Jun 6, 2024
1 parent f85c543 commit 35a9bf5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
30 changes: 28 additions & 2 deletions operator/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{get_config, CardanoNodePort, Config, Error, State};
#[derive(Clone)]
pub struct Metrics {
pub dcu: IntCounterVec,
pub usage: IntCounterVec,
pub reconcile_failures: IntCounterVec,
pub metrics_failures: IntCounterVec,
}
Expand All @@ -28,6 +29,12 @@ impl Default for Metrics {
)
.unwrap();

let usage = IntCounterVec::new(
opts!("usage", "Feature usage",),
&["feature", "project", "resource_name", "tier"],
)
.unwrap();

let reconcile_failures = IntCounterVec::new(
opts!(
"node_operator_crd_reconciliation_errors_total",
Expand All @@ -48,6 +55,7 @@ impl Default for Metrics {

Metrics {
dcu,
usage,
reconcile_failures,
metrics_failures,
}
Expand All @@ -59,6 +67,7 @@ impl Metrics {
registry.register(Box::new(self.reconcile_failures.clone()))?;
registry.register(Box::new(self.metrics_failures.clone()))?;
registry.register(Box::new(self.dcu.clone()))?;
registry.register(Box::new(self.usage.clone()))?;

Ok(self)
}
Expand Down Expand Up @@ -90,6 +99,15 @@ impl Metrics {
.with_label_values(&[project, &service, &service_type, tenancy])
.inc_by(dcu);
}

pub fn count_usage(&self, project: &str, resource_name: &str, tier: &str, value: f64) {
let feature = &CardanoNodePort::kind(&());
let value: u64 = value.ceil() as u64;

self.usage
.with_label_values(&[feature, project, resource_name, tier])
.inc_by(value);
}
}

async fn api_get_metrics(
Expand Down Expand Up @@ -163,7 +181,7 @@ pub fn run_metrics_collector(state: Arc<State>) {
info!("collecting metrics running");

let config = get_config();
let project_regex = Regex::new(r"prj-(.+)\..+").unwrap();
let project_regex = Regex::new(r"prj-(.+)\.(.+)$").unwrap();
let network_regex = Regex::new(r"node-([\w]+)-.+").unwrap();
let mut last_execution = Utc::now();

Expand All @@ -176,7 +194,7 @@ pub fn run_metrics_collector(state: Arc<State>) {
last_execution = end;

let query = format!(
"sum by (consumer, exported_instance) (avg_over_time(node_proxy_total_connections[{interval}s] @ {})) > 0",
"sum by (consumer, exported_instance, tier) (avg_over_time(node_proxy_total_connections[{interval}s] @ {})) > 0",
end.timestamp_millis() / 1000
);

Expand All @@ -197,6 +215,7 @@ pub fn run_metrics_collector(state: Arc<State>) {
}
let project_captures = project_captures.unwrap();
let project = project_captures.get(1).unwrap().as_str();
let resource_name = project_captures.get(2).unwrap().as_str();

let instance = result.metric.exported_instance.unwrap();
let network_captures = network_regex.captures(&instance);
Expand Down Expand Up @@ -224,6 +243,12 @@ pub fn run_metrics_collector(state: Arc<State>) {
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);
}
}
}
});
Expand Down Expand Up @@ -256,6 +281,7 @@ async fn collect_prometheus_metrics(
struct PrometheusDataResultMetric {
consumer: Option<String>,
exported_instance: Option<String>,
tier: Option<String>,
}

#[derive(Debug, Deserialize)]
Expand Down
22 changes: 7 additions & 15 deletions proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ impl Metrics {
pub fn new() -> Self {
let total_connections = register_int_gauge_vec!(
opts!("node_proxy_total_connections", "Total connections",),
&["consumer", "namespace", "instance"]
&["consumer", "namespace", "instance", "tier"]
)
.unwrap();

let total_packages_bytes = register_int_counter_vec!(
opts!("node_proxy_total_packages_bytes", "Total bytes transferred",),
&["consumer", "namespace", "instance"]
&["consumer", "namespace", "instance", "tier"]
)
.unwrap();

Expand All @@ -222,7 +222,7 @@ impl Metrics {
"node_proxy_total_connections_denied",
"Total denied connections",
),
&["consumer", "namespace", "instance"]
&["consumer", "namespace", "instance", "tier"]
)
.unwrap();

Expand All @@ -240,26 +240,20 @@ impl Metrics {
instance: &str,
value: usize,
) {
let consumer = &consumer.to_string();

self.total_packages_bytes
.with_label_values(&[consumer, namespace, instance])
.with_label_values(&[&consumer.to_string(), namespace, instance, &consumer.tier])
.inc_by(value as u64)
}

pub fn inc_total_connections(&self, consumer: &Consumer, namespace: &str, instance: &str) {
let consumer = &consumer.to_string();

self.total_connections
.with_label_values(&[consumer, namespace, instance])
.with_label_values(&[&consumer.to_string(), namespace, instance, &consumer.tier])
.inc()
}

pub fn dec_total_connections(&self, consumer: &Consumer, namespace: &str, instance: &str) {
let consumer = &consumer.to_string();

self.total_connections
.with_label_values(&[consumer, namespace, instance])
.with_label_values(&[&consumer.to_string(), namespace, instance, &consumer.tier])
.dec()
}

Expand All @@ -269,10 +263,8 @@ impl Metrics {
namespace: &str,
instance: &str,
) {
let consumer = &consumer.to_string();

self.total_connections_denied
.with_label_values(&[consumer, namespace, instance])
.with_label_values(&[&consumer.to_string(), namespace, instance, &consumer.tier])
.inc()
}
}
Expand Down

0 comments on commit 35a9bf5

Please sign in to comment.