Skip to content

Commit 6fb60e1

Browse files
apollo_infra: move metrics initialization to apollo_infra
1 parent e029d08 commit 6fb60e1

File tree

6 files changed

+54
-42
lines changed

6 files changed

+54
-42
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_dashboard/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ apollo_l1_provider.workspace = true
3131
apollo_mempool.workspace = true
3232
apollo_mempool_p2p.workspace = true
3333
apollo_metrics.workspace = true
34-
apollo_monitoring_endpoint.workspace = true
3534
apollo_network.workspace = true
3635
apollo_state_sync_metrics.workspace = true
3736
apollo_storage.workspace = true

crates/apollo_dashboard/src/alert_scenarios/transaction_delays.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use apollo_batcher::metrics::NUM_TRANSACTION_IN_BLOCK;
22
use apollo_http_server::metrics::HTTP_SERVER_ADD_TX_LATENCY;
3+
use apollo_infra::metrics::HISTOGRAM_BUCKETS;
34
use apollo_mempool_p2p::metrics::MEMPOOL_P2P_NUM_CONNECTED_PEERS;
45
use apollo_metrics::metrics::MetricQueryName;
5-
use apollo_monitoring_endpoint::monitoring_endpoint::HISTOGRAM_BUCKETS;
66

77
use crate::alerts::{
88
Alert,

crates/apollo_infra/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ apollo_infra_utils.workspace = true
1818
apollo_metrics.workspace = true
1919
async-trait.workspace = true
2020
hyper = { workspace = true, features = ["client", "http2", "server", "tcp"] }
21+
metrics-exporter-prometheus.workspace = true
2122
rstest.workspace = true
2223
serde = { workspace = true, features = ["derive"] }
2324
serde_json.workspace = true

crates/apollo_infra/src/metrics.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ use apollo_metrics::metrics::{
33
MetricCounter,
44
MetricGauge,
55
MetricHistogram,
6+
COLLECT_SEQUENCER_PROFILING_METRICS,
67
};
8+
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle};
79
use serde::{Deserialize, Serialize};
810

911
use crate::requests::LABEL_NAME_REQUEST_VARIANT;
12+
use crate::tokio_metrics::setup_tokio_metrics;
13+
14+
pub const HISTOGRAM_BUCKETS: &[f64] = &[
15+
0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 25.0, 50.0,
16+
100.0, 250.0,
17+
];
1018

1119
/// Configuration for metrics collection.
1220
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq)]
@@ -29,6 +37,45 @@ impl MetricsConfig {
2937
}
3038
}
3139

40+
/// Initializes the metrics recorder and tokio metrics if metrics collection is enabled.
41+
/// This should be called once during application startup, before creating components that use
42+
/// metrics.
43+
///
44+
/// Returns a PrometheusHandle if metrics collection is enabled, None otherwise.
45+
///
46+
/// # Example
47+
/// ```no_run
48+
/// use apollo_infra::metrics::{initialize_metrics_recorder, MetricsConfig};
49+
///
50+
/// let config = MetricsConfig::enabled();
51+
/// let prometheus_handle = initialize_metrics_recorder(config);
52+
/// ```
53+
pub fn initialize_metrics_recorder(config: MetricsConfig) -> Option<PrometheusHandle> {
54+
// TODO(Tsabary): consider error handling
55+
let prometheus_handle = if config.collect_metrics {
56+
// TODO(Lev): add tests that show the metrics are collected / not collected based on the
57+
// config value.
58+
COLLECT_SEQUENCER_PROFILING_METRICS
59+
.set(config.collect_profiling_metrics)
60+
.expect("Should be able to set profiling metrics collection.");
61+
62+
Some(
63+
PrometheusBuilder::new()
64+
.set_buckets(HISTOGRAM_BUCKETS)
65+
.expect("Should be able to set buckets")
66+
.install_recorder()
67+
.expect("should be able to build the recorder and install it globally"),
68+
)
69+
} else {
70+
None
71+
};
72+
73+
// Setup tokio metrics along with other metrics initialization
74+
setup_tokio_metrics();
75+
76+
prometheus_handle
77+
}
78+
3279
/// Metrics of a local client.
3380
#[derive(Clone)]
3481
pub struct LocalClientMetrics {

crates/apollo_monitoring_endpoint/src/monitoring_endpoint.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
use std::net::SocketAddr;
22

33
use apollo_infra::component_definitions::ComponentStarter;
4-
use apollo_infra::metrics::MetricsConfig;
5-
use apollo_infra::tokio_metrics::setup_tokio_metrics;
4+
use apollo_infra::metrics::{initialize_metrics_recorder, MetricsConfig};
65
use apollo_infra::trace_util::{configure_tracing, get_log_directives, set_log_level};
76
use apollo_infra_utils::type_name::short_type_name;
87
use apollo_l1_provider_types::{L1ProviderSnapshot, SharedL1ProviderClient};
98
use apollo_mempool_types::communication::SharedMempoolClient;
109
use apollo_mempool_types::mempool_types::MempoolSnapshot;
11-
use apollo_metrics::metrics::COLLECT_SEQUENCER_PROFILING_METRICS;
1210
use apollo_monitoring_endpoint_config::config::MonitoringEndpointConfig;
1311
use axum::extract::Path;
1412
use axum::http::StatusCode;
1513
use axum::response::{IntoResponse, Response};
1614
use axum::routing::{get, post};
1715
use axum::{async_trait, Json, Router, Server};
1816
use hyper::Error;
19-
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle};
17+
use metrics_exporter_prometheus::PrometheusHandle;
2018
use tracing::level_filters::LevelFilter;
2119
use tracing::{error, info, instrument};
2220

