Skip to content

Commit 8ca79f7

Browse files
apollo_infra: move metrics initialization to apollo_infra
1 parent c40d091 commit 8ca79f7

File tree

4 files changed

+54
-40
lines changed

4 files changed

+54
-40
lines changed

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
/// This controls whether metrics are collected across the entire application.
@@ -26,6 +34,45 @@ impl MetricsConfig {
2634
}
2735
}
2836

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