2019-09-06 14:21:09 +00:00
|
|
|
extern crate hyper;
|
2019-09-07 13:10:57 +00:00
|
|
|
extern crate prometheus;
|
2019-09-06 14:21:09 +00:00
|
|
|
extern crate prometheus_static_metric;
|
|
|
|
use hyper::rt::Future;
|
|
|
|
use hyper::service::service_fn_ok;
|
|
|
|
use hyper::{Body, Request, Response, Server};
|
2019-09-07 13:10:57 +00:00
|
|
|
use prometheus::{Encoder, IntGauge, IntGaugeVec, Opts, Registry, TextEncoder};
|
2019-09-06 14:21:09 +00:00
|
|
|
use std::thread;
|
2019-09-07 13:10:57 +00:00
|
|
|
use std::thread::JoinHandle;
|
2019-09-06 14:21:09 +00:00
|
|
|
|
|
|
|
pub struct ServerMetrics {
|
2019-09-07 13:10:57 +00:00
|
|
|
pub chonks_count: IntGaugeVec,
|
|
|
|
pub player_online: IntGauge,
|
|
|
|
pub entity_count: IntGauge,
|
|
|
|
pub tick_time: IntGaugeVec,
|
|
|
|
pub build_info: IntGauge,
|
|
|
|
pub light_count: IntGauge,
|
2019-09-06 14:21:09 +00:00
|
|
|
pub registry: Registry,
|
2019-09-07 13:10:57 +00:00
|
|
|
pub handle: Option<JoinHandle<()>>,
|
2019-09-06 14:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn metric_service(_req: Request<Body>) -> Response<Body> {
|
|
|
|
let encoder = TextEncoder::new();
|
|
|
|
let mut buffer = vec![];
|
|
|
|
let mf = prometheus::gather();
|
|
|
|
encoder.encode(&mf, &mut buffer).unwrap();
|
|
|
|
Response::builder()
|
|
|
|
.header(hyper::header::CONTENT_TYPE, encoder.format_type())
|
|
|
|
.body(Body::from(buffer))
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerMetrics {
|
2019-09-07 13:10:57 +00:00
|
|
|
/*
|
2019-09-06 14:21:09 +00:00
|
|
|
fn metric_service(&self, _req: Request<Body>) -> Response<Body> {
|
|
|
|
let encoder = TextEncoder::new();
|
|
|
|
let mut buffer = vec![];
|
|
|
|
let mf = self.registry.gather();
|
|
|
|
encoder.encode(&mf, &mut buffer).unwrap();
|
|
|
|
Response::builder()
|
|
|
|
.header(hyper::header::CONTENT_TYPE, encoder.format_type())
|
|
|
|
.body(Body::from(buffer))
|
|
|
|
.unwrap()
|
2019-09-07 13:10:57 +00:00
|
|
|
}*/
|
2019-09-06 14:21:09 +00:00
|
|
|
|
|
|
|
pub fn new() -> Self {
|
2019-09-07 13:10:57 +00:00
|
|
|
let opts = Opts::new(
|
|
|
|
"player_online",
|
|
|
|
"shows the number of clients connected to the server",
|
|
|
|
);
|
|
|
|
let player_online = IntGauge::with_opts(opts).unwrap();
|
|
|
|
let opts = Opts::new(
|
|
|
|
"entity_count",
|
|
|
|
"number of all entities currently active on the server",
|
|
|
|
);
|
|
|
|
let entity_count = IntGauge::with_opts(opts).unwrap();
|
2019-09-06 14:21:09 +00:00
|
|
|
let opts = Opts::new("veloren_build_info", "Build information")
|
|
|
|
.const_label("hash", common::util::GIT_HASH)
|
|
|
|
.const_label("version", "");
|
2019-09-07 13:10:57 +00:00
|
|
|
let build_info = IntGauge::with_opts(opts).unwrap();
|
|
|
|
let opts = Opts::new(
|
|
|
|
"light_count",
|
|
|
|
"number of all lights currently active on the server",
|
|
|
|
);
|
|
|
|
let light_count = IntGauge::with_opts(opts).unwrap();
|
|
|
|
let vec = IntGaugeVec::new(
|
|
|
|
Opts::new(
|
|
|
|
"chonks_count",
|
|
|
|
"number of all chonks currently active on the server",
|
|
|
|
),
|
|
|
|
&["type"],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let chonks_count: IntGaugeVec = IntGaugeVec::from(vec);
|
|
|
|
let vec = IntGaugeVec::new(
|
|
|
|
Opts::new("tick_time", "time in ns requiered for a tick of the server"),
|
|
|
|
&["period"],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let tick_time = IntGaugeVec::from(vec);
|
2019-09-06 14:21:09 +00:00
|
|
|
|
|
|
|
let registry = Registry::new();
|
|
|
|
//registry.register(Box::new(chonks_count.clone())).unwrap();
|
|
|
|
registry.register(Box::new(player_online.clone())).unwrap();
|
|
|
|
registry.register(Box::new(entity_count.clone())).unwrap();
|
|
|
|
registry.register(Box::new(build_info.clone())).unwrap();
|
2019-09-07 13:10:57 +00:00
|
|
|
//registry.register(Box::new(light_count.clone())).unwrap();
|
|
|
|
registry.register(Box::new(chonks_count.clone())).unwrap();
|
|
|
|
registry.register(Box::new(tick_time.clone())).unwrap();
|
2019-09-06 14:21:09 +00:00
|
|
|
prometheus::register(Box::new(player_online.clone())).unwrap();
|
|
|
|
prometheus::register(Box::new(entity_count.clone())).unwrap();
|
|
|
|
prometheus::register(Box::new(build_info.clone())).unwrap();
|
2019-09-07 13:10:57 +00:00
|
|
|
//prometheus::register(Box::new(light_count.clone())).unwrap();
|
|
|
|
prometheus::register(Box::new(chonks_count.clone())).unwrap();
|
|
|
|
prometheus::register(Box::new(tick_time.clone())).unwrap();
|
2019-09-06 14:21:09 +00:00
|
|
|
|
2019-09-07 13:10:57 +00:00
|
|
|
let mut metrics = Self {
|
2019-09-06 14:21:09 +00:00
|
|
|
chonks_count,
|
|
|
|
player_online,
|
|
|
|
entity_count,
|
|
|
|
tick_time,
|
|
|
|
build_info,
|
|
|
|
light_count,
|
|
|
|
registry,
|
2019-09-07 13:10:57 +00:00
|
|
|
handle: None,
|
2019-09-06 14:21:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let addr = ([0, 0, 0, 0], 14005).into();
|
|
|
|
let service = || service_fn_ok(metric_service);
|
|
|
|
let server = Server::bind(&addr)
|
|
|
|
.serve(service)
|
|
|
|
.map_err(|e| panic!("{}", e));
|
|
|
|
|
|
|
|
let handle = thread::spawn(|| {
|
|
|
|
hyper::rt::run(server);
|
|
|
|
});
|
2019-09-07 13:10:57 +00:00
|
|
|
metrics.handle = Some(handle);
|
2019-09-06 14:21:09 +00:00
|
|
|
|
|
|
|
metrics
|
|
|
|
}
|
2019-09-07 13:10:57 +00:00
|
|
|
}
|