Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(starknet_http_server): add metrics tests, still partial #2885

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions crates/starknet_http_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ tracing.workspace = true
validator.workspace = true

[dev-dependencies]
blockifier = { workspace = true, features = ["testing"] }
mempool_test_utils.workspace = true
metrics-exporter-prometheus.workspace = true
serde_json.workspace = true
starknet_api = { workspace = true, features = ["testing"] }
starknet_gateway_types = { workspace = true, features = ["testing"] }
starknet_http_server = { workspace = true, features = ["testing"] }
tokio = { workspace = true, features = ["rt"] }
81 changes: 80 additions & 1 deletion crates/starknet_http_server/src/http_server_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
use std::net::SocketAddr;
use std::sync::Arc;

use axum::body::{Bytes, HttpBody};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use blockifier::test_utils::CairoVersion;
use infra_utils::metrics::parse_numeric_metric;
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_sequencer_infra::component_client::ClientError;
use tokio::task;

use crate::http_server::add_tx_result_as_json;
use crate::config::HttpServerConfig;
use crate::http_server::{add_tx_result_as_json, HttpServer};
use crate::metrics::{
ADDED_TRANSACTIONS_FAILURE,
ADDED_TRANSACTIONS_SUCCESS,
ADDED_TRANSACTIONS_TOTAL,
};
use crate::test_utils::HttpTestClient;

#[tokio::test]
async fn test_tx_hash_json_conversion() {
Expand All @@ -20,3 +37,65 @@ async fn test_tx_hash_json_conversion() {
async fn to_bytes(res: Response) -> Bytes {
res.into_body().collect().await.unwrap().to_bytes()
}

#[tokio::test]
async fn get_metrics_test() {
let prometheus_handle = PrometheusBuilder::new()
.install_recorder()
.expect("should be able to build the recorder and install it globally");

// TODO(Tsabary): there is a bug in the http server where a failed gateway request crashes it.
// The current setup does not test that. Fix the bug and update this test accordingly.
// Create a mock gateway client that returns arbitrary responses.
const TXS_TO_SEND: usize = 3;
let success_txs_to_send = TXS_TO_SEND;
let failure_txs_to_send = TXS_TO_SEND - success_txs_to_send;
let mut mock_gateway_client = MockGatewayClient::new();
let mut i = 0;
mock_gateway_client.expect_add_tx().times(TXS_TO_SEND).returning(move |_| {
// TODO(Tsabary): the following discombobulated mechanism is a placeholder for the future
// test, where there will be failed requests as well.
i += 1;
match i {
Comment on lines +58 to +59
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The counter i is incremented before the match statement, which causes the first case (i=0) to be skipped. To properly test all cases, either move the increment after the match or initialize i to -1. This ensures the error case is actually tested.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

0 => Err(GatewayClientError::ClientError(ClientError::UnexpectedResponse(
"mock response".to_string(),
))),
1 => Ok(TransactionHash::default()),
_ => Ok(TransactionHash::default()),
}
});

// TODO(Tsabary): replace the const port with something that is not hardcoded.
// Create and run the server.
let http_server_config = HttpServerConfig { ip: "127.0.0.1".parse().unwrap(), port: 15123 };
let mut http_server =
HttpServer::new(http_server_config.clone(), Arc::new(mock_gateway_client));
tokio::spawn(async move { http_server.run().await });

let HttpServerConfig { ip, port } = http_server_config;
let add_tx_http_client = HttpTestClient::new(SocketAddr::from((ip, port)));

// Ensure the server starts running.
task::yield_now().await;

// Send transactions to the server.
for _ in std::iter::repeat(()).take(TXS_TO_SEND) {
let rpc_tx = invoke_tx(CairoVersion::default());
add_tx_http_client.add_tx(rpc_tx).await;
}

// Obtain metrics.
let metrics = prometheus_handle.render();

let added_transactions_total_count =
parse_numeric_metric::<usize>(&metrics, ADDED_TRANSACTIONS_TOTAL.0);
let added_transactions_success_count =
parse_numeric_metric::<usize>(&metrics, ADDED_TRANSACTIONS_SUCCESS.0);
let added_transactions_failure_count =
parse_numeric_metric::<usize>(&metrics, ADDED_TRANSACTIONS_FAILURE.0);

// Ensure the metrics are as expected.
assert_eq!(added_transactions_total_count.unwrap(), TXS_TO_SEND);
assert_eq!(added_transactions_success_count.unwrap(), success_txs_to_send);
assert_eq!(added_transactions_failure_count.unwrap(), failure_txs_to_send);
}
7 changes: 4 additions & 3 deletions crates/starknet_http_server/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use metrics::{absolute_counter, describe_counter, register_counter};
use tracing::info;

const ADDED_TRANSACTIONS_TOTAL: (&str, &str, u64) =
// TODO(Tsabary): add tests for metrics.
pub(crate) const ADDED_TRANSACTIONS_TOTAL: (&str, &str, u64) =
("ADDED_TRANSACTIONS_TOTAL", "Total number of transactions added", 0);
const ADDED_TRANSACTIONS_SUCCESS: (&str, &str, u64) =
pub(crate) const ADDED_TRANSACTIONS_SUCCESS: (&str, &str, u64) =
("ADDED_TRANSACTIONS_SUCCESS", "Number of successfully added transactions", 0);
const ADDED_TRANSACTIONS_FAILURE: (&str, &str, u64) =
pub(crate) const ADDED_TRANSACTIONS_FAILURE: (&str, &str, u64) =
("ADDED_TRANSACTIONS_FAILURE", "Number of faulty added transactions", 0);

pub(crate) fn init_metrics() {
Expand Down
Loading