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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::net::SocketAddr;

use hyper::service::make_service_fn;
use hyper::{body::Body, header::CONTENT_TYPE, service::service_fn, Request, Response};
use prometheus::{Encoder, TextEncoder};
use tracing::{error, info};

use crate::MetricsStopper;

#[allow(dead_code)]
pub(crate) fn bind_metrics(addr: SocketAddr) -> MetricsStopper {
    let (tx, rx) = tokio::sync::oneshot::channel::<()>();
    let handle = std::thread::spawn(move || {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("error on build tokio runtime for metrics server");

        rt.block_on(async {
            let server = hyper::Server::bind(&addr).serve(make_service_fn(|_| async {
                Ok::<_, hyper::Error>(service_fn(serve_req))
            }));

            let graceful_server = server.with_graceful_shutdown(async {
                rx.await.ok();
            });
            info!("METRICS | listening on http://{}", addr);
            if let Err(e) = graceful_server.await {
                error!("metrics server error: {}", e);
            }
            info!("METRICS | server stopped");
        });
    });
    MetricsStopper {
        stopper: Some(tx),
        stop_handle: Some(handle),
    }
}

#[allow(dead_code)]
async fn serve_req(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    if req.uri().path() != "/metrics" {
        // return hyper error
        Ok(Response::builder()
            .status(404)
            .body(Body::from("Not Found"))
            .unwrap())
    } else {
        let encoder = TextEncoder::new();
        let mut buffer = vec![];
        encoder
            .encode(&prometheus::gather(), &mut buffer)
            .expect("Failed to encode metrics");

        let response = Response::builder()
            .status(200)
            .header(CONTENT_TYPE, encoder.format_type())
            .body(Body::from(buffer))
            .unwrap();

        Ok(response)
    }
}