From 3185d56161a288bfd83842cfed5d9ea90644ba4a Mon Sep 17 00:00:00 2001 From: Renato Dinhani Date: Thu, 1 Aug 2024 17:39:35 -0300 Subject: [PATCH] feat: compute rpc active connections metrics from jsonrpsee logs --- config/stratus.env.local | 2 +- src/infra/metrics/metrics_definitions.rs | 2 +- src/infra/tracing/tracing.rs | 30 +++++++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/config/stratus.env.local b/config/stratus.env.local index bd31cdb43..d222b49e5 100644 --- a/config/stratus.env.local +++ b/config/stratus.env.local @@ -1,4 +1,4 @@ -RUST_LOG=info,stratus::eth::rpc::rpc_subscriptions::rx=off,stratus::eth::consensus::rx=off,stratus::eth::consensus=off +RUST_LOG=info,stratus::eth::rpc::rpc_subscriptions::rx=off,stratus::eth::consensus::rx=off,stratus::eth::consensus=off,jsonrpsee-server=debug CHAIN_ID=2008 EVMS=1 diff --git a/src/infra/metrics/metrics_definitions.rs b/src/infra/metrics/metrics_definitions.rs index fbf2bdb7f..c5c2ed4a1 100644 --- a/src/infra/metrics/metrics_definitions.rs +++ b/src/infra/metrics/metrics_definitions.rs @@ -5,7 +5,7 @@ metrics! { group: json_rpc, "Number of JSON-RPC requests active right now." - gauge rpc_requests_active{client, method}, + gauge rpc_requests_active{}, "Number of JSON-RPC requests that started." counter rpc_requests_started{client, method, contract, function}, diff --git a/src/infra/tracing/tracing.rs b/src/infra/tracing/tracing.rs index ceea495d2..ba668583c 100644 --- a/src/infra/tracing/tracing.rs +++ b/src/infra/tracing/tracing.rs @@ -29,7 +29,6 @@ use tonic::metadata::MetadataKey; use tonic::metadata::MetadataMap; use tracing::span; use tracing::span::Attributes; -use tracing::Event; use tracing::Span; use tracing::Subscriber; use tracing_serde::fields::AsMap; @@ -52,6 +51,8 @@ use crate::ext::spawn_named; use crate::ext::to_json_string; use crate::ext::to_json_value; use crate::infra::build_info; +#[cfg(feature = "metrics")] +use crate::infra::metrics; use crate::infra::tracing::TracingConfig; use crate::infra::tracing::TracingLogFormat; use crate::infra::tracing::TracingProtocol; @@ -310,13 +311,20 @@ where } }); + // TODO: temporary metrics from events + let fields = to_json_value(event.field_map()); + #[cfg(feature = "metrics")] + { + event_to_metrics(&fields); + } + // parse metadata and event let log = TracingLog { timestamp: Utc::now(), level: meta.level().as_serde(), target: meta.target(), thread: std::thread::current(), - fields: event.field_map(), + fields, context, }; @@ -324,13 +332,29 @@ where } } +#[cfg(feature = "metrics")] +fn event_to_metrics(json: &JsonValue) { + let Some(message) = json.as_object().and_then(|obj| obj.get("message")).and_then(|msg| msg.as_str()) else { + return; + }; + + // jsonrpsee active connections + let Some(message) = message.strip_prefix("Accepting new connection ") else { + return; + }; + let Some((current, _)) = message.split_once('/') else { return }; + let Ok(current) = current.parse::() else { return }; + metrics::set_rpc_requests_active(current); +} + #[derive(derive_new::new)] struct TracingLog<'a> { timestamp: DateTime, level: SerializeLevel<'a>, target: &'a str, thread: Thread, - fields: SerializeFieldMap<'a, Event<'a>>, + // fields: SerializeFieldMap<'a, Event<'a>>, + fields: JsonValue, context: Option>, }