Skip to content

Commit

Permalink
Add method call metrics to RPC calls
Browse files Browse the repository at this point in the history
Implement an `IntCounterVec` to track the total number of method calls per chain and method. Register the new counter in the `Metrics` struct, and increment the counter in the `rpc_call` function to monitor method invocations.
  • Loading branch information
Inkvi committed Sep 24, 2024
1 parent a9adf8b commit 1fa6371
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ async fn rpc_call(
}
};

// Increment the method call counter
metrics.method_call_counter.with_label_values(&[&chain, &method]).inc();

// Check if the method starts with an allowed prefix
if !chain_state
.allowed_prefixes
Expand Down
26 changes: 25 additions & 1 deletion src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web::{web, Error, HttpResponse};
use prometheus::{Counter, Encoder, Registry, TextEncoder};
use prometheus::{Counter, Encoder, Registry, TextEncoder, IntCounterVec};

pub struct Metrics {
pub registry: prometheus::Registry,
Expand All @@ -8,6 +8,7 @@ pub struct Metrics {
pub cache_expired_miss_counter: Counter,
pub cache_uncacheable_counter: Counter,
pub error_counter: Counter,
pub method_call_counter: IntCounterVec,
}

// Function to add a prefix to the metric names
Expand All @@ -29,6 +30,21 @@ fn register_counter_with_prefix(
counter
}

// Create a function to register IntCounterVec with a prefix
fn register_int_counter_vec_with_prefix(
registry: &Registry,
prefix: &str,
name: &str,
description: &str,
labels: &[&str],
) -> IntCounterVec {
let name = add_prefix(prefix, name);
let opts = prometheus::Opts::new(name, description);
let counter_vec = IntCounterVec::new(opts, labels).unwrap();
registry.register(Box::new(counter_vec.clone())).unwrap();
counter_vec
}

impl Metrics {
pub fn new(prefix: &str) -> Self {
let registry = Registry::new();
Expand Down Expand Up @@ -63,6 +79,13 @@ impl Metrics {
"error_total",
"Total number of errors.",
);
let method_call_counter = register_int_counter_vec_with_prefix(
&registry,
prefix,
"method_call_total",
"Total number of method calls per chain",
&["chain", "method"],
);

Self {
registry,
Expand All @@ -71,6 +94,7 @@ impl Metrics {
cache_expired_miss_counter,
cache_uncacheable_counter,
error_counter,
method_call_counter,
}
}
}
Expand Down

0 comments on commit 1fa6371

Please sign in to comment.