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
8 changes: 6 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/starknet_batcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ starknet_api = { workspace = true, features = ["testing"] }
starknet_infra_utils.workspace = true
starknet_l1_provider_types = { workspace = true, features = ["testing"] }
starknet_mempool_types = { workspace = true, features = ["testing"] }
starknet_sequencer_metrics.workspace = true
2 changes: 1 addition & 1 deletion crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use starknet_batcher_types::batcher_types::{
ValidateBlockInput,
};
use starknet_batcher_types::errors::BatcherError;
use starknet_infra_utils::metrics::parse_numeric_metric;
use starknet_l1_provider_types::MockL1ProviderClient;
use starknet_mempool_types::communication::MockMempoolClient;
use starknet_mempool_types::mempool_types::CommitBlockArgs;
use starknet_sequencer_metrics::metrics::parse_numeric_metric;
use starknet_state_sync_types::state_sync_types::SyncBlock;

use crate::batcher::{Batcher, MockBatcherStorageReaderTrait, MockBatcherStorageWriterTrait};
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_http_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ starknet_api.workspace = true
starknet_gateway_types = { workspace = true, features = ["testing"] }
starknet_infra_utils.workspace = true
starknet_sequencer_infra.workspace = true
starknet_sequencer_metrics.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["rt"] }
tracing.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_http_server/src/metrics_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use mempool_test_utils::starknet_api_test_utils::invoke_tx;
use metrics_exporter_prometheus::PrometheusBuilder;
use starknet_api::transaction::TransactionHash;
use starknet_gateway_types::communication::{GatewayClientError, MockGatewayClient};
use starknet_infra_utils::metrics::parse_numeric_metric;
use starknet_sequencer_infra::component_client::ClientError;
use starknet_sequencer_metrics::metrics::parse_numeric_metric;

use crate::config::HttpServerConfig;
use crate::metrics::{
Expand Down
2 changes: 0 additions & 2 deletions crates/starknet_infra_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ testing = []
workspace = true

[dependencies]
num-traits.workspace = true
regex.workspace = true
tokio = { workspace = true, features = ["process", "rt", "time"] }
tracing.workspace = true

Expand Down
1 change: 0 additions & 1 deletion crates/starknet_infra_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod command;
pub mod metrics;
pub mod path;
pub mod run_until;
pub mod tasks;
Expand Down
41 changes: 0 additions & 41 deletions crates/starknet_infra_utils/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,41 +0,0 @@
use std::str::FromStr;

use num_traits::Num;
use regex::{escape, Regex};

/// Parses a specific numeric metric value from a metrics string.
///
/// # Arguments
///
/// - `metrics_as_string`: A string containing the renders metrics data.
/// - `metric_name`: The name of the metric to search for.
///
/// # Type Parameters
///
/// - `T`: The numeric type to which the metric value will be parsed. The type must implement the
/// `Num` and `FromStr` traits, allowing it to represent numeric values and be parsed from a
/// string. Common types include `i32`, `u64`, and `f64`.
///
/// # Returns
///
/// - `Option<T>`: Returns `Some(T)` if the metric is found and successfully parsed into the
/// specified numeric type `T`. Returns `None` if the metric is not found or if parsing fails.
pub fn parse_numeric_metric<T: Num + FromStr>(
metrics_as_string: &str,
metric_name: &str,
) -> Option<T> {
// Create a regex to match "metric_name <number>".
let pattern = format!(r"{}\s+(\d+)", escape(metric_name));
let re = Regex::new(&pattern).expect("Invalid regex");

// Search for the pattern in the output.
if let Some(captures) = re.captures(metrics_as_string) {
// Extract the numeric value.
if let Some(value) = captures.get(1) {
// Parse the string into a number.
return value.as_str().parse().ok();
}
}
// If no match is found, return None.
None
}
1 change: 1 addition & 0 deletions crates/starknet_monitoring_endpoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ papyrus_config.workspace = true
serde.workspace = true
starknet_infra_utils.workspace = true
starknet_sequencer_infra.workspace = true
starknet_sequencer_metrics.workspace = true
thiserror = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }
tower = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_monitoring_endpoint/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use hyper::body::to_bytes;
use hyper::client::HttpConnector;
use hyper::Client;
use num_traits::Num;
use starknet_infra_utils::metrics::parse_numeric_metric;
use starknet_infra_utils::run_until::run_until;
use starknet_infra_utils::tracing::{CustomLogger, TraceLevel};
use starknet_sequencer_metrics::metrics::parse_numeric_metric;
use thiserror::Error;
use tracing::info;

Expand Down
4 changes: 4 additions & 0 deletions crates/starknet_sequencer_metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ testing = []

[lints]
workspace = true

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

use num_traits::Num;
use regex::{escape, Regex};

pub struct MetricCounter {
pub name: &'static str,
pub description: &'static str,
Expand All @@ -8,3 +13,40 @@ pub struct MetricGauge {
pub name: &'static str,
pub description: &'static str,
}

/// Parses a specific numeric metric value from a metrics string.
///
/// # Arguments
///
/// - `metrics_as_string`: A string containing the renders metrics data.
/// - `metric_name`: The name of the metric to search for.
///
/// # Type Parameters
///
/// - `T`: The numeric type to which the metric value will be parsed. The type must implement the
/// `Num` and `FromStr` traits, allowing it to represent numeric values and be parsed from a
/// string. Common types include `i32`, `u64`, and `f64`.
///
/// # Returns
///
/// - `Option<T>`: Returns `Some(T)` if the metric is found and successfully parsed into the
/// specified numeric type `T`. Returns `None` if the metric is not found or if parsing fails.
pub fn parse_numeric_metric<T: Num + FromStr>(
metrics_as_string: &str,
metric_name: &str,
) -> Option<T> {
// Create a regex to match "metric_name <number>".
let pattern = format!(r"{}\s+(\d+)", escape(metric_name));
let re = Regex::new(&pattern).expect("Invalid regex");

// Search for the pattern in the output.
if let Some(captures) = re.captures(metrics_as_string) {
// Extract the numeric value.
if let Some(value) = captures.get(1) {
// Parse the string into a number.
return value.as_str().parse().ok();
}
}
// If no match is found, return None.
None
}