Skip to content

Commit

Permalink
Merge pull request #29 from jnth/feature/handle_kv_metric
Browse files Browse the repository at this point in the history
Handle key/value metric.
  • Loading branch information
markstory authored Jun 15, 2023
2 parents 4145487 + 2bcde15 commit 1094ea6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
44 changes: 44 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1094ea6

Please sign in to comment.