Skip to content

Commit

Permalink
Bad token detector metrics (#3228)
Browse files Browse the repository at this point in the history
# 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 b46fc72)
  • Loading branch information
squadgazzz committed Jan 9, 2025
1 parent 1c3f205 commit 02b8059
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
20 changes: 18 additions & 2 deletions crates/driver/src/domain/competition/bad_tokens/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use {
super::Quality,
crate::domain::eth,
crate::{
domain::eth,
infra::{observe::metrics, solver},
},
dashmap::DashMap,
std::{
sync::Arc,
Expand All @@ -27,6 +30,7 @@ pub struct Detector {
counter: Arc<DashMap<eth::TokenAddress, TokenStatistics>>,
log_only: bool,
token_freeze_time: Duration,
solver: solver::Name,
}

impl Detector {
Expand All @@ -35,13 +39,15 @@ impl Detector {
required_measurements: u32,
log_only: bool,
token_freeze_time: Duration,
solver: solver::Name,
) -> Self {
Self {
failure_ratio,
required_measurements,
counter: Default::default(),
log_only,
token_freeze_time,
solver,
}
}

Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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])));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
},
eth,
},
infra,
infra::{self, observe::metrics},
},
futures::FutureExt,
model::interaction::InteractionData,
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/infra/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}

Expand Down
3 changes: 3 additions & 0 deletions crates/driver/src/infra/observe/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/driver/src/infra/observe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use {
url::Url,
};

mod metrics;
pub mod metrics;

/// Setup the observability. The log argument configures the tokio tracing
/// framework.
Expand Down

0 comments on commit 02b8059

Please sign in to comment.