Skip to content
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use blockifier::state::contract_class_manager::ContractClassManager;
use metrics::{counter, gauge};
use metrics::gauge;
#[cfg(test)]
use mockall::automock;
use papyrus_storage::state::{StateStorageReader, StateStorageWriter};
Expand Down Expand Up @@ -49,7 +49,12 @@ use crate::block_builder::{
BlockMetadata,
};
use crate::config::BatcherConfig;
use crate::metrics::{register_metrics, ProposalMetricsHandle};
use crate::metrics::{
register_metrics,
ProposalMetricsHandle,
BATCHED_TRANSACTIONS,
REJECTED_TRANSACTIONS,
};
use crate::transaction_provider::{ProposeTransactionProvider, ValidateTransactionProvider};
use crate::utils::{
deadline_as_instant,
Expand Down Expand Up @@ -443,8 +448,8 @@ impl Batcher {
block_execution_artifacts.rejected_tx_hashes,
)
.await?;
counter!(crate::metrics::BATCHED_TRANSACTIONS.name).increment(n_txs);
counter!(crate::metrics::REJECTED_TRANSACTIONS.name).increment(n_rejected_txs);
BATCHED_TRANSACTIONS.increment(n_txs);
REJECTED_TRANSACTIONS.increment(n_rejected_txs);
Ok(DecisionReachedResponse {
state_diff,
l2_gas_used: block_execution_artifacts.l2_gas_used,
Expand Down
20 changes: 14 additions & 6 deletions crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ use crate::block_builder::{
MockBlockBuilderFactoryTrait,
};
use crate::config::BatcherConfig;
use crate::metrics::{
BATCHED_TRANSACTIONS,
PROPOSAL_ABORTED,
PROPOSAL_FAILED,
PROPOSAL_STARTED,
PROPOSAL_SUCCEEDED,
REJECTED_TRANSACTIONS,
};
use crate::test_utils::{test_txs, FakeProposeBlockBuilder, FakeValidateBlockBuilder};

const INITIAL_HEIGHT: BlockNumber = BlockNumber(3);
Expand Down Expand Up @@ -198,10 +206,10 @@ fn assert_proposal_metrics(
let n_expected_active_proposals =
expected_started - (expected_succeeded + expected_failed + expected_aborted);
assert!(n_expected_active_proposals <= 1);
let started = parse_numeric_metric::<u64>(metrics, crate::metrics::PROPOSAL_STARTED.name);
let succeeded = parse_numeric_metric::<u64>(metrics, crate::metrics::PROPOSAL_SUCCEEDED.name);
let failed = parse_numeric_metric::<u64>(metrics, crate::metrics::PROPOSAL_FAILED.name);
let aborted = parse_numeric_metric::<u64>(metrics, crate::metrics::PROPOSAL_ABORTED.name);
let started = PROPOSAL_STARTED.parse_numeric_metric::<u64>(metrics);
let succeeded = PROPOSAL_SUCCEEDED.parse_numeric_metric::<u64>(metrics);
let failed = PROPOSAL_FAILED.parse_numeric_metric::<u64>(metrics);
let aborted = PROPOSAL_ABORTED.parse_numeric_metric::<u64>(metrics);

assert_eq!(
started,
Expand Down Expand Up @@ -786,11 +794,11 @@ async fn decision_reached() {
Some(INITIAL_HEIGHT.unchecked_next().0)
);
assert_eq!(
parse_numeric_metric::<usize>(&metrics, crate::metrics::BATCHED_TRANSACTIONS.name),
BATCHED_TRANSACTIONS.parse_numeric_metric::<usize>(&metrics),
Some(expected_artifacts.execution_infos.len())
);
assert_eq!(
parse_numeric_metric::<usize>(&metrics, crate::metrics::REJECTED_TRANSACTIONS.name),
REJECTED_TRANSACTIONS.parse_numeric_metric::<usize>(&metrics),
Some(expected_artifacts.rejected_tx_hashes.len())
);
}
Expand Down
77 changes: 24 additions & 53 deletions crates/starknet_batcher/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use metrics::{counter, describe_counter, describe_gauge, gauge};
use metrics::{describe_gauge, gauge};
use starknet_api::block::BlockNumber;
use starknet_sequencer_metrics::metrics::{MetricCounter, MetricGauge};

Expand All @@ -9,59 +9,36 @@ pub const STORAGE_HEIGHT: MetricGauge = MetricGauge {
};

// Proposal metrics.
pub const PROPOSAL_STARTED: MetricCounter = MetricCounter {
name: "batcher_proposal_started",
description: "Counter of proposals started",
initial_value: 0,
};
pub const PROPOSAL_SUCCEEDED: MetricCounter = MetricCounter {
name: "batcher_proposal_succeeded",
description: "Counter of successful proposals",
initial_value: 0,
};
pub const PROPOSAL_FAILED: MetricCounter = MetricCounter {
name: "batcher_proposal_failed",
description: "Counter of failed proposals",
initial_value: 0,
};
pub const PROPOSAL_ABORTED: MetricCounter = MetricCounter {
name: "batcher_proposal_aborted",
description: "Counter of aborted proposals",
initial_value: 0,
};
pub const PROPOSAL_STARTED: MetricCounter =
MetricCounter::new("batcher_proposal_started", "Counter of proposals started", 0);
pub const PROPOSAL_SUCCEEDED: MetricCounter =
MetricCounter::new("batcher_proposal_succeeded", "Counter of successful proposals", 0);
pub const PROPOSAL_FAILED: MetricCounter =
MetricCounter::new("batcher_proposal_failed", "Counter of failed proposals", 0);
pub const PROPOSAL_ABORTED: MetricCounter =
MetricCounter::new("batcher_proposal_aborted", "Counter of aborted proposals", 0);

// Transaction metrics.
pub const BATCHED_TRANSACTIONS: MetricCounter = MetricCounter {
name: "batcher_batched_transactions",
description: "Counter of batched transactions",
initial_value: 0,
};
pub const REJECTED_TRANSACTIONS: MetricCounter = MetricCounter {
name: "batcher_rejected_transactions",
description: "Counter of rejected transactions",
initial_value: 0,
};
pub const BATCHED_TRANSACTIONS: MetricCounter =
MetricCounter::new("batcher_batched_transactions", "Counter of batched transactions", 0);
pub const REJECTED_TRANSACTIONS: MetricCounter =
MetricCounter::new("batcher_rejected_transactions", "Counter of rejected transactions", 0);

pub fn register_metrics(storage_height: BlockNumber) {
let storage_height_metric = gauge!(STORAGE_HEIGHT.name);
describe_gauge!(STORAGE_HEIGHT.name, STORAGE_HEIGHT.description);
#[allow(clippy::as_conversions)]
storage_height_metric.set(storage_height.0 as f64);

counter!(PROPOSAL_STARTED.name).absolute(PROPOSAL_STARTED.initial_value);
describe_counter!(PROPOSAL_STARTED.name, PROPOSAL_STARTED.description);
counter!(PROPOSAL_SUCCEEDED.name).absolute(PROPOSAL_STARTED.initial_value);
describe_counter!(PROPOSAL_SUCCEEDED.name, PROPOSAL_SUCCEEDED.description);
counter!(PROPOSAL_FAILED.name).absolute(PROPOSAL_STARTED.initial_value);
describe_counter!(PROPOSAL_FAILED.name, PROPOSAL_FAILED.description);
counter!(PROPOSAL_ABORTED.name).absolute(PROPOSAL_STARTED.initial_value);
describe_counter!(PROPOSAL_ABORTED.name, PROPOSAL_ABORTED.description);
PROPOSAL_STARTED.register();
PROPOSAL_STARTED.register();
PROPOSAL_SUCCEEDED.register();
PROPOSAL_FAILED.register();
PROPOSAL_ABORTED.register();

// In case of revert, consider calling `absolute`.
counter!(BATCHED_TRANSACTIONS.name).absolute(PROPOSAL_STARTED.initial_value);
describe_counter!(BATCHED_TRANSACTIONS.name, BATCHED_TRANSACTIONS.description);
counter!(REJECTED_TRANSACTIONS.name).absolute(PROPOSAL_STARTED.initial_value);
describe_counter!(REJECTED_TRANSACTIONS.name, REJECTED_TRANSACTIONS.description);
BATCHED_TRANSACTIONS.register();
REJECTED_TRANSACTIONS.register();
}

/// A handle to update the proposal metrics when the proposal is created and dropped.
Expand All @@ -72,7 +49,7 @@ pub(crate) struct ProposalMetricsHandle {

impl ProposalMetricsHandle {
pub fn new() -> Self {
counter!(crate::metrics::PROPOSAL_STARTED.name).increment(1);
PROPOSAL_STARTED.increment(1);
Self { finish_status: ProposalFinishStatus::Failed }
}

Expand All @@ -95,15 +72,9 @@ enum ProposalFinishStatus {
impl Drop for ProposalMetricsHandle {
fn drop(&mut self) {
match self.finish_status {
ProposalFinishStatus::Succeeded => {
counter!(crate::metrics::PROPOSAL_SUCCEEDED.name).increment(1)
}
ProposalFinishStatus::Aborted => {
counter!(crate::metrics::PROPOSAL_ABORTED.name).increment(1)
}
ProposalFinishStatus::Failed => {
counter!(crate::metrics::PROPOSAL_FAILED.name).increment(1)
}
ProposalFinishStatus::Succeeded => PROPOSAL_SUCCEEDED.increment(1),
ProposalFinishStatus::Aborted => PROPOSAL_ABORTED.increment(1),
ProposalFinishStatus::Failed => PROPOSAL_FAILED.increment(1),
}
}
}
1 change: 1 addition & 0 deletions crates/starknet_sequencer_metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ testing = []
workspace = true

[dependencies]
metrics.workspace = true
num-traits.workspace = true
regex.workspace = true
26 changes: 23 additions & 3 deletions crates/starknet_sequencer_metrics/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
use std::str::FromStr;

use metrics::{counter, describe_counter};
use num_traits::Num;
use regex::{escape, Regex};

pub struct MetricCounter {
pub name: &'static str,
pub description: &'static str,
pub initial_value: u64,
name: &'static str,
description: &'static str,
initial_value: u64,
}

impl MetricCounter {
pub const fn new(name: &'static str, description: &'static str, initial_value: u64) -> Self {
Self { name, description, initial_value }
}

pub fn register(&self) {
counter!(self.name).absolute(self.initial_value);
describe_counter!(self.name, self.description);
}

pub fn increment(&self, value: u64) {
counter!(self.name).increment(value);
}

pub fn parse_numeric_metric<T: Num + FromStr>(&self, metrics_as_string: &str) -> Option<T> {
parse_numeric_metric::<T>(metrics_as_string, self.name)
}
}

pub struct MetricGauge {
Expand Down
Loading