@@ -34,11 +32,6 @@ pub(crate) const L1_PROVIDER_SNAPSHOT: &str = "l1ProviderSnapshot";
3432
pub(crate) const SET_LOG_LEVEL: &str = "setLogLevel";
3533
pub(crate) const LOG_LEVEL: &str = "logLevel";
3634

37-
pub const HISTOGRAM_BUCKETS: &[f64] = &[
38-
0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 25.0, 50.0,
39-
100.0, 250.0,
40-
];
41-
4235
pub struct MonitoringEndpoint {
4336
config: MonitoringEndpointConfig,
4437
version: &'static str,
@@ -51,28 +44,10 @@ impl MonitoringEndpoint {
5144
pub fn new(
5245
config: MonitoringEndpointConfig,
5346
version: &'static str,
54-
metrics_config: MetricsConfig,
47+
prometheus_handle: Option<PrometheusHandle>,
5548
mempool_client: Option<SharedMempoolClient>,
5649
l1_provider_client: Option<SharedL1ProviderClient>,
5750
) -> Self {
58-
// TODO(Tsabary): consider error handling
59-
let prometheus_handle = if metrics_config.collect_metrics {
60-
// TODO(Lev): add tests that show the metrics are collected / not collected based on the
61-
// config value.
62-
COLLECT_SEQUENCER_PROFILING_METRICS
63-
.set(metrics_config.collect_profiling_metrics)
64-
.expect("Should be able to set profiling metrics collection.");
65-
66-
Some(
67-
PrometheusBuilder::new()
68-
.set_buckets(HISTOGRAM_BUCKETS)
69-
.expect("Should be able to set buckets")
70-
.install_recorder()
71-
.expect("should be able to build the recorder and install it globally"),
72-
)
73-
} else {
74-
None
75-
};
7651
MonitoringEndpoint {
7752
config,
7853
version,
@@ -141,24 +116,15 @@ impl MonitoringEndpoint {
141116
}
142117
}
143118

144-
// TODO(tsabary): finalize the separation of the metrics recorder initialization and the monitoring
145-
// endpoint setup
146119
pub fn create_monitoring_endpoint(
147120
config: MonitoringEndpointConfig,
148121
version: &'static str,
149122
metrics_config: MetricsConfig,
150123
mempool_client: Option<SharedMempoolClient>,
151124
l1_provider_client: Option<SharedL1ProviderClient>,
152125
) -> MonitoringEndpoint {
153-
let result = MonitoringEndpoint::new(
154-
config,
155-
version,
156-
metrics_config,
157-
mempool_client,
158-
l1_provider_client,
159-
);
160-
setup_tokio_metrics();
161-
result
126+
let prometheus_handle = initialize_metrics_recorder(metrics_config);
127+
MonitoringEndpoint::new(config, version, prometheus_handle, mempool_client, l1_provider_client)
162128
}
163129

164130
#[async_trait]

0 commit comments

Comments
 (0)