Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compute rpc active connections metrics from jsonrpsee logs #1590

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/stratus.env.local
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/infra/metrics/metrics_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
30 changes: 27 additions & 3 deletions src/infra/tracing/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -310,27 +311,50 @@ 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,
};

writeln!(writer, "{}", to_json_string(&log))
}
}

#[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::<u64>() else { return };
metrics::set_rpc_requests_active(current);
}

#[derive(derive_new::new)]
struct TracingLog<'a> {
timestamp: DateTime<Utc>,
level: SerializeLevel<'a>,
target: &'a str,
thread: Thread,
fields: SerializeFieldMap<'a, Event<'a>>,
// fields: SerializeFieldMap<'a, Event<'a>>,
fields: JsonValue,
context: Option<TracingLogContextField<'a>>,
}

Expand Down