1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
mod query;
mod results;
mod settings;
use std::convert::Infallible;
use std::sync::Arc;
use std::time;
use warp::Filter;

#[macro_use]
extern crate log;

async fn get_metrics(s: Arc<settings::Settings>) -> Result<impl warp::Reply, Infallible> {
    let start = time::Instant::now();
    let result = query::process_targets(&s).await;
    let duration = start.elapsed();
    Ok(vec![
        result.to_string(),
        format!(
            "web_exporter_scrape_duration_milliseconds {}",
            duration.as_millis()
        ),
    ]
    .join("\n"))
}

pub async fn run() {
    pretty_env_logger::init_custom_env("WEB_EXPORTER_LOG_LEVEL");
    match settings::Settings::new() {
        Ok(setting) => {
            let addr = setting.ip_address;
            let port = setting.port;
            let path = setting.metrics_path.clone();
            let s = Arc::new(setting);

            info!("settings: {:?}", s.clone());
            let state = warp::any().map(move || s.clone());
            let metrics = warp::path(path).and(state).and_then(get_metrics);
            let routes = metrics.with(warp::compression::gzip());
            let server = warp::serve(routes).run((addr, port));
            server.await;
            info!("Initialization Complete!");
        }
        Err(err) => error!("Cannot parse configuration file: {:?}", err),
    }
}