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
use std::str;
use std::thread;
use std::sync::Mutex;
use std::time::Duration;
use std::fmt::Arguments;
use std::net::{UdpSocket,SocketAddr};
use std::io::{self,Write,Error,ErrorKind};
use network::buffer::Buffer;
lazy_static! {
pub static ref METRICS: Mutex<ProxyMetrics> = Mutex::new(ProxyMetrics::new(String::from("sozu")));
}
pub struct ProxyMetrics {
pub buffer: Buffer,
pub prefix: String,
remote: Option<(SocketAddr, UdpSocket)>,
}
impl ProxyMetrics {
pub fn new(prefix: String) -> Self {
ProxyMetrics {
buffer: Buffer::with_capacity(2048),
prefix: prefix,
remote: None,
}
}
pub fn run() -> thread::JoinHandle<()> {
thread::spawn(move || {
loop {
thread::sleep(Duration::from_millis(500));
METRICS.lock().unwrap().send();
}
})
}
pub fn set_up_remote(&mut self, socket: UdpSocket, addr: SocketAddr) {
self.remote = Some((addr, socket));
}
pub fn write(&mut self, args: Arguments) {
self.buffer.write_fmt(args);
self.send();
}
pub fn send(&mut self) -> io::Result<usize> {
if self.buffer.available_data() >= 512 {
if let Some((ref addr, ref socket)) = self.remote {
match socket.send_to(self.buffer.data(), addr) {
Ok(sz) => {
self.buffer.consume(sz);
Ok(sz)
},
Err(e) => {
Err(e)
}
}
} else {
Err(Error::new(ErrorKind::NotConnected, "metrics socket not set up"))
}
} else {
Err(Error::new(ErrorKind::Other, "no data to send"))
}
}
fn emit(&mut self, metric: &str) -> io::Result<usize> {
self.buffer.write(metric.as_bytes())
}
pub fn count(&mut self, key: &str, count: i64) -> io::Result<()> {
self.buffer.write_fmt(format_args!("{}.{}:{}|c\n", &self.prefix, key, count))
}
pub fn incr(&mut self, key: &str) -> io::Result<()> {
self.count(key, 1)
}
pub fn decr(&mut self, key: &str) -> io::Result<()> {
self.count(key, -1)
}
pub fn time(&mut self, key: &str, time: u64) -> io::Result<()> {
self.buffer.write_fmt(format_args!("{}.{}:{}|ms\n", self.prefix, key, time))
}
pub fn gauge(&mut self, key: &str, value: u64) -> io::Result<()> {
self.buffer.write_fmt(format_args!("{}.{}:{}|g\n", self.prefix, key, value))
}
pub fn meter(&mut self, key: &str, value: u64) -> io::Result<()> {
self.buffer.write_fmt(format_args!("{}.{}:{}|m\n", self.prefix, key, value))
}
}
#[macro_export]
macro_rules! count (
($key:expr, $value: expr) => {
let mut metrics = ::network::metrics::METRICS.lock().unwrap();
metrics.write(format_args!("{}.{}:{}|c\n", *$crate::logging::TAG, $key, $value));
}
);
#[macro_export]
macro_rules! incr (
($key:expr) => (count!($key, 1);)
);
#[macro_export]
macro_rules! decr (
($key:expr) => (count!($key, -1);)
);
#[macro_export]
macro_rules! time (
($key:expr, $value: expr) => {
let mut metrics = ::network::metrics::METRICS.lock().unwrap();
metrics.write(format_args!("{}.{}:{}|ms\n", *$crate::logging::TAG, $key, $value));
}
);
#[macro_export]
macro_rules! gauge (
($key:expr, $value: expr) => {
let mut metrics = ::network::metrics::METRICS.lock().unwrap();
metrics.write(format_args!("{}.{}:{}|g\n", *$crate::logging::TAG, $key, $value));
}
);
#[macro_export]
macro_rules! meter (
($key:expr, $value: expr) => {
let mut metrics = ::network::metrics::METRICS.lock().unwrap();
metrics.write(format_args!("{}.{}:{}|m\n", *$crate::logging::TAG, $key, $value));
}
);