From 65eda618093059df24d0ec046b47021a639a0a41 Mon Sep 17 00:00:00 2001 From: dan-starkware <56217775+dan-starkware@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:18:24 +0200 Subject: [PATCH] feat(starknet_monitoring_endpoint): add config for collect metrics (#2472) --- config/sequencer/default_config.json | 5 +++++ .../src/integration_test_setup.rs | 2 +- .../starknet_monitoring_endpoint/src/config.rs | 9 ++++++++- .../src/monitoring_endpoint.rs | 16 +++++++++++++--- .../src/monitoring_endpoint_test.rs | 2 +- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config/sequencer/default_config.json b/config/sequencer/default_config.json index 5199edd4b2..23bbdcdf0d 100644 --- a/config/sequencer/default_config.json +++ b/config/sequencer/default_config.json @@ -924,6 +924,11 @@ "privacy": "Public", "value": 10000 }, + "monitoring_endpoint_config.collect_metrics": { + "description": "If true, collect and return metrics in the monitoring endpoint.", + "privacy": "Public", + "value": false + }, "monitoring_endpoint_config.ip": { "description": "The monitoring endpoint ip address.", "privacy": "Public", diff --git a/crates/starknet_integration_tests/src/integration_test_setup.rs b/crates/starknet_integration_tests/src/integration_test_setup.rs index 08d1f930d0..1167752cca 100644 --- a/crates/starknet_integration_tests/src/integration_test_setup.rs +++ b/crates/starknet_integration_tests/src/integration_test_setup.rs @@ -59,7 +59,7 @@ impl IntegrationTestSetup { ); // Wait for the node to start. - let MonitoringEndpointConfig { ip, port } = config.monitoring_endpoint_config; + let MonitoringEndpointConfig { ip, port, .. } = config.monitoring_endpoint_config; let is_alive_test_client = IsAliveClient::new(SocketAddr::from((ip, port))); let HttpServerConfig { ip, port } = config.http_server_config; diff --git a/crates/starknet_monitoring_endpoint/src/config.rs b/crates/starknet_monitoring_endpoint/src/config.rs index 82384cf7da..4eddcf2929 100644 --- a/crates/starknet_monitoring_endpoint/src/config.rs +++ b/crates/starknet_monitoring_endpoint/src/config.rs @@ -11,11 +11,12 @@ use validator::Validate; pub struct MonitoringEndpointConfig { pub ip: IpAddr, pub port: u16, + pub collect_metrics: bool, } impl Default for MonitoringEndpointConfig { fn default() -> Self { - Self { ip: "0.0.0.0".parse().unwrap(), port: 8082 } + Self { ip: "0.0.0.0".parse().unwrap(), port: 8082, collect_metrics: false } } } @@ -34,6 +35,12 @@ impl SerializeConfig for MonitoringEndpointConfig { "The monitoring endpoint port.", ParamPrivacyInput::Public, ), + ser_param( + "collect_metrics", + &self.collect_metrics, + "If true, collect and return metrics in the monitoring endpoint.", + ParamPrivacyInput::Public, + ), ]) } } diff --git a/crates/starknet_monitoring_endpoint/src/monitoring_endpoint.rs b/crates/starknet_monitoring_endpoint/src/monitoring_endpoint.rs index 863e218cee..dfbf4fae2e 100644 --- a/crates/starknet_monitoring_endpoint/src/monitoring_endpoint.rs +++ b/crates/starknet_monitoring_endpoint/src/monitoring_endpoint.rs @@ -6,7 +6,7 @@ use axum::response::{IntoResponse, Response}; use axum::routing::get; use axum::{async_trait, Router, Server}; use hyper::Error; -use metrics_exporter_prometheus::PrometheusHandle; +use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle}; use starknet_sequencer_infra::component_definitions::ComponentStarter; use starknet_sequencer_infra::errors::ComponentError; use tracing::{info, instrument}; @@ -31,7 +31,17 @@ pub struct MonitoringEndpoint { impl MonitoringEndpoint { pub fn new(config: MonitoringEndpointConfig, version: &'static str) -> Self { - MonitoringEndpoint { config, version, prometheus_handle: None } + // TODO(Tsabary): consider error handling + let prometheus_handle = if config.collect_metrics { + Some( + PrometheusBuilder::new() + .install_recorder() + .expect("should be able to build the recorder and install it globally"), + ) + } else { + None + }; + MonitoringEndpoint { config, version, prometheus_handle } } #[instrument( @@ -42,7 +52,7 @@ impl MonitoringEndpoint { ), level = "debug")] pub async fn run(&self) -> Result<(), Error> { - let MonitoringEndpointConfig { ip, port } = self.config; + let MonitoringEndpointConfig { ip, port, .. } = self.config; let endpoint_addr = SocketAddr::new(ip, port); let app = self.app(self.prometheus_handle.clone()); diff --git a/crates/starknet_monitoring_endpoint/src/monitoring_endpoint_test.rs b/crates/starknet_monitoring_endpoint/src/monitoring_endpoint_test.rs index 4a15f4b8ab..dcd05cbab3 100644 --- a/crates/starknet_monitoring_endpoint/src/monitoring_endpoint_test.rs +++ b/crates/starknet_monitoring_endpoint/src/monitoring_endpoint_test.rs @@ -90,7 +90,7 @@ async fn test_endpoint_as_server() { spawn(async move { setup_monitoring_endpoint().run().await }); yield_now().await; - let MonitoringEndpointConfig { ip, port } = MonitoringEndpointConfig::default(); + let MonitoringEndpointConfig { ip, port, .. } = MonitoringEndpointConfig::default(); let client = Client::new();