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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*! A simple prometheus-compatible metrics framework

A metric has a name and a value of type `f64`.  There is a single global
set of metrics which can be set or incremented from any thread.

```
use epimetheus::metric;

metric!(foobar).set(12.3);
metric!(foobar).add(0.7);
```

If you increment a metric which has never been set, it is
considered to start from zero.

```
# use epimetheus::metric;
metric!(barqux).add(6.5);
```

A metric's name can include labels.  Whereas the base part of a metric's
name must be known at compile-time, the labels can be any runtime value
which implements `Display`.  This can be useful - but see the performance
notes below.

```
# use epimetheus::metric;
# let is_admin = |_| "";
metric!(whizzbang{user=7, admin=is_admin(7)}).add(2.0);
metric!(whizzbang{user=2, admin=is_admin(2)}).add(12.3);
```

The first time you update some metric, a thread is spawned which runs an
extremely simple HTTP server.  By default it runs on port 9898, but you
can change this by setting the `RUST_METRICS_PORT` environment variable.
Connect to the server to see the current values of all metrics.  Metrics appear
in the output after being updated for the first time.

```text
$ curl localhost:9898
barqux 6.5
foobar 20
whizzbang{admin="false",user="2"} 12.3
whizzbang{admin="true",user="7"} 2
```

Tip: If you want to specify the metrics port in your application iteself,
do this at the start of your `main()` function:

```
std::env::set_var("RUST_METRICS_PORT", "1234");
```

Make sure you do this _before_ updating any metrics.

*/

use crossbeam_channel::{Receiver, Sender};
use lazy_static::*;
use log::*;
use mio::*;
use std::collections::BTreeMap;
use std::fmt;
use std::io::prelude::*;
use std::net::{Ipv4Addr, SocketAddr};
use std::time::Duration;

lazy_static! {
    /// The global update channel.
    static ref CHAN: (Sender<(Metric, Action)>, Receiver<(Metric, Action)>) = {
        std::thread::spawn(|| if let Err(e) = run_http_server() {
            warn!("HTTP thread failed to start: {}", e);
        });
        crossbeam_channel::unbounded()
    };
}

/// A named metric;  it has a associated global mutable `f64` value.
///
/// You can create these by hand, but you might find it more convenient to
/// use the `metric!()` macro.
pub struct Metric {
    pub name: &'static str,
    pub labels: Labels,
}
type Labels = Vec<(&'static str, Box<dyn fmt::Display + Send>)>;
enum Action {
    Inc(f64),
    Set(f64),
}

impl Metric {
    // If the HTTP thread fails to start (eg. because the port is in use)
    // then the Receiver will be dropped and the send() calls below will
    // return errors.  This is fine, so we ignore it.

    /// Set the metric to the specified value.
    #[inline]
    pub fn set(self, x: f64) {
        let _ = CHAN.0.send((self, Action::Set(x)));
    }

    /// Increment the metric by the specified amount.
    #[inline]
    pub fn add(self, x: f64) {
        let _ = CHAN.0.send((self, Action::Inc(x)));
    }
}

/// Refer to a metric.
#[macro_export]
macro_rules! metric{
    ($name:ident) => {
        epimetheus::Metric {
            name: stringify!($name),
            labels: Vec::new(),
        }
    };
    ($name:ident{$($key:ident = $val:expr),*}) => {
        epimetheus::Metric {
            name: stringify!($name),
            labels: vec![$((stringify!($key), Box::new($val))),*],
        }
    };
}

#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct LabelsDisplay(BTreeMap<&'static str, String>);
impl LabelsDisplay {
    fn new(labels: Labels) -> LabelsDisplay {
        let labels = labels
            .into_iter()
            .map(|(k, v)| (k, v.to_string()))
            .collect::<BTreeMap<_, _>>();
        LabelsDisplay(labels)
    }
}
impl fmt::Display for LabelsDisplay {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut labels = self.0.iter();
        if let Some(head) = labels.next() {
            f.write_str("{")?;
            f.write_str(head.0)?;
            f.write_str("=")?;
            fmt::Debug::fmt(head.1, f)?;
            for (k, v) in labels {
                f.write_str(",")?;
                f.write_str(k)?;
                f.write_str("=")?;
                fmt::Debug::fmt(v, f)?;
            }
            f.write_str("}")?;
        }
        Ok(())
    }
}

fn run_http_server() -> Result<(), std::io::Error> {
    // This thread stores the current value of all the metrics in a map.
    // (We use a BTreeMap so that we can render the metrics in lexicographic
    // order.)  The labels must be sorted.
    let mut metrics = BTreeMap::<(&'static str, LabelsDisplay), f64>::default();

    // We're going to bind a socket and listen for incoming connections from
    // HTTP clients.  When we get a connection, we'll:
    //
    // 1. drain any updates from the channel and apply them to the local
    //    metrics map; and then
    // 2. render the map to prometheus exposition format and send it to
    //    the client.
    //
    // However, we want to ensure that the channel is regularly drained, even
    // when there are no new connections coming in.  (Otherwise, we'd have
    // a memory leak and - worse - the metric update latency would suffer.)
    // Therefore we set a timeout while we wait, and drain the channel when
    // someone connects or a timeout occurs.
    let port = std::env::var("RUST_METRICS_PORT")
        .ok()
        .and_then(|x| x.parse::<u16>().ok())
        .unwrap_or(9898);
    let sock = net::TcpListener::bind(&SocketAddr::from((Ipv4Addr::LOCALHOST, port)))?;
    let poll = Poll::new()?;
    poll.register(&sock, Token(0), Ready::readable(), PollOpt::edge())?;
    info!("Started HTTP thread");

    let mut events = Events::with_capacity(64);
    let mut buf = [0; 1024];
    loop {
        poll.poll(&mut events, Some(Duration::from_secs(20)))?;
        // No point reading `events` - there's only one thing it could be.
        events.clear();
        for (metric, action) in CHAN.1.try_iter() {
            let labels = LabelsDisplay::new(metric.labels);
            match action {
                Action::Inc(x) => *metrics.entry((metric.name, labels)).or_insert(0.0) += x,
                Action::Set(x) => *metrics.entry((metric.name, labels)).or_insert(0.0) = x,
            }
        }
        while let Ok((mut conn, _)) = sock.accept_std() {
            // This is the world's simplest HTTP implementation.  It totally
            // ignores the request, and sends a response with no headers
            // or anything.  We don't care about the request, but some HTTP
            // clients get upset if you don't at least read it.  Unfortunate.
            let _ = conn.read(&mut buf).unwrap();
            writeln!(conn, "HTTP/1.1 200 OK\r\n").unwrap();
            for ((name, labels), val) in &metrics {
                writeln!(conn, "{}{} {}", name, labels, val).unwrap();
            }
        }
    }
}