refactoring the futures to be more readable

This commit is contained in:
Jan Christian Grünhage
2019-02-05 01:20:32 +01:00
parent f05a6d9083
commit 8748ba7e5b
3 changed files with 179 additions and 25 deletions

View File

@ -1,20 +1,25 @@
#[macro_use]
extern crate serde_derive;
extern crate toml;
extern crate hyper;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate prometheus;
extern crate futures;
extern crate tokio;
extern crate env_logger;
use hyper::header::CONTENT_TYPE;
use hyper::{Body, Request, Response, Server};
use hyper::rt::Future;
use hyper::service::service_fn_ok;
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};
use lazy_static::lazy_static;
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};
use prometheus::*;
use futures::future::lazy;
//TODO: Replace with useful metrics, these are beyond useless
lazy_static! {
static ref HTTP_COUNTER: Counter = register_counter!(opts!(
"example_http_requests_total",
@ -36,16 +41,11 @@ lazy_static! {
.unwrap();
}
fn main() {
//TODO: Add service that does the pinging, based on oping and the example over at https://tokio.rs/docs/futures/spawning/
//TODO: Clean this shameful mess up!
//TODO: Ivestigate why all samples end up in all histogram buckets.
//TODO: Do config reading etc
fn start_serving_metrics() {
let serve_metrics = || {
service_fn_ok(|_req| {
HTTP_COUNTER.inc();
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["all"]).start_timer();
let metric_families = prometheus::gather();
let mut buffer = vec![];
let encoder = TextEncoder::new();
@ -58,19 +58,28 @@ fn main() {
})
};
println!("listening addr 127.0.0.1:9898");
let server = Server::bind(&([127, 0, 0, 1], 9898).into()).serve(serve_metrics);
// Prepare some signal for when the server should start
// shutting down...
let (tx, rx) = futures::sync::oneshot::channel::<()>();
let graceful = server.with_graceful_shutdown(rx).map_err(|err| {
eprintln!("server error: {}", err)
});
// Spawn `server` onto an Executor...
hyper::rt::run(graceful);
// And later, trigger the signal by calling `tx.send(())`.
let _ = tx.send(());
let server = Server::bind(&([127, 0, 0, 1], 9898).into())
.serve(serve_metrics)
.map_err(|err| eprintln!("server error: {}", err));
tokio::spawn(server);
}
fn start_pinging_hosts() {
tokio::spawn(lazy(|| {
//TODO: Implement the pinging, based on oping and the example over at
// https://tokio.rs/docs/futures/spawning/
Ok(())
}));
}
//TODO: Clean this shameful mess up!
//TODO: Do config reading, cli args, logging etc
fn main() {
env_logger::init();
tokio::run(lazy(|| {
start_serving_metrics();
start_pinging_hosts();
Ok(())
}));
}