From 02b805976a29afb115885904d8324a90edf459f7 Mon Sep 17 00:00:00 2001 From: ilya Date: Thu, 9 Jan 2025 17:18:10 +0000 Subject: [PATCH] Bad token detector metrics (#3228) # Description Adds Prometheus metrics for driver's bad token detector strategies, so it will be possible to show how many tokens were marked as `bad` by each solver and strategy. It would be helpful to identify solvers that perform as not expected and to analyze the work of each detection strategy individually. ## How to test Create 2 grafana panels: the one that shows detected tokens by solver and another - by strategy. Added `hotfix` label to deploy it asap. (cherry picked from commit b46fc728dde8fc648108f117c3e13c84481f4dd5) --- .../domain/competition/bad_tokens/metrics.rs | 20 +++++++++++++++++-- .../competition/bad_tokens/simulation.rs | 4 +++- crates/driver/src/infra/api/mod.rs | 1 + crates/driver/src/infra/observe/metrics.rs | 3 +++ crates/driver/src/infra/observe/mod.rs | 2 +- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/driver/src/domain/competition/bad_tokens/metrics.rs b/crates/driver/src/domain/competition/bad_tokens/metrics.rs index c6951fbea6..daaad1ee7c 100644 --- a/crates/driver/src/domain/competition/bad_tokens/metrics.rs +++ b/crates/driver/src/domain/competition/bad_tokens/metrics.rs @@ -1,6 +1,9 @@ use { super::Quality, - crate::domain::eth, + crate::{ + domain::eth, + infra::{observe::metrics, solver}, + }, dashmap::DashMap, std::{ sync::Arc, @@ -27,6 +30,7 @@ pub struct Detector { counter: Arc>, log_only: bool, token_freeze_time: Duration, + solver: solver::Name, } impl Detector { @@ -35,6 +39,7 @@ impl Detector { required_measurements: u32, log_only: bool, token_freeze_time: Duration, + solver: solver::Name, ) -> Self { Self { failure_ratio, @@ -42,6 +47,7 @@ impl Detector { counter: Default::default(), log_only, token_freeze_time, + solver, } } @@ -120,6 +126,10 @@ impl Detector { tokens = ?new_unsupported_tokens, "mark tokens as unsupported" ); + metrics::get() + .bad_tokens_detected + .with_label_values(&[&self.solver.0, "metrics"]) + .inc_by(new_unsupported_tokens.len() as u64); } } } @@ -133,7 +143,13 @@ mod tests { #[tokio::test] async fn unfreeze_bad_tokens() { const FREEZE_DURATION: Duration = Duration::from_millis(50); - let detector = Detector::new(0.5, 2, false, FREEZE_DURATION); + let detector = Detector::new( + 0.5, + 2, + false, + FREEZE_DURATION, + solver::Name("mysolver".to_string()), + ); let token_a = eth::TokenAddress(eth::ContractAddress(H160([1; 20]))); let token_b = eth::TokenAddress(eth::ContractAddress(H160([2; 20]))); diff --git a/crates/driver/src/domain/competition/bad_tokens/simulation.rs b/crates/driver/src/domain/competition/bad_tokens/simulation.rs index dd8eb5e0f4..9b593b8726 100644 --- a/crates/driver/src/domain/competition/bad_tokens/simulation.rs +++ b/crates/driver/src/domain/competition/bad_tokens/simulation.rs @@ -8,7 +8,7 @@ use { }, eth, }, - infra, + infra::{self, observe::metrics}, }, futures::FutureExt, model::interaction::InteractionData, @@ -100,6 +100,8 @@ impl Detector { } Ok(TokenQuality::Bad { reason }) => { tracing::debug!(reason, token=?sell_token.0, "cache token as unsupported"); + // All solvers share the same cache for the simulation detector, so there is no need to specify the solver name here. + metrics::get().bad_tokens_detected.with_label_values(&["any", "simulation"]).inc(); inner .cache .update_quality(sell_token, false, now); diff --git a/crates/driver/src/infra/api/mod.rs b/crates/driver/src/infra/api/mod.rs index de7897cfa8..ccbdc5a178 100644 --- a/crates/driver/src/infra/api/mod.rs +++ b/crates/driver/src/infra/api/mod.rs @@ -84,6 +84,7 @@ impl Api { bad_token_config.metrics_strategy_required_measurements, bad_token_config.metrics_strategy_log_only, bad_token_config.metrics_strategy_token_freeze_time, + name.clone(), )); } diff --git a/crates/driver/src/infra/observe/metrics.rs b/crates/driver/src/infra/observe/metrics.rs index 330e250867..237206c668 100644 --- a/crates/driver/src/infra/observe/metrics.rs +++ b/crates/driver/src/infra/observe/metrics.rs @@ -19,6 +19,9 @@ pub struct Metrics { /// The results of the mempool submission. #[metric(labels("mempool", "result"))] pub mempool_submission: prometheus::IntCounterVec, + /// How many tokens detected by specific solver and strategy. + #[metric(labels("solver", "strategy"))] + pub bad_tokens_detected: prometheus::IntCounterVec, } /// Setup the metrics registry. diff --git a/crates/driver/src/infra/observe/mod.rs b/crates/driver/src/infra/observe/mod.rs index f8c1394e54..c3c0e34a7b 100644 --- a/crates/driver/src/infra/observe/mod.rs +++ b/crates/driver/src/infra/observe/mod.rs @@ -28,7 +28,7 @@ use { url::Url, }; -mod metrics; +pub mod metrics; /// Setup the observability. The log argument configures the tokio tracing /// framework.