diff --git a/apollo-router/Cargo.toml b/apollo-router/Cargo.toml index fc5aab720e..44f9c30224 100644 --- a/apollo-router/Cargo.toml +++ b/apollo-router/Cargo.toml @@ -220,8 +220,6 @@ sysinfo = { version = "0.35.0", features = [ "windows", ], default-features = false } thiserror = "2.0.0" -tikv-jemallocator = { version = "0.6.0", optional = true } -tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats"], optional = true } tokio.workspace = true tokio-stream = { version = "0.1.15", features = ["sync", "net"] } tokio-util = { version = "0.7.11", features = ["net", "codec", "time"] } @@ -292,6 +290,10 @@ hyperlocal = { version = "0.9.1", default-features = false, features = [ "client", ] } +[target.'cfg(target_os = "linux")'.dependencies] +tikv-jemallocator = { version = "0.6.0", optional = true } +tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats"], optional = true } + [dev-dependencies] axum = { version = "0.8.1", features = ["http2", "ws"] } axum-server = "0.7.1" diff --git a/apollo-router/src/axum_factory/axum_http_server_factory.rs b/apollo-router/src/axum_factory/axum_http_server_factory.rs index 6923aa380a..e21a3a51a5 100644 --- a/apollo-router/src/axum_factory/axum_http_server_factory.rs +++ b/apollo-router/src/axum_factory/axum_http_server_factory.rs @@ -502,16 +502,23 @@ where early_cancel: configuration.supergraph.early_cancel, experimental_log_on_broken_pipe: configuration.supergraph.experimental_log_on_broken_pipe, })); + let session_count_instrument = session_count_instrument(); + #[cfg(all( + feature = "global-allocator", + not(feature = "dhat-heap"), + target_os = "linux" + ))] + let (_epoch_advance_loop, jemalloc_instrument) = jemalloc_metrics_instruments(); // Tie the lifetime of the various instruments to the lifetime of the router - // by creating them in a no-op layer. - router = router.layer(layer_fn(|service| { - let _session_count_instrument = session_count_instrument(); + // by referencing them in a no-op layer. + router = router.layer(layer_fn(move |service| { + let _session_count_instrument = &session_count_instrument; #[cfg(all( feature = "global-allocator", not(feature = "dhat-heap"), target_os = "linux" ))] - let (_epoch_advance_loop, _je_malloc_instrument) = jemalloc_metrics_instruments(); + let _jemalloc_instrument = &jemalloc_instrument; service })); diff --git a/apollo-router/src/axum_factory/metrics.rs b/apollo-router/src/axum_factory/metrics.rs index 4954d8e13a..1e97b78c5b 100644 --- a/apollo-router/src/axum_factory/metrics.rs +++ b/apollo-router/src/axum_factory/metrics.rs @@ -11,13 +11,13 @@ pub(crate) mod jemalloc { use crate::metrics::meter_provider; - fn start_epoch_advance_loop() -> tokio::task::JoinHandle<()> { + pub(crate) fn start_epoch_advance_loop() -> tokio::task::JoinHandle<()> { tokio::spawn(async move { loop { - tokio::time::sleep(Duration::from_millis(500)).await; if let Err(e) = tikv_jemalloc_ctl::epoch::advance() { tracing::warn!("Failed to advance jemalloc epoch: {}", e); } + tokio::time::sleep(Duration::from_millis(500)).await; } }) }