From 96213b37c6d7b8ea998d652d245f51b920bb4852 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 30 Aug 2023 16:39:07 +0200 Subject: [PATCH] fix(misc/server): adhere to --metrics-path and listen on 0.0.0.0:8888 This commit reverts the previous metrics behavior. Namely: - adhering to --metrics-path flag - listening on 0.0.0.0:8888 instead of 127.0.0.1:8080 Note that 8888 is the default IPFS debug (aka. metrics) port. See https://github.com/mxinden/rust-libp2p-server/blob/master/src/metric_server.rs for previous behavior. Pull-Request: #4392. --- Cargo.lock | 2 +- Cargo.toml | 2 +- misc/server/CHANGELOG.md | 8 +++++++- misc/server/Cargo.toml | 2 +- misc/server/src/http_service.rs | 31 +++++++++++++++++++++++-------- misc/server/src/main.rs | 21 +++++++++++++-------- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 886aad7c1ab..83e140de29d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3165,7 +3165,7 @@ dependencies = [ [[package]] name = "libp2p-server" -version = "0.12.1" +version = "0.12.2" dependencies = [ "base64 0.21.2", "clap", diff --git a/Cargo.toml b/Cargo.toml index 3b239fec3d3..97c8c10adfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,7 +91,7 @@ libp2p-quic = { version = "0.9.2", path = "transports/quic" } libp2p-relay = { version = "0.16.1", path = "protocols/relay" } libp2p-rendezvous = { version = "0.13.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.25.1", path = "protocols/request-response" } -libp2p-server = { version = "0.12.1", path = "misc/server" } +libp2p-server = { version = "0.12.2", path = "misc/server" } libp2p-swarm = { version = "0.43.3", path = "swarm" } libp2p-swarm-derive = { version = "0.33.0", path = "swarm-derive" } libp2p-swarm-test = { version = "0.2.0", path = "swarm-test" } diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index aee8707b5bb..8bb5768822c 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -4,8 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.12.1] +## [0.12.2] +### Fixed +- Adhere to `--metrics-path` flag and listen on `0.0.0.0:8888` (default IPFS metrics port). + [PR 4392] +[PR 4392]: https://github.com/libp2p/rust-libp2p/pull/4392 + +## [0.12.1] ### Changed - Move to tokio and hyper. See [PR 4311]. diff --git a/misc/server/Cargo.toml b/misc/server/Cargo.toml index 4604ed48cf6..32b815c407a 100644 --- a/misc/server/Cargo.toml +++ b/misc/server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-server" -version = "0.12.1" +version = "0.12.2" authors = ["Max Inden "] edition = "2021" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/misc/server/src/http_service.rs b/misc/server/src/http_service.rs index 46cb7aacb84..8b5779f3cef 100644 --- a/misc/server/src/http_service.rs +++ b/misc/server/src/http_service.rs @@ -31,15 +31,23 @@ use std::task::{Context, Poll}; const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0"; -pub(crate) async fn metrics_server(registry: Registry) -> Result<(), std::io::Error> { +pub(crate) async fn metrics_server( + registry: Registry, + metrics_path: String, +) -> Result<(), std::io::Error> { // Serve on localhost. - let addr = ([127, 0, 0, 1], 8080).into(); + let addr = ([0, 0, 0, 0], 8888).into(); // Use the tokio runtime to run the hyper server. let rt = tokio::runtime::Runtime::new()?; rt.block_on(async { - let server = Server::bind(&addr).serve(MakeMetricService::new(registry)); - info!("Metrics server on http://{}/metrics", server.local_addr()); + let server = + Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone())); + info!( + "Metrics server on http://{}{}", + server.local_addr(), + metrics_path + ); if let Err(e) = server.await { error!("server error: {}", e); } @@ -49,6 +57,7 @@ pub(crate) async fn metrics_server(registry: Registry) -> Result<(), std::io::Er pub(crate) struct MetricService { reg: Arc>, + metrics_path: String, } type SharedRegistry = Arc>; @@ -75,7 +84,10 @@ impl MetricService { fn respond_with_404_not_found(&mut self) -> Response { Response::builder() .status(StatusCode::NOT_FOUND) - .body("Not found try localhost:[port]/metrics".to_string()) + .body(format!( + "Not found try localhost:[port]/{}", + self.metrics_path + )) .unwrap() } } @@ -92,7 +104,7 @@ impl Service> for MetricService { fn call(&mut self, req: Request) -> Self::Future { let req_path = req.uri().path(); let req_method = req.method(); - let resp = if (req_method == Method::GET) && (req_path == "/metrics") { + let resp = if (req_method == Method::GET) && (req_path == self.metrics_path) { // Encode and serve metrics from registry. self.respond_with_metrics() } else { @@ -104,12 +116,14 @@ impl Service> for MetricService { pub(crate) struct MakeMetricService { reg: SharedRegistry, + metrics_path: String, } impl MakeMetricService { - pub(crate) fn new(registry: Registry) -> MakeMetricService { + pub(crate) fn new(registry: Registry, metrics_path: String) -> MakeMetricService { MakeMetricService { reg: Arc::new(Mutex::new(registry)), + metrics_path, } } } @@ -125,7 +139,8 @@ impl Service for MakeMetricService { fn call(&mut self, _: T) -> Self::Future { let reg = self.reg.clone(); - let fut = async move { Ok(MetricService { reg }) }; + let metrics_path = self.metrics_path.clone(); + let fut = async move { Ok(MetricService { reg, metrics_path }) }; Box::pin(fut) } } diff --git a/misc/server/src/main.rs b/misc/server/src/main.rs index c2dff1f9228..67abb5b2549 100644 --- a/misc/server/src/main.rs +++ b/misc/server/src/main.rs @@ -18,7 +18,7 @@ use libp2p::swarm::{SwarmBuilder, SwarmEvent}; use libp2p::tcp; use libp2p::yamux; use libp2p::Transport; -use log::{debug, info}; +use log::{debug, info, warn}; use prometheus_client::metrics::info::Info; use prometheus_client::registry::Registry; use std::error::Error; @@ -79,7 +79,7 @@ async fn main() -> Result<(), Box> { (peer_id, keypair) }; - println!("Local peer id: {local_peer_id}"); + info!("Local peer id: {local_peer_id}"); let transport = { let tcp_transport = @@ -115,24 +115,24 @@ async fn main() -> Result<(), Box> { let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build(); if config.addresses.swarm.is_empty() { - log::warn!("No listen addresses configured."); + warn!("No listen addresses configured."); } for address in &config.addresses.swarm { match swarm.listen_on(address.clone()) { Ok(_) => {} Err(e @ libp2p::TransportError::MultiaddrNotSupported(_)) => { - log::warn!("Failed to listen on {address}, continuing anyways, {e}") + warn!("Failed to listen on {address}, continuing anyways, {e}") } Err(e) => return Err(e.into()), } } if config.addresses.append_announce.is_empty() { - log::warn!("No external addresses configured."); + warn!("No external addresses configured."); } for address in &config.addresses.append_announce { swarm.add_external_address(address.clone()) } - log::info!( + info!( "External addresses: {:?}", swarm.external_addresses().collect::>() ); @@ -145,7 +145,12 @@ async fn main() -> Result<(), Box> { "A metric with a constant '1' value labeled by version", build_info, ); - thread::spawn(move || block_on(http_service::metrics_server(metric_registry))); + thread::spawn(move || { + block_on(http_service::metrics_server( + metric_registry, + opt.metrics_path, + )) + }); let mut bootstrap_timer = Delay::new(BOOTSTRAP_INTERVAL); @@ -205,7 +210,7 @@ async fn main() -> Result<(), Box> { // metrics.record(&e) } SwarmEvent::NewListenAddr { address, .. } => { - println!("Listening on {address:?}"); + info!("Listening on {address:?}"); } _ => {} }