From 36591966700a526e9382343d517244d8b508e065 Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Wed, 18 Dec 2024 08:25:31 +0200 Subject: [PATCH] chore(starknet_http_server): add metrics for added txs commit-id:9bf71741 --- Cargo.lock | 1 + crates/starknet_http_server/Cargo.toml | 1 + .../starknet_http_server/src/http_server.rs | 6 +++- crates/starknet_http_server/src/lib.rs | 1 + crates/starknet_http_server/src/metrics.rs | 34 +++++++++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 crates/starknet_http_server/src/metrics.rs diff --git a/Cargo.lock b/Cargo.lock index 7412cef79c..c48f12e316 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10458,6 +10458,7 @@ dependencies = [ "axum", "hyper 0.14.30", "infra_utils", + "metrics 0.21.1", "papyrus_config", "reqwest 0.11.27", "serde", diff --git a/crates/starknet_http_server/Cargo.toml b/crates/starknet_http_server/Cargo.toml index 056eead0af..0ea77da072 100644 --- a/crates/starknet_http_server/Cargo.toml +++ b/crates/starknet_http_server/Cargo.toml @@ -15,6 +15,7 @@ workspace = true axum.workspace = true hyper.workspace = true infra_utils.workspace = true +metrics.workspace = true papyrus_config.workspace = true reqwest = { workspace = true, optional = true } serde.workspace = true diff --git a/crates/starknet_http_server/src/http_server.rs b/crates/starknet_http_server/src/http_server.rs index fe70577a1a..1934e00baf 100644 --- a/crates/starknet_http_server/src/http_server.rs +++ b/crates/starknet_http_server/src/http_server.rs @@ -16,6 +16,7 @@ use tracing::{error, info, instrument}; use crate::config::HttpServerConfig; use crate::errors::HttpServerRunError; +use crate::metrics::{count_added_transaction, count_transaction_status, init_metrics}; #[cfg(test)] #[path = "http_server_test.rs"] @@ -36,6 +37,7 @@ pub struct AppState { impl HttpServer { pub fn new(config: HttpServerConfig, gateway_client: SharedGatewayClient) -> Self { let app_state = AppState { gateway_client }; + init_metrics(); HttpServer { config, app_state } } @@ -62,12 +64,14 @@ async fn add_tx( State(app_state): State, Json(tx): Json, ) -> HttpServerResult> { - let gateway_input: GatewayInput = GatewayInput { rpc_tx: tx.clone(), message_metadata: None }; + count_added_transaction(); + let gateway_input: GatewayInput = GatewayInput { rpc_tx: tx, message_metadata: None }; let add_tx_result = app_state.gateway_client.add_tx(gateway_input).await.map_err(|join_err| { error!("Failed to process tx: {}", join_err); GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() } }); + count_transaction_status(add_tx_result.is_ok()); add_tx_result_as_json(add_tx_result) } diff --git a/crates/starknet_http_server/src/lib.rs b/crates/starknet_http_server/src/lib.rs index 350df0d4c2..2c50134f38 100644 --- a/crates/starknet_http_server/src/lib.rs +++ b/crates/starknet_http_server/src/lib.rs @@ -2,5 +2,6 @@ pub mod communication; pub mod config; pub mod errors; pub mod http_server; +mod metrics; #[cfg(feature = "testing")] pub mod test_utils; diff --git a/crates/starknet_http_server/src/metrics.rs b/crates/starknet_http_server/src/metrics.rs new file mode 100644 index 0000000000..0c2a7003a8 --- /dev/null +++ b/crates/starknet_http_server/src/metrics.rs @@ -0,0 +1,34 @@ +use metrics::{absolute_counter, describe_counter, register_counter}; + +const ADDED_TRANSACTIONS_TOTAL: (&str, &str, u64) = + ("ADDED_TRANSACTIONS_TOTAL", "Total number of transactions added", 0); +const ADDED_TRANSACTIONS_SUCCESS: (&str, &str, u64) = + ("ADDED_TRANSACTIONS_SUCCESS", "Number of successfully added transactions", 0); +const ADDED_TRANSACTIONS_FAILURE: (&str, &str, u64) = + ("ADDED_TRANSACTIONS_FAILURE", "Number of faulty added transactions", 0); + +pub(crate) fn init_metrics() { + register_counter!(ADDED_TRANSACTIONS_TOTAL.0); + describe_counter!(ADDED_TRANSACTIONS_TOTAL.0, ADDED_TRANSACTIONS_TOTAL.1); + absolute_counter!(ADDED_TRANSACTIONS_TOTAL.0, ADDED_TRANSACTIONS_TOTAL.2); + + register_counter!(ADDED_TRANSACTIONS_SUCCESS.0); + describe_counter!(ADDED_TRANSACTIONS_SUCCESS.0, ADDED_TRANSACTIONS_SUCCESS.1); + absolute_counter!(ADDED_TRANSACTIONS_SUCCESS.0, ADDED_TRANSACTIONS_SUCCESS.2); + + register_counter!(ADDED_TRANSACTIONS_FAILURE.0); + describe_counter!(ADDED_TRANSACTIONS_FAILURE.0, ADDED_TRANSACTIONS_FAILURE.1); + absolute_counter!(ADDED_TRANSACTIONS_FAILURE.0, ADDED_TRANSACTIONS_FAILURE.2); +} + +pub(crate) fn count_added_transaction() { + metrics::increment_counter!(ADDED_TRANSACTIONS_TOTAL.0); +} + +pub(crate) fn count_transaction_status(add_tx_success: bool) { + if add_tx_success { + metrics::increment_counter!(ADDED_TRANSACTIONS_SUCCESS.0); + } else { + metrics::increment_counter!(ADDED_TRANSACTIONS_FAILURE.0); + } +}