metrics_exporter_http/
lib.rs

1//! Exports metrics over HTTP.
2//!
3//! This exporter can utilize observers that are able to be converted to a textual representation
4//! via [`Drain<String>`].  It will respond to any requests, regardless of the method or path.
5//!
6//! Awaiting on `async_run` will drive an HTTP server listening on the configured address.
7#![deny(missing_docs)]
8
9use hyper::{
10    service::{make_service_fn, service_fn},
11    {Body, Error, Response, Server},
12};
13use metrics_core::{Builder, Drain, Observe, Observer};
14use std::{net::SocketAddr, sync::Arc};
15
16/// Exports metrics over HTTP.
17pub struct HttpExporter<C, B> {
18    controller: C,
19    builder: B,
20    address: SocketAddr,
21}
22
23impl<C, B> HttpExporter<C, B>
24where
25    C: Observe + Send + Sync + 'static,
26    B: Builder + Send + Sync + 'static,
27    B::Output: Drain<String> + Observer,
28{
29    /// Creates a new [`HttpExporter`] that listens on the given `address`.
30    ///
31    /// Observers expose their output by being converted into strings.
32    pub fn new(controller: C, builder: B, address: SocketAddr) -> Self {
33        HttpExporter {
34            controller,
35            builder,
36            address,
37        }
38    }
39
40    /// Starts an HTTP server on the `address` the exporter was originally configured with,
41    /// responding to any request with the output of the configured observer.
42    pub async fn async_run(self) -> hyper::error::Result<()> {
43        let builder = Arc::new(self.builder);
44        let controller = Arc::new(self.controller);
45
46        let make_svc = make_service_fn(move |_| {
47            let builder = builder.clone();
48            let controller = controller.clone();
49
50            async move {
51                Ok::<_, Error>(service_fn(move |_| {
52                    let builder = builder.clone();
53                    let controller = controller.clone();
54
55                    async move {
56                        let mut observer = builder.build();
57                        controller.observe(&mut observer);
58                        let output = observer.drain();
59                        Ok::<_, Error>(Response::new(Body::from(output)))
60                    }
61                }))
62            }
63        });
64
65        Server::bind(&self.address).serve(make_svc).await
66    }
67}