Skip to content

Commit ee2db4f

Browse files
authored
feat: add batcher accumulated cost metric (#1663)
1 parent f73d455 commit ee2db4f

File tree

3 files changed

+442
-67
lines changed

3 files changed

+442
-67
lines changed

batcher/aligned-batcher/src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,10 @@ impl Batcher {
15571557
{
15581558
warn!("Failed to send task status to telemetry: {:?}", e);
15591559
}
1560+
let gas_cost = Self::gas_cost_in_eth(receipt.effective_gas_price, receipt.gas_used);
1561+
self.metrics
1562+
.batcher_gas_cost_create_task_total
1563+
.inc_by(gas_cost);
15601564
Ok(receipt)
15611565
}
15621566
Err(RetryError::Permanent(BatcherError::ReceiptNotFoundError)) => {
@@ -1646,14 +1650,40 @@ impl Batcher {
16461650
)
16471651
.await
16481652
{
1649-
Ok(_) => info!("createNewTask transaction successfully canceled"),
1653+
Ok(receipt) => {
1654+
info!("createNewTask transaction successfully canceled");
1655+
let gas_cost = Self::gas_cost_in_eth(receipt.effective_gas_price, receipt.gas_used);
1656+
self.metrics
1657+
.batcher_gas_cost_cancel_task_total
1658+
.inc_by(gas_cost);
1659+
}
16501660
Err(e) => error!("Could not cancel createNewTask transaction: {e}"),
16511661
};
16521662
self.metrics
16531663
.cancel_create_new_task_duration
16541664
.set(start.elapsed().as_millis() as i64);
16551665
}
16561666

1667+
fn gas_cost_in_eth(gas_price: Option<U256>, gas_used: Option<U256>) -> f64 {
1668+
if let (Some(gas_price), Some(gas_used)) = (gas_price, gas_used) {
1669+
let wei_gas_cost = gas_price
1670+
.checked_mul(gas_used)
1671+
.unwrap_or_else(U256::max_value);
1672+
1673+
// f64 is typically sufficient for transaction gas costs.
1674+
let max_f64_u256 = U256::from(f64::MAX as u64);
1675+
if wei_gas_cost > max_f64_u256 {
1676+
return f64::MAX;
1677+
}
1678+
1679+
let wei_gas_cost_f64 = wei_gas_cost.low_u128() as f64;
1680+
let eth_gas_cost = wei_gas_cost_f64 / 1e18;
1681+
1682+
return eth_gas_cost;
1683+
}
1684+
0.0
1685+
}
1686+
16571687
/// Only relevant for testing and for users to easily use Aligned
16581688
fn is_nonpaying(&self, addr: &Address) -> bool {
16591689
self.non_paying_config

batcher/aligned-batcher/src/metrics.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use std::{thread, time::Duration};
22

33
// Prometheus
44
use prometheus::{
5-
opts, register_int_counter, register_int_counter_vec, register_int_gauge, IntCounter,
6-
IntCounterVec, IntGauge,
5+
core::{AtomicF64, GenericCounter},
6+
opts, register_counter, register_int_counter, register_int_counter_vec, register_int_gauge,
7+
IntCounter, IntCounterVec, IntGauge,
78
};
89

910
use warp::{Filter, Rejection, Reply};
@@ -22,6 +23,8 @@ pub struct BatcherMetrics {
2223
pub s3_duration: IntGauge,
2324
pub create_new_task_duration: IntGauge,
2425
pub cancel_create_new_task_duration: IntGauge,
26+
pub batcher_gas_cost_create_task_total: GenericCounter<AtomicF64>,
27+
pub batcher_gas_cost_cancel_task_total: GenericCounter<AtomicF64>,
2528
}
2629

2730
impl BatcherMetrics {
@@ -59,6 +62,17 @@ impl BatcherMetrics {
5962
"Cancel create New Task Duration"
6063
))?;
6164

65+
let batcher_gas_cost_create_task_total: GenericCounter<AtomicF64> =
66+
register_counter!(opts!(
67+
"batcher_gas_cost_create_task_total",
68+
"Batcher Gas Cost Create Task Total"
69+
))?;
70+
let batcher_gas_cost_cancel_task_total: GenericCounter<AtomicF64> =
71+
register_counter!(opts!(
72+
"batcher_gas_cost_cancel_task_total",
73+
"Batcher Gas Cost Cancel Task Total"
74+
))?;
75+
6276
registry.register(Box::new(open_connections.clone()))?;
6377
registry.register(Box::new(received_proofs.clone()))?;
6478
registry.register(Box::new(sent_batches.clone()))?;
@@ -71,6 +85,8 @@ impl BatcherMetrics {
7185
registry.register(Box::new(s3_duration.clone()))?;
7286
registry.register(Box::new(create_new_task_duration.clone()))?;
7387
registry.register(Box::new(cancel_create_new_task_duration.clone()))?;
88+
registry.register(Box::new(batcher_gas_cost_create_task_total.clone()))?;
89+
registry.register(Box::new(batcher_gas_cost_cancel_task_total.clone()))?;
7490

7591
let metrics_route = warp::path!("metrics")
7692
.and(warp::any().map(move || registry.clone()))
@@ -95,6 +111,8 @@ impl BatcherMetrics {
95111
s3_duration,
96112
create_new_task_duration,
97113
cancel_create_new_task_duration,
114+
batcher_gas_cost_create_task_total,
115+
batcher_gas_cost_cancel_task_total,
98116
})
99117
}
100118

0 commit comments

Comments
 (0)