diff --git a/src/main.rs b/src/main.rs index a79af80..0ae178b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 diff --git a/src/metrics.rs b/src/metrics.rs index ae32008..94b563b 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -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, @@ -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 @@ -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(); @@ -63,6 +79,13 @@ impl Metrics { "error_total", "Total number of errors.", ); + let method_call_counter = register_int_counter_vec_with_prefix( + ®istry, + prefix, + "method_call_total", + "Total number of method calls per chain", + &["chain", "method"], + ); Self { registry, @@ -71,6 +94,7 @@ impl Metrics { cache_expired_miss_counter, cache_uncacheable_counter, error_counter, + method_call_counter, } } }