diff --git a/Cargo.toml b/Cargo.toml index 525a1b1..9b1592e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "statsd" -version = "0.15.0" +version = "0.16.0" description = "A basic statsd client for rust." homepage = "https://github.com/markstory/rust-statsd" readme = "README.md" diff --git a/README.md b/README.md index e86e1b3..066979d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Add the `statsd` package as a dependency in your `Cargo.toml` file: ```toml [dependencies] -statsd = "^0.13.1" +statsd = "^0.16" ``` You need rustc >= 1.31.0 for statsd to work. @@ -48,6 +48,9 @@ client.count("some.counter", 511.0); // Send a histogram value as a float. client.histogram("some.histogram", 511.0); + +// Send a key/value. +client.kv("some.data", 15.26); ``` ### Tracking Timers @@ -86,6 +89,9 @@ pipe.count("some.counter", 511.0); // Send a histogram value as a float. pipe.histogram("some.histogram", 511.0); +// Send a key/value. +pipe.kv("some.data", 15.26); + // Set max UDP packet size if you wish, default is 512 pipe.set_max_udp_size(128); diff --git a/src/client.rs b/src/client.rs index 0ce7366..f4a8633 100644 --- a/src/client.rs +++ b/src/client.rs @@ -219,6 +219,16 @@ impl Client { let data = self.prepare(format!("{}:{}|h", metric, value)); self.send(data); } + + /// Send a key/value + /// + /// ```ignore + /// client.kv("key", 1.); + /// ``` + pub fn kv(&self, metric: &str, value: f64) { + let data = self.prepare(format!("{}:{}|kv", metric, value)); + self.send(data); + } } pub struct Pipeline { @@ -384,6 +394,20 @@ impl Pipeline { self.stats.push_back(data); } + /// Send a key/value. + /// + /// ``` + /// use statsd::client::Pipeline; + /// + /// let mut pipe = Pipeline::new(); + /// // pass response size value + /// pipe.kv("response.size", 256.); + /// ``` + pub fn kv(&mut self, metric: &str, value: f64) { + let data = format!("{}:{}|kv", metric, value); + self.stats.push_back(data); + } + /// Send data along the UDP socket. pub fn send(&mut self, client: &Client) { let mut _data = String::new(); @@ -566,6 +590,14 @@ mod test { assert_eq!("myapp.metric:9.1|h", response); } + #[test] + fn test_sending_kv() { + let server = Server::new(); + let client = Client::new(server.addr(), "myapp").unwrap(); + let response = server.run_while_receiving(|| client.kv("metric", 15.26)); + assert_eq!("myapp.metric:15.26|kv", response); + } + #[test] fn test_pipeline_sending_time_block() { let server = Server::new(); @@ -608,6 +640,18 @@ mod test { assert_eq!("myapp.metric:9.1|h", response); } + #[test] + fn test_pipeline_sending_kv() { + let server = Server::new(); + let client = Client::new(server.addr(), "myapp").unwrap(); + let response = server.run_while_receiving(|| { + let mut pipeline = client.pipeline(); + pipeline.kv("metric", 15.26); + pipeline.send(&client); + }); + assert_eq!("myapp.metric:15.26|kv", response); + } + #[test] fn test_pipeline_sending_multiple_data() { let server = Server::new();