Disable metrics server for wasm

This commit is contained in:
durch
2024-03-19 12:41:35 +01:00
parent 72cffc71cc
commit 27978908d0
3 changed files with 39 additions and 39 deletions
Generated
+3 -28
View File
@@ -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",
+13 -5
View File
@@ -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"]
@@ -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);