Merge branch 'wusyong/metrics_port' into 'master'

Bind random port for ServerMetrics

Closes #321

See merge request veloren/veloren!584
This commit is contained in:
Acrimon 2019-10-11 09:06:34 +00:00
commit 0afa0b394f
3 changed files with 9 additions and 1 deletions

1
Cargo.lock generated
View File

@ -3579,6 +3579,7 @@ dependencies = [
"hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"portpicker 0.1.0 (git+https://github.com/wusyong/portpicker-rs?branch=fix_ipv6)",
"prometheus 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"prometheus-static-metric 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -24,3 +24,4 @@ crossbeam = "0.7.2"
prometheus = "0.7"
prometheus-static-metric = "0.2"
rouille = "3.0.0"
portpicker = { git = "https://github.com/wusyong/portpicker-rs", branch = "fix_ipv6" }

View File

@ -1,3 +1,5 @@
use log::info;
use portpicker::pick_unused_port;
use prometheus::{Encoder, Gauge, IntGauge, IntGaugeVec, Opts, Registry, TextEncoder};
use rouille::{router, Server};
use std::{
@ -93,7 +95,10 @@ impl ServerMetrics {
//TODO: make this a job
let handle = Some(thread::spawn(move || {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 14005);
let addr = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
pick_unused_port().expect("Failed to find unused port!"),
);
let server = Server::new(addr, move |request| {
router!(request,
(GET) (/metrics) => {
@ -107,6 +112,7 @@ impl ServerMetrics {
)
})
.expect("Failed to start server");
info!("Started server metrics: {}", addr);
while running2.load(Ordering::Relaxed) {
server.poll();
// Poll at 10Hz