2019-10-11 09:06:34 +00:00
|
|
|
use log::info;
|
2019-09-10 13:22:34 +00:00
|
|
|
use prometheus::{Encoder, Gauge, IntGauge, IntGaugeVec, Opts, Registry, TextEncoder};
|
|
|
|
use rouille::{router, Server};
|
|
|
|
use std::{
|
|
|
|
convert::TryInto,
|
2020-02-01 20:39:39 +00:00
|
|
|
error::Error,
|
2019-10-11 12:19:55 +00:00
|
|
|
net::SocketAddr,
|
2019-09-10 13:22:34 +00:00
|
|
|
sync::{
|
2020-02-19 17:08:57 +00:00
|
|
|
atomic::{AtomicBool, AtomicU64, Ordering},
|
2019-09-10 13:22:34 +00:00
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
thread,
|
2019-09-20 04:41:44 +00:00
|
|
|
time::{Duration, SystemTime, UNIX_EPOCH},
|
2019-09-10 13:22:34 +00:00
|
|
|
};
|
2019-09-06 14:21:09 +00:00
|
|
|
|
2020-02-19 17:08:57 +00:00
|
|
|
pub struct TickMetrics {
|
2019-09-09 08:46:58 +00:00
|
|
|
pub chonks_count: IntGauge,
|
|
|
|
pub chunks_count: IntGauge,
|
2019-09-07 13:10:57 +00:00
|
|
|
pub player_online: IntGauge,
|
|
|
|
pub entity_count: IntGauge,
|
|
|
|
pub tick_time: IntGaugeVec,
|
|
|
|
pub build_info: IntGauge,
|
2019-09-10 13:22:34 +00:00
|
|
|
pub start_time: IntGauge,
|
|
|
|
pub time_of_day: Gauge,
|
2019-09-07 13:10:57 +00:00
|
|
|
pub light_count: IntGauge,
|
2020-02-19 17:08:57 +00:00
|
|
|
tick: Arc<AtomicU64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ServerMetrics {
|
2019-09-20 04:41:44 +00:00
|
|
|
running: Arc<AtomicBool>,
|
2020-02-19 17:08:57 +00:00
|
|
|
handle: Option<thread::JoinHandle<()>>,
|
|
|
|
registry: Option<Registry>,
|
|
|
|
tick: Arc<AtomicU64>,
|
2019-09-06 14:21:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 17:08:57 +00:00
|
|
|
impl TickMetrics {
|
|
|
|
pub fn new(registry: &Registry, tick: Arc<AtomicU64>) -> Result<Self, Box<dyn Error>> {
|
|
|
|
let player_online = IntGauge::with_opts(Opts::new(
|
2019-09-07 13:10:57 +00:00
|
|
|
"player_online",
|
|
|
|
"shows the number of clients connected to the server",
|
2020-02-19 17:08:57 +00:00
|
|
|
))?;
|
|
|
|
let entity_count = IntGauge::with_opts(Opts::new(
|
2019-09-07 13:10:57 +00:00
|
|
|
"entity_count",
|
|
|
|
"number of all entities currently active on the server",
|
2020-02-19 17:08:57 +00:00
|
|
|
))?;
|
2019-09-06 14:21:09 +00:00
|
|
|
let opts = Opts::new("veloren_build_info", "Build information")
|
2019-10-18 13:32:26 +00:00
|
|
|
.const_label("hash", &common::util::GIT_HASH)
|
2019-09-06 14:21:09 +00:00
|
|
|
.const_label("version", "");
|
2019-10-23 16:38:09 +00:00
|
|
|
let build_info = IntGauge::with_opts(opts)?;
|
2020-02-19 17:08:57 +00:00
|
|
|
let start_time = IntGauge::with_opts(Opts::new(
|
2019-09-10 13:22:34 +00:00
|
|
|
"veloren_start_time",
|
|
|
|
"start time of the server in seconds since EPOCH",
|
2020-02-19 17:08:57 +00:00
|
|
|
))?;
|
|
|
|
let time_of_day =
|
|
|
|
Gauge::with_opts(Opts::new("time_of_day", "ingame time in ingame-seconds"))?;
|
|
|
|
let light_count = IntGauge::with_opts(Opts::new(
|
2019-09-07 13:10:57 +00:00
|
|
|
"light_count",
|
|
|
|
"number of all lights currently active on the server",
|
2020-02-19 17:08:57 +00:00
|
|
|
))?;
|
|
|
|
let chonks_count = IntGauge::with_opts(Opts::new(
|
2019-09-09 08:46:58 +00:00
|
|
|
"chonks_count",
|
|
|
|
"number of all chonks currently active on the server",
|
2020-02-19 17:08:57 +00:00
|
|
|
))?;
|
|
|
|
let chunks_count = IntGauge::with_opts(Opts::new(
|
2019-09-09 08:46:58 +00:00
|
|
|
"chunks_count",
|
|
|
|
"number of all chunks currently active on the server",
|
2020-02-19 17:08:57 +00:00
|
|
|
))?;
|
|
|
|
let tick_time = IntGaugeVec::from(IntGaugeVec::new(
|
2019-09-07 13:10:57 +00:00
|
|
|
Opts::new("tick_time", "time in ns requiered for a tick of the server"),
|
|
|
|
&["period"],
|
2020-02-19 17:08:57 +00:00
|
|
|
)?);
|
2019-09-06 14:21:09 +00:00
|
|
|
|
2019-09-10 13:22:34 +00:00
|
|
|
let since_the_epoch = SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("Time went backwards");
|
2019-10-23 16:38:09 +00:00
|
|
|
start_time.set(since_the_epoch.as_secs().try_into()?);
|
2019-09-10 13:22:34 +00:00
|
|
|
|
2019-10-23 16:38:09 +00:00
|
|
|
registry.register(Box::new(player_online.clone()))?;
|
|
|
|
registry.register(Box::new(entity_count.clone()))?;
|
|
|
|
registry.register(Box::new(build_info.clone()))?;
|
|
|
|
registry.register(Box::new(start_time.clone()))?;
|
|
|
|
registry.register(Box::new(time_of_day.clone()))?;
|
|
|
|
registry.register(Box::new(chonks_count.clone()))?;
|
|
|
|
registry.register(Box::new(chunks_count.clone()))?;
|
|
|
|
registry.register(Box::new(tick_time.clone()))?;
|
2019-09-06 14:21:09 +00:00
|
|
|
|
2020-02-19 17:08:57 +00:00
|
|
|
Ok(Self {
|
|
|
|
chonks_count,
|
|
|
|
chunks_count,
|
|
|
|
player_online,
|
|
|
|
entity_count,
|
|
|
|
tick_time,
|
|
|
|
build_info,
|
|
|
|
start_time,
|
|
|
|
time_of_day,
|
|
|
|
light_count,
|
|
|
|
tick,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_100th_tick(&self) -> bool { self.tick.load(Ordering::Relaxed).rem_euclid(100) == 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerMetrics {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let running = Arc::new(AtomicBool::new(false));
|
|
|
|
let tick = Arc::new(AtomicU64::new(0));
|
|
|
|
let registry = Some(Registry::new());
|
|
|
|
|
|
|
|
Self {
|
|
|
|
running,
|
|
|
|
handle: None,
|
|
|
|
registry,
|
|
|
|
tick,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn registry(&self) -> &Registry {
|
|
|
|
match self.registry {
|
|
|
|
Some(ref r) => r,
|
|
|
|
None => panic!("You cannot longer register new metrics after the server has started!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self, addr: SocketAddr) -> Result<(), Box<dyn Error>> {
|
|
|
|
self.running.store(true, Ordering::Relaxed);
|
|
|
|
let running2 = self.running.clone();
|
|
|
|
|
|
|
|
let registry = self
|
|
|
|
.registry
|
|
|
|
.take()
|
|
|
|
.expect("ServerMetrics must be already started");
|
2019-09-06 14:21:09 +00:00
|
|
|
|
2019-09-10 13:22:34 +00:00
|
|
|
//TODO: make this a job
|
2020-02-19 17:08:57 +00:00
|
|
|
self.handle = Some(thread::spawn(move || {
|
2019-09-10 13:22:34 +00:00
|
|
|
let server = Server::new(addr, move |request| {
|
2019-09-08 23:59:19 +00:00
|
|
|
router!(request,
|
|
|
|
(GET) (/metrics) => {
|
|
|
|
let encoder = TextEncoder::new();
|
|
|
|
let mut buffer = vec![];
|
2019-09-10 13:22:34 +00:00
|
|
|
let mf = registry.gather();
|
2019-10-23 16:38:09 +00:00
|
|
|
encoder.encode(&mf, &mut buffer).expect("Failed to encoder metrics text.");
|
|
|
|
rouille::Response::text(String::from_utf8(buffer).expect("Failed to parse bytes as a string."))
|
2019-09-08 23:59:19 +00:00
|
|
|
},
|
|
|
|
_ => rouille::Response::empty_404()
|
|
|
|
)
|
2019-09-10 13:22:34 +00:00
|
|
|
})
|
2020-02-19 17:08:57 +00:00
|
|
|
.expect("Failed to start server");
|
2019-10-11 09:06:34 +00:00
|
|
|
info!("Started server metrics: {}", addr);
|
2019-09-20 04:41:44 +00:00
|
|
|
while running2.load(Ordering::Relaxed) {
|
2019-09-10 13:22:34 +00:00
|
|
|
server.poll();
|
2019-09-20 04:41:44 +00:00
|
|
|
// Poll at 10Hz
|
|
|
|
thread::sleep(Duration::from_millis(100));
|
2019-09-10 13:22:34 +00:00
|
|
|
}
|
|
|
|
}));
|
2020-02-19 17:08:57 +00:00
|
|
|
Ok(())
|
2019-09-10 13:22:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 17:08:57 +00:00
|
|
|
pub fn tick(&self) -> u64 { self.tick.fetch_add(1, Ordering::Relaxed) + 1 }
|
|
|
|
|
|
|
|
pub fn tick_clone(&self) -> Arc<AtomicU64> { self.tick.clone() }
|
2019-09-10 13:22:34 +00:00
|
|
|
}
|
2019-09-06 14:21:09 +00:00
|
|
|
|
2019-09-10 13:22:34 +00:00
|
|
|
impl Drop for ServerMetrics {
|
|
|
|
fn drop(&mut self) {
|
2019-09-20 04:41:44 +00:00
|
|
|
self.running.store(false, Ordering::Relaxed);
|
2019-09-10 13:22:34 +00:00
|
|
|
let handle = self.handle.take();
|
|
|
|
handle
|
2019-10-23 16:38:09 +00:00
|
|
|
.expect("ServerMetrics worker handle does not exist.")
|
2019-09-10 13:22:34 +00:00
|
|
|
.join()
|
|
|
|
.expect("Error shutting down prometheus metric exporter");
|
2019-09-06 14:21:09 +00:00
|
|
|
}
|
2019-09-07 13:10:57 +00:00
|
|
|
}
|