Skip to content

Commit 96213b3

Browse files
authored
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.
1 parent e36e8ce commit 96213b3

File tree

6 files changed

+46
-20
lines changed

6 files changed

+46
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ libp2p-quic = { version = "0.9.2", path = "transports/quic" }
9191
libp2p-relay = { version = "0.16.1", path = "protocols/relay" }
9292
libp2p-rendezvous = { version = "0.13.0", path = "protocols/rendezvous" }
9393
libp2p-request-response = { version = "0.25.1", path = "protocols/request-response" }
94-
libp2p-server = { version = "0.12.1", path = "misc/server" }
94+
libp2p-server = { version = "0.12.2", path = "misc/server" }
9595
libp2p-swarm = { version = "0.43.3", path = "swarm" }
9696
libp2p-swarm-derive = { version = "0.33.0", path = "swarm-derive" }
9797
libp2p-swarm-test = { version = "0.2.0", path = "swarm-test" }

misc/server/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [0.12.1]
7+
## [0.12.2]
8+
### Fixed
9+
- Adhere to `--metrics-path` flag and listen on `0.0.0.0:8888` (default IPFS metrics port).
10+
[PR 4392]
811

12+
[PR 4392]: https://github.com/libp2p/rust-libp2p/pull/4392
13+
14+
## [0.12.1]
915
### Changed
1016
- Move to tokio and hyper.
1117
See [PR 4311].

misc/server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libp2p-server"
3-
version = "0.12.1"
3+
version = "0.12.2"
44
authors = ["Max Inden <[email protected]>"]
55
edition = "2021"
66
repository = "https://github.com/libp2p/rust-libp2p"

misc/server/src/http_service.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@ use std::task::{Context, Poll};
3131

3232
const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0";
3333

