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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Cadence - An extensible Statsd client for Rust!
//
// Copyright 2015 TSH Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! An extensible Statsd client for Rust!
//!
//! [Statsd](https://github.com/etsy/statsd) is a network server that listens for
//! metrics (things like counters and timers) sent over UDP and sends aggregates of
//! these metrics to a backend service of some kind (often
//! [Graphite](http://graphite.readthedocs.org/)).
//!
//! Cadence is a client written in Rust for interacting with a Statsd server. You
//! might want to emit metrics (using Cadence, sending them to a Statsd server) in
//! your Rust server application.
//!
//! For example, if you are running a Rust web service you might want to record:
//!
//! * Number of succesful requests
//! * Number of error requests
//! * Time taken for each request
//!
//! Cadence is a flexible and easy way to do this!
//!
//! ## Features
//!
//! * Support for emitting counters, timers, gauges, and meters to Statsd over UDP.
//! * Support for alternate backends via the `MetricSink` trait.
//! * A simple yet flexible API for sending metrics.
//!
//! ## Install
//!
//! To make use of Cadence in your project, add it as a dependency in your `Cargo.toml`
//! file.
//!
//! ``` toml
//! [dependencies]
//! cadence = "x.y.z"
//! ```
//!
//! Then, link to it in your library or application.
//!
//! ``` rust,no_run
//! // bin.rs or lib.rs
//! extern crate cadence;
//!
//! // rest of your library or application
//! ```
//!
//! ## Usage
//!
//! Some examples of how to use Cadence are shown below.
//!
//! ### Simple Use
//!
//! Simple usage of Cadence is shown below. In this example, we just import the client,
//! create an instance that will write to some imaginary metrics server, and send a few
//! metrics.
//!
//! ``` rust,no_run
//! // Import the client.
//! //
//! // Note that we're also importing each of the traits that the client uses to emit
//! // metircs (Counted, Timed, Gauged, and Metered).
//! use cadence::{
//! Counted,
//! Timed,
//! Gauged,
//! Metered,
//! StatsdClient,
//! UdpMetricSink,
//! DEFAULT_PORT
//! };
//!
//!
//! fn main() {
//! // Create client that will write to the given host over UDP.
//! //
//! // Note that you'll probably want to actually handle any errors creating the client
//! // when you use it for real in your application. We're just using .unwrap() here
//! // since this is an example!
//! let host = ("metrics.example.com", DEFAULT_PORT);
//! let client = StatsdClient::<UdpMetricSink>::from_udp_host(
//! "my.metrics", host).unwrap();
//!
//! // Emit metrics!
//! client.incr("some.counter");
//! client.time("some.methodCall", 42);
//! client.gauge("some.thing", 7);
//! client.meter("some.value", 5);
//! }
//! ```
//!
//! ### Counted, Timed, Gauged, and Metered Traits
//!
//! Each of the methods that the Cadence `StatsdClient` struct uses to send metrics are
//! implemented as a trait. If we want, we can just use the trait type to refer to the
//! client instance. This might be useful to you if you'd like to swap out the actual
//! Cadence client with a dummy version when you are unit testing your code.
//!
//! ``` rust,no_run
//! use cadence::{
//! Counted,
//! StatsdClient,
//! UdpMetricSink,
//! DEFAULT_PORT
//! };
//!
//!
//! pub struct User {
//! id: u64,
//! username: String,
//! email: String
//! }
//!
//!
//! // Here's a simple DAO (Data Access Object) that doesn't do anything but
//! // uses a counter to keep track of the number of times the 'getUserById'
//! // method gets called.
//! pub struct MyUserDao<T: Counted> {
//! counter: T
//! }
//!
//!
//! impl<T: Counted> MyUserDao<T> {
//! // Create a new instance that will use the counter / client
//! pub fn new(counter: T) -> MyUserDao<T> {
//! MyUserDao{counter: counter}
//! }
//!
//! /// Get a new user by their ID
//! pub fn get_user_by_id(&self, id: u64) -> Option<User> {
//! self.counter.incr("getUserById");
//! None
//! }
//! }
//!
//!
//! fn main() {
//! // Create a new Statsd client that writes to "metrics.example.com"
//! let host = ("metrics.example.com", DEFAULT_PORT);
//! let counter = StatsdClient::<UdpMetricSink>::from_udp_host(
//! "counter.example", host).unwrap();
//!
//! // Create a new instance of the DAO that will use the client
//! let dao = MyUserDao::new(counter);
//!
//! // Try to lookup a user by ID!
//! match dao.get_user_by_id(123) {
//! Some(u) => println!("Found a user!"),
//! None => println!("No user!")
//! };
//! }
//! ```
//!
//! ### Custom Metric Sinks
//!
//! The Cadence `StatsdClient` uses implementations of the `MetricSink` trait to
//! send metrics to a metric server. Most users of the Candence library probably
//! want to use the `UdpMetricSink` implementation. This is the way people typically
//! interact with a Statsd server, sending packets over UDP.
//!
//! However, maybe you'd like to do something custom: use a thread pool, send multiple
//! metrics at the same time, or something else. An example of creating a custom sink
//! is below.
//!
//! ``` rust,no_run
//! use std::io;
//! use cadence::{
//! Counted,
//! Timed,
//! Gauged,
//! Metered,
//! StatsdClient,
//! MetricSink,
//! DEFAULT_PORT
//! };
//!
//! pub struct MyMetricSink;
//!
//!
//! impl MetricSink for MyMetricSink {
//! fn emit(&self, metric: &str) -> io::Result<usize> {
//! // Your custom metric sink implementation goes here!
//! Ok(0)
//! }
//! }
//!
//!
//! fn main() {
//! let sink = MyMetricSink;
//! let client = StatsdClient::from_sink("my.prefix", sink);
//!
//! client.count("my.counter.thing", 42);
//! client.time("my.method.time", 25);
//! client.incr("some.other.counter");
//! }
//! ```
//!
extern crate log;
pub const DEFAULT_PORT: u16 = 8125;
pub use ;
pub use ;
pub use ;