From 27978908d015e4a1c2d4e9232deb42d8c39dac08 Mon Sep 17 00:00:00 2001 From: durch Date: Tue, 19 Mar 2024 12:41:35 +0100 Subject: [PATCH] Disable metrics server for wasm --- Cargo.lock | 31 ++----------------- common/client-core/Cargo.toml | 18 ++++++++--- .../src/client/packet_statistics_control.rs | 29 +++++++++++++---- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 48c5ae1753..d7ab5721bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3088,25 +3088,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "h2" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51ee2dd2e4f378392eeff5d51618cd9a63166a2513846bbc55f21cfacd9199d4" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http 1.1.0", - "indexmap 2.0.2", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "half" version = "1.8.2" @@ -3431,7 +3412,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.3.21", + "h2", "http 0.2.9", "http-body 0.4.5", "httparse", @@ -3454,7 +3435,6 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.3", "http 1.1.0", "http-body 1.0.0", "httparse", @@ -3463,7 +3443,6 @@ dependencies = [ "pin-project-lite 0.2.13", "smallvec", "tokio", - "want", ] [[package]] @@ -3499,7 +3478,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" dependencies = [ "bytes", - "futures-channel", "futures-util", "http 1.1.0", "http-body 1.0.0", @@ -3507,9 +3485,6 @@ dependencies = [ "pin-project-lite 0.2.13", "socket2 0.5.4", "tokio", - "tower", - "tower-service", - "tracing", ] [[package]] @@ -8135,7 +8110,7 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2 0.3.21", + "h2", "http 0.2.9", "http-body 0.4.5", "hyper 0.14.27", @@ -10075,7 +10050,7 @@ dependencies = [ "bytes", "futures-core", "futures-util", - "h2 0.3.21", + "h2", "http 0.2.9", "http-body 0.4.5", "hyper 0.14.27", diff --git a/common/client-core/Cargo.toml b/common/client-core/Cargo.toml index 607ca15c36..013efbeef5 100644 --- a/common/client-core/Cargo.toml +++ b/common/client-core/Cargo.toml @@ -31,11 +31,6 @@ tokio = { workspace = true, features = ["macros"] } time = "0.3.17" zeroize = { workspace = true } -# For serving metrics -hyper = { version = "1", features = ["full"] } -http-body-util = "0.1" -hyper-util = { version = "0.1", features = ["full"] } - # internal nym-bandwidth-controller = { path = "../bandwidth-controller" } nym-config = { path = "../config" } @@ -54,6 +49,19 @@ nym-credential-storage = { path = "../credential-storage" } nym-network-defaults = { path = "../network-defaults" } si-scale = "0.2.2" +### For serving prometheus metrics +[target."cfg(not(target_arch = \"wasm32\"))".dependencies.hyper] +version = "1.2" +features = ["server", "http1"] + +[target."cfg(not(target_arch = \"wasm32\"))".dependencies.http-body-util] +version = "0.1" + +[target."cfg(not(target_arch = \"wasm32\"))".dependencies.hyper-util] +version = "0.1" +features = ["tokio"] +### + [target."cfg(not(target_arch = \"wasm32\"))".dependencies.tokio-stream] version = "0.1.11" features = ["time"] diff --git a/common/client-core/src/client/packet_statistics_control.rs b/common/client-core/src/client/packet_statistics_control.rs index 27fc90f0e4..f05054f73b 100644 --- a/common/client-core/src/client/packet_statistics_control.rs +++ b/common/client-core/src/client/packet_statistics_control.rs @@ -8,14 +8,23 @@ use nym_metrics::{inc, inc_by, metrics}; use si_scale::helpers::bibytes2; // Metrics server +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use http_body_util::Full; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use hyper::body::Bytes; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use hyper::server::conn::http1; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use hyper::service::service_fn; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use hyper::{Request, Response}; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use hyper_util::rt::TokioIo; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use std::convert::Infallible; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use std::net::SocketAddr; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] use tokio::net::TcpListener; use crate::spawn_future; @@ -496,11 +505,18 @@ impl PacketStatisticsControl { let snapshot_interval = Duration::from_millis(SNAPSHOT_INTERVAL_MS); let mut snapshot_interval = tokio::time::interval(snapshot_interval); - let metrics_port = 18000; - let addr = SocketAddr::from(([0, 0, 0, 0], metrics_port)); - let listener = TcpListener::bind(addr) - .await - .expect(&format!("Cannot bind metrics server to {metrics_port}!")); + cfg_if::cfg_if! { + if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { + log::warn!("Metrics server is not supported on wasm32-unknown-unknown"); + let listener = None; + } else { + let metrics_port = 18000; + let addr = SocketAddr::from(([0, 0, 0, 0], metrics_port)); + let listener = Some(TcpListener::bind(addr) + .await + .unwrap_or_else(|_ | panic!("Cannot bind metrics server to {metrics_port}!"))); + } + } loop { tokio::select! { @@ -514,7 +530,8 @@ impl PacketStatisticsControl { break; } }, - result = listener.accept() => { + // conditional will disable the branch if we're in wasm32-unknown-unknown + result = listener.as_ref().unwrap().accept(), if listener.is_some() => { if let Ok((stream, _)) = result { let io = TokioIo::new(stream);