34-
pub(crate) async fn metrics_server(registry: Registry) -> Result<(), std::io::Error> {
34+
pub(crate) async fn metrics_server(
35+
registry: Registry,
36+
metrics_path: String,
37+
) -> Result<(), std::io::Error> {
3538
// Serve on localhost.
36-
let addr = ([127, 0, 0, 1], 8080).into();
39+
let addr = ([0, 0, 0, 0], 8888).into();
3740

3841
// Use the tokio runtime to run the hyper server.
3942
let rt = tokio::runtime::Runtime::new()?;
4043
rt.block_on(async {
41-
let server = Server::bind(&addr).serve(MakeMetricService::new(registry));
42-
info!("Metrics server on http://{}/metrics", server.local_addr());
44+
let server =
45+
Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone()));
46+
info!(
47+
"Metrics server on http://{}{}",
48+
server.local_addr(),
49+
metrics_path
50+
);
4351
if let Err(e) = server.await {
4452
error!("server error: {}", e);
4553
}
@@ -49,6 +57,7 @@ pub(crate) async fn metrics_server(registry: Registry) -> Result<(), std::io::Er
4957

5058
pub(crate) struct MetricService {
5159
reg: Arc<Mutex<Registry>>,
60+
metrics_path: String,
5261
}
5362

5463
type SharedRegistry = Arc<Mutex<Registry>>;
@@ -75,7 +84,10 @@ impl MetricService {
7584
fn respond_with_404_not_found(&mut self) -> Response<String> {
7685
Response::builder()
7786
.status(StatusCode::NOT_FOUND)
78-
.body("Not found try localhost:[port]/metrics".to_string())
87+
.body(format!(
88+
"Not found try localhost:[port]/{}",
89+
self.metrics_path
90+
))
7991
.unwrap()
8092
}
8193
}
@@ -92,7 +104,7 @@ impl Service<Request<Body>> for MetricService {
92104
fn call(&mut self, req: Request<Body>) -> Self::Future {
93105
let req_path = req.uri().path();
94106
let req_method = req.method();
95-
let resp = if (req_method == Method::GET) && (req_path == "/metrics") {
107+
let resp = if (req_method == Method::GET) && (req_path == self.metrics_path) {
96108
// Encode and serve metrics from registry.
97109
self.respond_with_metrics()
98110
} else {
@@ -104,12 +116,14 @@ impl Service<Request<Body>> for MetricService {
104116

105117
pub(crate) struct MakeMetricService {
106118
reg: SharedRegistry,
119+
metrics_path: String,
107120
}
108121

109122
impl MakeMetricService {
110-
pub(crate) fn new(registry: Registry) -> MakeMetricService {
123+
pub(crate) fn new(registry: Registry, metrics_path: String) -> MakeMetricService {
111124
MakeMetricService {
112125
reg: Arc::new(Mutex::new(registry)),
126+
metrics_path,
113127
}
114128
}
115129
}
@@ -125,7 +139,8 @@ impl<T> Service<T> for MakeMetricService {
125139

126140
fn call(&mut self, _: T) -> Self::Future {
127141
let reg = self.reg.clone();
128-
let fut = async move { Ok(MetricService { reg }) };
142+
let metrics_path = self.metrics_path.clone();
143+
let fut = async move { Ok(MetricService { reg, metrics_path }) };
129144
Box::pin(fut)
130145
}
131146
}

misc/server/src/main.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use libp2p::swarm::{SwarmBuilder, SwarmEvent};
1818
use libp2p::tcp;
1919
use libp2p::yamux;
2020
use libp2p::Transport;
21-
use log::{debug, info};
21+
use log::{debug, info, warn};
2222
use prometheus_client::metrics::info::Info;
2323
use prometheus_client::registry::Registry;
2424
use std::error::Error;
@@ -79,7 +79,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
7979

8080
(peer_id, keypair)
8181
};
82-
println!("Local peer id: {local_peer_id}");
82+
info!("Local peer id: {local_peer_id}");
8383

8484
let transport = {
8585
let tcp_transport =
@@ -115,24 +115,24 @@ async fn main() -> Result<(), Box<dyn Error>> {
115115
let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build();
116116

117117
if config.addresses.swarm.is_empty() {
118-
log::warn!("No listen addresses configured.");
118+
warn!("No listen addresses configured.");
119119
}
120120
for address in &config.addresses.swarm {
121121
match swarm.listen_on(address.clone()) {
122122
Ok(_) => {}
123123
Err(e @ libp2p::TransportError::MultiaddrNotSupported(_)) => {
124-
log::warn!("Failed to listen on {address}, continuing anyways, {e}")
124+
warn!("Failed to listen on {address}, continuing anyways, {e}")
125125
}
126126
Err(e) => return Err(e.into()),
127127
}
128128
}
129129
if config.addresses.append_announce.is_empty() {
130-
log::warn!("No external addresses configured.");
130+
warn!("No external addresses configured.");
131131
}
132132
for address in &config.addresses.append_announce {
133133
swarm.add_external_address(address.clone())
134134
}
135-
log::info!(
135+
info!(
136136
"External addresses: {:?}",
137137
swarm.external_addresses().collect::<Vec<_>>()
138138
);
@@ -145,7 +145,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
145145
"A metric with a constant '1' value labeled by version",
146146
build_info,
147147
);
148-
thread::spawn(move || block_on(http_service::metrics_server(metric_registry)));
148+
thread::spawn(move || {
149+
block_on(http_service::metrics_server(
150+
metric_registry,
151+
opt.metrics_path,
152+
))
153+
});
149154

150155
let mut bootstrap_timer = Delay::new(BOOTSTRAP_INTERVAL);
151156

@@ -205,7 +210,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
205210
// metrics.record(&e)
206211
}
207212
SwarmEvent::NewListenAddr { address, .. } => {
208-
println!("Listening on {address:?}");
213+
info!("Listening on {address:?}");
209214
}
210215
_ => {}
211216
}

0 commit comments

Comments
 (0)