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
use crate::io::MultiLineWriter;
use crate::sinks::core::MetricSink;
use crossbeam_channel::{bounded, unbounded, Receiver, Sender, TrySendError};
use std::io::{self, ErrorKind, Write};
use std::sync::Mutex;
const DEFAULT_BUFFER_SIZE: usize = 512;
#[derive(Debug)]
pub struct SpyMetricSink {
sender: Sender<Vec<u8>>,
}
impl SpyMetricSink {
pub fn new() -> (Receiver<Vec<u8>>, Self) {
Self::with_queue_capacity(None)
}
pub fn with_capacity(queue: usize) -> (Receiver<Vec<u8>>, Self) {
Self::with_queue_capacity(Some(queue))
}
fn with_queue_capacity(queue: Option<usize>) -> (Receiver<Vec<u8>>, Self) {
let (tx, rx) = new_channel(queue);
let sink = SpyMetricSink { sender: tx };
(rx, sink)
}
}
impl MetricSink for SpyMetricSink {
fn emit(&self, metric: &str) -> io::Result<usize> {
send_metric(&self.sender, metric.as_bytes())
}
}
#[derive(Debug)]
pub struct BufferedSpyMetricSink {
writer: Mutex<MultiLineWriter<WriteAdapter>>,
}
impl BufferedSpyMetricSink {
pub fn new() -> (Receiver<Vec<u8>>, Self) {
Self::with_capacity(None, Some(DEFAULT_BUFFER_SIZE))
}
pub fn with_capacity(queue: Option<usize>, buffer: Option<usize>) -> (Receiver<Vec<u8>>, Self) {
let (tx, rx) = new_channel(queue);
let buffer_sz = buffer.unwrap_or(DEFAULT_BUFFER_SIZE);
let writer = MultiLineWriter::new(WriteAdapter::new(tx), buffer_sz);
let sink = BufferedSpyMetricSink {
writer: Mutex::new(writer),
};
(rx, sink)
}
}
impl MetricSink for BufferedSpyMetricSink {
fn emit(&self, metric: &str) -> io::Result<usize> {
let mut writer = self.writer.lock().unwrap();
writer.write(metric.as_bytes())
}
fn flush(&self) -> io::Result<()> {
let mut writer = self.writer.lock().unwrap();
writer.flush()
}
}
#[derive(Debug)]
struct WriteAdapter {
sender: Sender<Vec<u8>>,
}
impl WriteAdapter {
fn new(sender: Sender<Vec<u8>>) -> Self {
WriteAdapter { sender }
}
}
impl Write for WriteAdapter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
send_metric(&self.sender, buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
fn new_channel(cap: Option<usize>) -> (Sender<Vec<u8>>, Receiver<Vec<u8>>) {
if let Some(sz) = cap {
bounded(sz)
} else {
unbounded()
}
}
fn send_metric(sender: &Sender<Vec<u8>>, metric: &[u8]) -> io::Result<usize> {
match sender.try_send(metric.to_vec()) {
Err(TrySendError::Disconnected(_)) => Err(io::Error::new(ErrorKind::Other, "channel disconnected")),
Err(TrySendError::Full(_)) => Err(io::Error::new(ErrorKind::Other, "channel full")),
Ok(_) => Ok(metric.len()),
}
}
#[cfg(test)]
mod test {
use super::{BufferedSpyMetricSink, MetricSink, SpyMetricSink};
#[test]
fn test_spy_metric_sink() {
let (rx, sink) = SpyMetricSink::new();
sink.emit("buz:1|c").unwrap();
let sent = rx.recv().unwrap();
assert_eq!("buz:1|c".as_bytes(), sent.as_slice());
}
#[test]
fn test_buffered_spy_metric_sink() {
let rx = {
let (rx, sink) = BufferedSpyMetricSink::with_capacity(None, Some(64));
sink.emit("foo:54|c").unwrap();
sink.emit("foo:67|c").unwrap();
rx
};
let sent = rx.recv().unwrap();
assert_eq!("foo:54|c\nfoo:67|c\n".as_bytes(), sent.as_slice());
}
#[test]
fn test_buffered_spy_metric_sink_flush() {
let (rx, sink) = BufferedSpyMetricSink::with_capacity(None, Some(64));
sink.emit("foo:54|c").unwrap();
sink.emit("foo:67|c").unwrap();
assert!(rx.is_empty());
let flush = sink.flush();
let sent = rx.recv().unwrap();
assert_eq!("foo:54|c\nfoo:67|c\n".as_bytes(), sent.as_slice());
assert!(flush.is_ok());
}
}