From 8bc962e1ee351669e2b0237d1a2add43a67cd98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 23 Oct 2025 18:08:11 -0300 Subject: [PATCH 1/3] refactor: remove Mutex from `FunctionProfilingLayer` --- cmd/ethrex/initializers.rs | 2 +- crates/blockchain/metrics/profiling.rs | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/cmd/ethrex/initializers.rs b/cmd/ethrex/initializers.rs index 839e7fc7508..5d98cd7e0b9 100644 --- a/cmd/ethrex/initializers.rs +++ b/cmd/ethrex/initializers.rs @@ -65,7 +65,7 @@ pub fn init_tracing(opts: &Options) -> reload::Handle { let fmt_layer = layer.with_filter(filter); let subscriber: Box = if opts.metrics_enabled { - let profiling_layer = FunctionProfilingLayer::default(); + let profiling_layer = FunctionProfilingLayer; Box::new(Registry::default().with(fmt_layer).with(profiling_layer)) } else { Box::new(Registry::default().with(fmt_layer)) diff --git a/crates/blockchain/metrics/profiling.rs b/crates/blockchain/metrics/profiling.rs index 01af686fd91..1a4bd3b03b4 100644 --- a/crates/blockchain/metrics/profiling.rs +++ b/crates/blockchain/metrics/profiling.rs @@ -1,8 +1,5 @@ use prometheus::{Encoder, HistogramTimer, HistogramVec, TextEncoder, register_histogram_vec}; -use std::{ - collections::HashMap, - sync::{LazyLock, Mutex}, -}; +use std::sync::LazyLock; use tracing::{Subscriber, span::Id}; use tracing_subscriber::{Layer, layer::Context, registry::LookupSpan}; @@ -23,9 +20,9 @@ fn initialize_histogram_vec() -> HistogramVec { // We use this struct to simplify accumulating the time spent doing each task and publishing the metric only when the sync cycle is finished // We need to do this because things like database reads and writes are spread out throughout the code, so we need to gather multiple measurements to publish #[derive(Default)] -pub struct FunctionProfilingLayer { - function_timers: Mutex>, -} +pub struct FunctionProfilingLayer; + +struct ProfileTimer(HistogramTimer); impl Layer for FunctionProfilingLayer where @@ -40,14 +37,15 @@ where let timer = METRICS_BLOCK_PROCESSING_PROFILE .with_label_values(&[name]) .start_timer(); - let mut timers = self.function_timers.lock().unwrap(); - timers.insert(id.clone(), timer); + span.extensions_mut().insert(ProfileTimer(timer)); } } - fn on_exit(&self, id: &Id, _ctx: Context<'_, S>) { - let mut timers = self.function_timers.lock().unwrap(); - if let Some(timer) = timers.remove(id) { + fn on_exit(&self, id: &Id, ctx: Context<'_, S>) { + let timer = ctx + .span(id) + .and_then(|span| span.extensions_mut().remove::()); + if let Some(ProfileTimer(timer)) = timer { timer.observe_duration(); } } From 76da36da6079e56d04d23d577e80c842ff283bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 23 Oct 2025 18:09:18 -0300 Subject: [PATCH 2/3] docs: add comment --- crates/blockchain/metrics/profiling.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/blockchain/metrics/profiling.rs b/crates/blockchain/metrics/profiling.rs index 1a4bd3b03b4..f1a181234f0 100644 --- a/crates/blockchain/metrics/profiling.rs +++ b/crates/blockchain/metrics/profiling.rs @@ -37,6 +37,7 @@ where let timer = METRICS_BLOCK_PROCESSING_PROFILE .with_label_values(&[name]) .start_timer(); + // PERF: `extensions_mut` uses a Mutex internally (per span) span.extensions_mut().insert(ProfileTimer(timer)); } } @@ -44,6 +45,7 @@ where fn on_exit(&self, id: &Id, ctx: Context<'_, S>) { let timer = ctx .span(id) + // PERF: `extensions_mut` uses a Mutex internally (per span) .and_then(|span| span.extensions_mut().remove::()); if let Some(ProfileTimer(timer)) = timer { timer.observe_duration(); From d632d6e028338211877629c993919e568bae0208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 23 Oct 2025 18:14:10 -0300 Subject: [PATCH 3/3] docs: add documentation on newtype --- crates/blockchain/metrics/profiling.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/blockchain/metrics/profiling.rs b/crates/blockchain/metrics/profiling.rs index f1a181234f0..cd90977d365 100644 --- a/crates/blockchain/metrics/profiling.rs +++ b/crates/blockchain/metrics/profiling.rs @@ -22,6 +22,7 @@ fn initialize_histogram_vec() -> HistogramVec { #[derive(Default)] pub struct FunctionProfilingLayer; +/// Wrapper around [`HistogramTimer`] to avoid conflicts with other layers struct ProfileTimer(HistogramTimer); impl Layer for FunctionProfilingLayer