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

Add metrics for AutoUpdatingSolverTokenOwnerFinder #842

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion crates/shared/src/bad_token/token_owner_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ pub async fn init(
.into_iter()
.zip(args.solver_token_owners_cache_update_intervals.clone())
{
let identifier = url.to_string();
let solver = Box::new(SolverConfiguration {
url,
client: http_factory.create(),
});
let solver = AutoUpdatingSolverTokenOwnerFinder::new(solver, update_interval);
let solver =
AutoUpdatingSolverTokenOwnerFinder::new(solver, update_interval, identifier);
proposers.push(Arc::new(solver));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use super::TokenOwnerSolverApi;
use crate::bad_token::token_owner_finder::TokenOwnerProposing;
use anyhow::Result;
use ethcontract::H160;
use prometheus::{
core::{AtomicU64, GenericCounter},
IntCounterVec,
};
use std::{
collections::HashMap,
fmt::{self, Debug, Formatter},
Expand All @@ -17,25 +21,54 @@ pub struct AutoUpdatingSolverTokenOwnerFinder {
inner: Arc<Inner>,
}

#[derive(prometheus_metric_storage::MetricStorage, Clone, Debug)]
struct Metrics {
/// Tracks how often a token owner update succeeded or failed.
#[metric(labels("identifier", "result"))]
updates: IntCounterVec,
}

struct Inner {
solver: Box<dyn TokenOwnerSolverApi>,
cache: RwLock<HashMap<Token, Vec<Owner>>>,
metrics: &'static Metrics,
identifier: String,
}

impl Inner {
pub fn get_update_counter(&self, success: bool) -> GenericCounter<AtomicU64> {
let result = if success { "success" } else { "failure" };
let labels = [&self.identifier, result];
self.metrics.updates.with_label_values(&labels)
}
}

impl AutoUpdatingSolverTokenOwnerFinder {
pub fn new(solver: Box<dyn TokenOwnerSolverApi>, update_interval: Duration) -> Self {
pub fn new(
solver: Box<dyn TokenOwnerSolverApi>,
update_interval: Duration,
identifier: String,
) -> Self {
let inner = Arc::new(Inner {
solver,
cache: RwLock::new(Default::default()),
metrics: Metrics::instance(global_metrics::get_metric_storage_registry()).unwrap(),
identifier,
});

// reset metrics for consistent graphs in grafana
inner.get_update_counter(true).reset();
inner.get_update_counter(false).reset();

// spawn a background task to regularly update cache
{
let inner = inner.clone();
let updater = async move {
loop {
if let Err(err) = inner.update().await {
tracing::error!(?err, "failed to update token list");
let result = inner.update().await;
inner.get_update_counter(result.is_ok()).inc();
if let Err(err) = result {
tracing::warn!(?err, "failed to update token list");
}
tokio::time::sleep(update_interval).await;
}
Expand Down Expand Up @@ -99,8 +132,11 @@ mod tests {
url: Url::from_str(&url).unwrap(),
client: Client::new(),
});
let finder =
AutoUpdatingSolverTokenOwnerFinder::new(configuration, Duration::from_secs(1000));
let finder = AutoUpdatingSolverTokenOwnerFinder::new(
configuration,
Duration::from_secs(1000),
"test".to_owned(),
);
tokio::time::sleep(Duration::from_secs(10)).await;
let owners = finder
.find_candidate_owners(addr!("132d8D2C76Db3812403431fAcB00F3453Fc42125"))
Expand Down
1 change: 1 addition & 0 deletions crates/shared/src/bad_token/trace_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ mod tests {
client: reqwest::Client::new(),
}),
Duration::MAX,
"test".to_owned(),
));

// Force the cache to update at least once.
Expand Down