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
//! Send metrics to a statsd server.

use core::*;
use error;
use self_metrics::*;

use std::net::UdpSocket;
use std::sync::{Arc, RwLock};

pub use std::net::ToSocketAddrs;

mod_metrics!(Aggregate, STATSD_METRICS = DIPSTICK_METRICS.with_prefix("statsd"));
mod_marker!(Aggregate, STATSD_METRICS, { SEND_ERR: "send_failed" });
mod_counter!(Aggregate, STATSD_METRICS, { SENT_BYTES: "sent_bytes" });

/// Send metrics to a statsd server at the address and port provided.
pub fn to_statsd<ADDR>(address: ADDR) -> error::Result<Chain<Statsd>>
where
    ADDR: ToSocketAddrs,
{
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:0")?);
    socket.set_nonblocking(true)?;
    socket.connect(address)?;

    Ok(Chain::new(
        move |kind, name, rate| {
            let mut prefix = String::with_capacity(32);
            prefix.push_str(name);
            prefix.push(':');

            let mut suffix = String::with_capacity(16);
            suffix.push('|');
            suffix.push_str(match kind {
                Kind::Marker | Kind::Counter => "c",
                Kind::Gauge => "g",
                Kind::Timer => "ms",
            });

            if rate < FULL_SAMPLING_RATE {
                suffix.push_str("|@");
                suffix.push_str(&rate.to_string());
            }

            let scale = match kind {
                // timers are in µs, statsd wants ms
                Kind::Timer => 1000,
                _ => 1,
            };

            Statsd {
                prefix,
                suffix,
                scale,
            }
        },
        move |buffered| {
            let buf = RwLock::new(ScopeBuffer {
                buffer: String::with_capacity(MAX_UDP_PAYLOAD),
                socket: socket.clone(),
                buffered,
            });
            ControlScopeFn::new(move |cmd| {
                if let Ok(mut buf) = buf.write() {
                    match cmd {
                        ScopeCmd::Write(metric, value) => buf.write(metric, value),
                        ScopeCmd::Flush => buf.flush(),
                    }
                }
            })
        },
    ))
}

/// Key of a statsd metric.
#[derive(Debug, Clone)]
pub struct Statsd {
    prefix: String,
    suffix: String,
    scale: u64,
}

/// Use a safe maximum size for UDP to prevent fragmentation.
// TODO make configurable?
const MAX_UDP_PAYLOAD: usize = 576;

/// Wrapped string buffer & socket as one.
#[derive(Debug)]
struct ScopeBuffer {
    buffer: String,
    socket: Arc<UdpSocket>,
    buffered: bool,
}

/// Any remaining buffered data is flushed on Drop.
impl Drop for ScopeBuffer {
    fn drop(&mut self) {
        self.flush()
    }
}

impl ScopeBuffer {
    fn write(&mut self, metric: &Statsd, value: Value) {
        let scaled_value = value / metric.scale;
        let value_str = scaled_value.to_string();
        let entry_len = metric.prefix.len() + value_str.len() + metric.suffix.len();

        if entry_len > self.buffer.capacity() {
            // TODO report entry too big to fit in buffer (!?)
            return;
        }

        let remaining = self.buffer.capacity() - self.buffer.len();
        if entry_len + 1 > remaining {
            // buffer is full, flush before appending
            self.flush();
        } else {
            if !self.buffer.is_empty() {
                // separate from previous entry
                self.buffer.push('\n')
            }
            self.buffer.push_str(&metric.prefix);
            self.buffer.push_str(&value_str);
            self.buffer.push_str(&metric.suffix);
        }
        if self.buffered {
            self.flush();
        }
    }

    fn flush(&mut self) {
        if !self.buffer.is_empty() {
            match self.socket.send(self.buffer.as_bytes()) {
                Ok(size) => {
                    SENT_BYTES.count(size);
                    trace!("Sent {} bytes to statsd", self.buffer.len());
                }
                Err(e) => {
                    SEND_ERR.mark();
                    debug!("Failed to send packet to statsd: {}", e);
                }
            };
            self.buffer.clear();
        }
    }
}

#[cfg(feature = "bench")]
mod bench {

    use super::*;
    use test;

    #[bench]
    pub fn timer_statsd(b: &mut test::Bencher) {
        let sd = to_statsd("localhost:8125").unwrap();
        let timer = sd.define_metric(Kind::Timer, "timer", 1000000.0);
        let scope = sd.open_scope(false);

        b.iter(|| test::black_box(scope.write(&timer, 2000)));
    }

}