Skip to content

Commit

Permalink
feat(starknet_monitoring_endpoint): add config for collect metrics (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-starkware authored Dec 9, 2024
1 parent 3893430 commit 65eda61
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions config/sequencer/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion crates/starknet_monitoring_endpoint/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand All @@ -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,
),
])
}
}
Expand Down
16 changes: 13 additions & 3 deletions crates/starknet_monitoring_endpoint/src/monitoring_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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(
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 65eda61

Please sign in to comment.