diff --git a/crates/transaction-pool/src/metrics.rs b/crates/transaction-pool/src/metrics.rs index 9076829427..90d46854d4 100644 --- a/crates/transaction-pool/src/metrics.rs +++ b/crates/transaction-pool/src/metrics.rs @@ -93,3 +93,15 @@ impl MaintainPoolMetrics { self.drift_count.increment(1); } } + +/// All Transactions metrics +#[derive(Metrics)] +#[metrics(scope = "transaction_pool")] +pub struct AllTransactionsMetrics { + /// Number of all transactions by hash in the pool + pub(crate) all_transactions_by_hash: Gauge, + /// Number of all transactions by id in the pool + pub(crate) all_transactions_by_id: Gauge, + /// Number of all transactions by all senders in the pool + pub(crate) all_transactions_by_all_senders: Gauge, +} diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index 91e1f1022a..d31ae8d770 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -4,7 +4,7 @@ use crate::{ config::{LocalTransactionConfig, TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER}, error::{Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind}, identifier::{SenderId, TransactionId}, - metrics::TxPoolMetrics, + metrics::{AllTransactionsMetrics, TxPoolMetrics}, pool::{ best::BestTransactions, blob::BlobTransactions, @@ -950,6 +950,8 @@ pub(crate) struct AllTransactions { price_bumps: PriceBumpConfig, /// How to handle [TransactionOrigin::Local](crate::TransactionOrigin) transactions. local_transactions_config: LocalTransactionConfig, + /// All Transactions metrics + metrics: AllTransactionsMetrics, } impl AllTransactions { @@ -990,6 +992,7 @@ impl AllTransactions { pub(crate) fn tx_inc(&mut self, sender: SenderId) { let count = self.tx_counter.entry(sender).or_default(); *count += 1; + self.metrics.all_transactions_by_all_senders.increment(1.0); } /// Decrements the transaction counter for the sender @@ -998,9 +1001,11 @@ impl AllTransactions { let count = entry.get_mut(); if *count == 1 { entry.remove(); + self.metrics.all_transactions_by_all_senders.decrement(1.0); return } *count -= 1; + self.metrics.all_transactions_by_all_senders.decrement(1.0); } } @@ -1020,6 +1025,12 @@ impl AllTransactions { } } + /// Updates the size metrics + pub(crate) fn update_size_metrics(&mut self) { + self.metrics.all_transactions_by_hash.set(self.by_hash.len() as f64); + self.metrics.all_transactions_by_id.set(self.txs.len() as f64); + } + /// Rechecks all transactions in the pool against the changes. /// /// Possible changes are: @@ -1268,6 +1279,7 @@ impl AllTransactions { let internal = self.txs.remove(&tx.transaction_id)?; // decrement the counter for the sender. self.tx_decr(tx.sender_id()); + self.update_size_metrics(); Some((tx, internal.subpool)) } @@ -1285,7 +1297,12 @@ impl AllTransactions { // decrement the counter for the sender. self.tx_decr(internal.transaction.sender_id()); - self.by_hash.remove(internal.transaction.hash()).map(|tx| (tx, internal.subpool)) + let result = + self.by_hash.remove(internal.transaction.hash()).map(|tx| (tx, internal.subpool)); + + self.update_size_metrics(); + + result } /// Checks if the given transaction's type conflicts with an existing transaction. @@ -1661,6 +1678,8 @@ impl AllTransactions { self.tx_inc(inserted_tx_id.sender); } + self.update_size_metrics(); + Ok(InsertOk { transaction, move_to: state.into(), state, replaced_tx, updates }) } @@ -1705,6 +1724,7 @@ impl Default for AllTransactions { pending_fees: Default::default(), price_bumps: Default::default(), local_transactions_config: Default::default(), + metrics: Default::default(), } } }