Skip to content

Commit

Permalink
refactor: simplify KeeperMetrics::new to use Vec<(String, Address)>
Browse files Browse the repository at this point in the history
Co-Authored-By: Jayant Krishnamurthy <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and Jayant Krishnamurthy committed Jan 31, 2025
1 parent 821f6f1 commit 5b7b097
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 72 deletions.
8 changes: 5 additions & 3 deletions apps/fortuna/src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ pub async fn run_keeper(
) -> Result<()> {
let mut handles = Vec::new();
let keeper_metrics = Arc::new({
let chain_ids: Vec<String> = chains.keys().map(|id| id.clone()).collect();
let provider_addresses: Vec<Address> = chains.values().map(|state| state.provider_address).collect();
KeeperMetrics::new(metrics_registry.clone(), chain_ids, provider_addresses).await
let chain_labels: Vec<(String, Address)> = chains
.iter()
.map(|(id, state)| (id.clone(), state.provider_address))
.collect();
KeeperMetrics::new(metrics_registry.clone(), chain_labels).await
});
for (chain_id, chain_config) in chains {
let chain_eth_config = config
Expand Down
124 changes: 55 additions & 69 deletions apps/fortuna/src/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,78 +130,10 @@ impl Default for KeeperMetrics {
impl KeeperMetrics {
pub async fn new(
registry: Arc<RwLock<Registry>>,
chain_ids: Vec<String>,
provider_addresses: Vec<Address>,
chain_labels: Vec<(String, Address)>,
) -> Self {
let mut writable_registry = registry.write().await;
let keeper_metrics = KeeperMetrics::default();

// Initialize metrics for each chain_id and provider_address pair
for (chain_id, provider_address) in chain_ids.into_iter().zip(provider_addresses) {
let account_label = AccountLabel {
chain_id,
address: provider_address.to_string(),
};

// Pre-initialize metrics with the account label
let _ = keeper_metrics
.current_sequence_number
.get_or_create(&account_label);
let _ = keeper_metrics
.end_sequence_number
.get_or_create(&account_label);
let _ = keeper_metrics
.balance
.get_or_create(&account_label);
let _ = keeper_metrics
.collected_fee
.get_or_create(&account_label);
let _ = keeper_metrics
.current_fee
.get_or_create(&account_label);
let _ = keeper_metrics
.target_provider_fee
.get_or_create(&account_label);
let _ = keeper_metrics
.total_gas_spent
.get_or_create(&account_label);
let _ = keeper_metrics
.total_gas_fee_spent
.get_or_create(&account_label);
let _ = keeper_metrics
.requests
.get_or_create(&account_label);
let _ = keeper_metrics
.requests_processed
.get_or_create(&account_label);
let _ = keeper_metrics
.requests_processed_success
.get_or_create(&account_label);
let _ = keeper_metrics
.requests_processed_failure
.get_or_create(&account_label);
let _ = keeper_metrics
.requests_reprocessed
.get_or_create(&account_label);
let _ = keeper_metrics
.reveals
.get_or_create(&account_label);
let _ = keeper_metrics
.request_duration_ms
.get_or_create(&account_label);
let _ = keeper_metrics
.retry_count
.get_or_create(&account_label);
let _ = keeper_metrics
.final_gas_multiplier
.get_or_create(&account_label);
let _ = keeper_metrics
.final_fee_multiplier
.get_or_create(&account_label);
let _ = keeper_metrics
.gas_price_estimate
.get_or_create(&account_label);
}

writable_registry.register(
"current_sequence_number",
Expand Down Expand Up @@ -317,6 +249,60 @@ impl KeeperMetrics {
keeper_metrics.gas_price_estimate.clone(),
);

// *Important*: When adding a new metric:
// 1. Register it above using `writable_registry.register(...)`
// 2. Add a get_or_create call in the loop below to initialize it for each chain/provider pair
for (chain_id, provider_address) in chain_labels {
let account_label = AccountLabel {
chain_id,
address: provider_address.to_string(),
};

let _ = keeper_metrics
.current_sequence_number
.get_or_create(&account_label);
let _ = keeper_metrics
.end_sequence_number
.get_or_create(&account_label);
let _ = keeper_metrics.balance.get_or_create(&account_label);
let _ = keeper_metrics.collected_fee.get_or_create(&account_label);
let _ = keeper_metrics.current_fee.get_or_create(&account_label);
let _ = keeper_metrics
.target_provider_fee
.get_or_create(&account_label);
let _ = keeper_metrics.total_gas_spent.get_or_create(&account_label);
let _ = keeper_metrics
.total_gas_fee_spent
.get_or_create(&account_label);
let _ = keeper_metrics.requests.get_or_create(&account_label);
let _ = keeper_metrics
.requests_processed
.get_or_create(&account_label);
let _ = keeper_metrics
.requests_processed_success
.get_or_create(&account_label);
let _ = keeper_metrics
.requests_processed_failure
.get_or_create(&account_label);
let _ = keeper_metrics
.requests_reprocessed
.get_or_create(&account_label);
let _ = keeper_metrics.reveals.get_or_create(&account_label);
let _ = keeper_metrics
.request_duration_ms
.get_or_create(&account_label);
let _ = keeper_metrics.retry_count.get_or_create(&account_label);
let _ = keeper_metrics
.final_gas_multiplier
.get_or_create(&account_label);
let _ = keeper_metrics
.final_fee_multiplier
.get_or_create(&account_label);
let _ = keeper_metrics
.gas_price_estimate
.get_or_create(&account_label);
}

keeper_metrics
}
}
Expand Down

0 comments on commit 5b7b097

Please sign in to comment.