Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions core/node/api_server/src/web3/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,29 @@ pub(super) struct TxReceiptMetrics {
#[vise::register]
pub(super) static TX_RECEIPT_METRICS: vise::Global<TxReceiptMetrics> = vise::Global::new();

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelValue, EncodeLabelSet)]
#[metrics(label = "stage", rename_all = "snake_case")]
pub(super) enum SendRawTxSyncStage {
Submit,
PollReceipt,
Timeout,
}

#[derive(Debug, Metrics)]
#[metrics(prefix = "api_send_raw_tx_sync")]
pub(super) struct SendRawTxSyncMetrics {
/// Latency of each stage of `eth_sendRawTransactionSync`.
#[metrics(buckets = Buckets::LATENCIES)]
pub latency: Family<SendRawTxSyncStage, Histogram<Duration>>,
/// Number of poll iterations until completion or timeout.
#[metrics(buckets = Buckets::exponential(1.0..=256.0, 2.0))]
pub polls: Family<SendRawTxSyncStage, Histogram<u64>>,
}

#[vise::register]
pub(super) static SEND_RAW_TX_SYNC_METRICS: vise::Global<SendRawTxSyncMetrics> =
vise::Global::new();

#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
Expand Down
30 changes: 27 additions & 3 deletions core/node/api_server/src/web3/namespaces/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ use crate::{
tx_sender::BinarySearchKind,
utils::open_readonly_transaction,
web3::{
backend_jsonrpsee::MethodTracer, namespaces::validate_gas_cap,
receipts::fill_transaction_receipts, state::RpcState, TypedFilter,
backend_jsonrpsee::MethodTracer,
metrics::{SendRawTxSyncStage, SEND_RAW_TX_SYNC_METRICS},
namespaces::validate_gas_cap,
receipts::fill_transaction_receipts,
state::RpcState,
TypedFilter,
},
};

Expand Down Expand Up @@ -713,7 +717,17 @@ impl EthNamespace {
};

// Submit transaction and get hash
let hash = self.send_raw_transaction_impl(tx_bytes).await?;
let submit_latency = SEND_RAW_TX_SYNC_METRICS.latency[&SendRawTxSyncStage::Submit].start();
let hash = match self.send_raw_transaction_impl(tx_bytes).await {
Ok(hash) => {
submit_latency.observe();
hash
}
Err(err) => {
submit_latency.observe();
return Err(err);
}
};

// Poll for receipt at regular intervals
let poll_interval = std::time::Duration::from_millis(
Expand All @@ -724,15 +738,25 @@ impl EthNamespace {

let deadline = tokio::time::sleep(std::time::Duration::from_millis(timeout_ms));
tokio::pin!(deadline);
let poll_latency =
SEND_RAW_TX_SYNC_METRICS.latency[&SendRawTxSyncStage::PollReceipt].start();
let timeout_latency =
SEND_RAW_TX_SYNC_METRICS.latency[&SendRawTxSyncStage::Timeout].start();
let mut polls = 0u64;

// Check immediately, then poll at intervals
loop {
tokio::select! {
_ = &mut deadline => {
timeout_latency.observe();
SEND_RAW_TX_SYNC_METRICS.polls[&SendRawTxSyncStage::Timeout].observe(polls);
return Err(Web3Error::TransactionTimeout(hash));
}
_ = interval.tick() => {
polls += 1;
if let Some(receipt) = self.get_transaction_receipt_impl(hash).await? {
poll_latency.observe();
SEND_RAW_TX_SYNC_METRICS.polls[&SendRawTxSyncStage::PollReceipt].observe(polls);
return Ok(receipt);
}
// Continue waiting
Expand Down
Loading