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
64
65
//! Exports metrics over HTTP.
//!
//! This exporter can utilize observers that are able to be converted to a textual representation
//! via [`Drain<String>`].  It will respond to any requests, regardless of the method or path.
//!
//! Awaiting on `async_run` will drive an HTTP server listening on the configured address.
#![deny(missing_docs)]

use async_std::net::TcpListener;
use async_std::prelude::*;
use metrics_core::{Builder, Drain, Observe, Observer};
use std::net::SocketAddr;

/// Exports metrics over HTTP.
pub struct HttpExporter<C, B> {
    controller: C,
    builder: B,
    address: SocketAddr,
}

impl<C, B> HttpExporter<C, B>
where
    C: Observe + Send + Sync + 'static,
    B: Builder + Send + Sync + 'static,
    B::Output: Drain<String> + Observer,
{
    /// Creates a new [`HttpExporter`] that listens on the given `address`.
    ///
    /// Observers expose their output by being converted into strings.
    pub fn new(controller: C, builder: B, address: SocketAddr) -> Self {
        HttpExporter {
            controller,
            builder,
            address,
        }
    }

    /// Starts an HTTP server on the `address` the exporter was originally configured with,
    /// responding to any request with the output of the configured observer.
    pub async fn async_run(self) -> std::io::Result<()> {
        let builder = self.builder;
        let controller = self.controller;

        let listener = TcpListener::bind(self.address).await?;
        let mut incoming = listener.incoming();

        while let Some(stream) = incoming.next().await {
            let mut stream = stream?;

            // seems like we have to read before writing if not we get an "Connection reset by peer"
            // cUrl and firefox are ok with 512 bytes but Chrome needs some more just in case we read 1024 bytes.
            let mut buffer = [0; 1024];
            stream.read(&mut buffer).await?;

            let mut observer = builder.build();
            controller.observe(&mut observer);
            let output = observer.drain();

            let response = format!("HTTP/1.1 200\r\n\r\n{}", output);
            stream.write_all(response.as_bytes()).await?;
        }

        Ok(())
    }
}