Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from Overmuse/SR/api_cleanup
Browse files Browse the repository at this point in the history
nicer api
  • Loading branch information
SebRollen authored Jun 17, 2021
2 parents c3b77e7 + b6d8ba1 commit e5521de
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 78 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 = "dogstatsd"
version = "0.1.0"
version = "0.2.0"
authors = ["Sebastian Rollen <[email protected]>"]
edition = "2018"

Expand Down
11 changes: 3 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::metric::Metric;
use crate::Metric;
use std::io::Error;
use tokio::net::{ToSocketAddrs, UdpSocket};

Expand All @@ -16,11 +16,7 @@ impl Client {
Ok(Self { socket })
}

pub async fn send<I, T>(&self, metric: Metric<'_, I, T>) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
{
pub async fn send(&self, metric: Metric<'_>) -> Result<(), Error> {
let bytes = metric.into_bytes();
self.socket.send(&bytes).await?;
Ok(())
Expand All @@ -34,11 +30,10 @@ mod test {
#[tokio::test]
async fn test_client() {
let udp_receiver = UdpSocket::bind("127.0.0.1:8125").await.unwrap();
let v: &[&str] = &[];
let client = Client::new("127.0.0.1:1234", "127.0.0.1:8125")
.await
.unwrap();
client.send(Metric::increase("test", v)).await.unwrap();
client.send(Metric::increase("test")).await.unwrap();
udp_receiver.connect("127.0.0.1:1234").await.unwrap();
let mut bytes_received: usize = 0;
let mut buf = [0; 8];
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod client;
mod metric;
mod tag;
pub use client::Client;
pub use metric::Metric;
pub use tag::Tag;
118 changes: 49 additions & 69 deletions src/metric.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::Tag;
use bytes::{Bytes, BytesMut};

enum Type<'a> {
Expand All @@ -10,75 +11,52 @@ enum Type<'a> {
Set(&'a str),
}

pub struct Metric<'a, I, T>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
{
pub struct Metric<'a> {
frame_type: Type<'a>,
message: &'a str,
tags: I,
tags: Vec<Tag<'a>>,
}

impl<'a, I, T> Metric<'a, I, T>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
{
pub fn increase(message: &'a str, tags: I) -> Self {
impl<'a> Metric<'a> {
fn new(frame_type: Type<'a>, message: &'a str) -> Self {
Self {
frame_type: Type::Increase,
frame_type,
message,
tags,
tags: vec![],
}
}

pub fn decrease(message: &'a str, tags: I) -> Self {
Self {
frame_type: Type::Decrease,
message,
tags,
}
pub fn increase(message: &'a str) -> Self {
Self::new(Type::Increase, message)
}

pub fn count(count: isize, message: &'a str, tags: I) -> Self {
Self {
frame_type: Type::Count(count),
message,
tags,
}
pub fn decrease(message: &'a str) -> Self {
Self::new(Type::Decrease, message)
}

pub fn gauge(value: &'a str, message: &'a str, tags: I) -> Self {
Self {
frame_type: Type::Gauge(value),
message,
tags,
}
pub fn count(count: isize, message: &'a str) -> Self {
Self::new(Type::Count(count), message)
}

pub fn histogram(value: &'a str, message: &'a str, tags: I) -> Self {
Self {
frame_type: Type::Histogram(value),
message,
tags,
}
pub fn gauge(value: &'a str, message: &'a str) -> Self {
Self::new(Type::Gauge(value), message)
}

pub fn distribution(value: &'a str, message: &'a str, tags: I) -> Self {
Self {
frame_type: Type::Distribution(value),
message,
tags,
}
pub fn histogram(value: &'a str, message: &'a str) -> Self {
Self::new(Type::Histogram(value), message)
}

pub fn set(value: &'a str, message: &'a str, tags: I) -> Self {
Self {
frame_type: Type::Set(value),
message,
tags,
}
pub fn distribution(value: &'a str, message: &'a str) -> Self {
Self::new(Type::Distribution(value), message)
}

pub fn set(value: &'a str, message: &'a str) -> Self {
Self::new(Type::Set(value), message)
}

pub fn add_tag<T: Into<Tag<'a>>>(mut self, tag: T) -> Self {
self.tags.push(tag.into());
self
}

pub(crate) fn into_bytes(self) -> Bytes {
Expand Down Expand Up @@ -138,7 +116,17 @@ where
}

while next_tag.is_some() {
buf.extend_from_slice(next_tag.unwrap().as_ref().as_bytes());
let tag = next_tag.unwrap();
match tag {
Tag::Single(value) => {
buf.extend_from_slice(value.as_bytes());
}
Tag::KeyValue(key, value) => {
buf.extend_from_slice(key.as_bytes());
buf.extend_from_slice(b":");
buf.extend_from_slice(value.as_bytes());
}
}
next_tag = tags_iter.next();

if next_tag.is_some() {
Expand All @@ -156,37 +144,29 @@ mod test {

#[test]
fn test_into_bytes() {
let v: &[&str] = &[];
assert_eq!(
Metric::increase("test", v).into_bytes().as_ref(),
b"test:1|c"
);
assert_eq!(
Metric::decrease("test", v).into_bytes().as_ref(),
b"test:-1|c"
);
assert_eq!(
Metric::count(2, "test", v).into_bytes().as_ref(),
b"test:2|c"
);
assert_eq!(Metric::increase("test").into_bytes().as_ref(), b"test:1|c");
assert_eq!(Metric::decrease("test").into_bytes().as_ref(), b"test:-1|c");
assert_eq!(Metric::count(2, "test").into_bytes().as_ref(), b"test:2|c");
assert_eq!(
Metric::gauge("1.2", "test", v).into_bytes().as_ref(),
Metric::gauge("1.2", "test").into_bytes().as_ref(),
b"test:1.2|g"
);
assert_eq!(
Metric::histogram("1.2", "test", v).into_bytes().as_ref(),
Metric::histogram("1.2", "test").into_bytes().as_ref(),
b"test:1.2|h"
);
assert_eq!(
Metric::distribution("1.2", "test", v).into_bytes().as_ref(),
Metric::distribution("1.2", "test").into_bytes().as_ref(),
b"test:1.2|d"
);
assert_eq!(
Metric::set("1.2", "test", v).into_bytes().as_ref(),
Metric::set("1.2", "test").into_bytes().as_ref(),
b"test:1.2|s"
);
assert_eq!(
Metric::increase("test", &["a:b", "c"])
Metric::increase("test")
.add_tag(("a", "b"))
.add_tag("c")
.into_bytes()
.as_ref(),
b"test:1|c|#a:b,c"
Expand Down
32 changes: 32 additions & 0 deletions src/tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::borrow::Cow;

pub enum Tag<'a> {
Single(Cow<'a, str>),
KeyValue(Cow<'a, str>, Cow<'a, str>),
}

impl<'a> From<&'a str> for Tag<'a> {
fn from(s: &'a str) -> Tag<'a> {
Tag::Single(Cow::Borrowed(s))
}
}

impl<'a> From<(&'a str, &'a str)> for Tag<'a> {
fn from(s: (&'a str, &'a str)) -> Tag<'a> {
Tag::KeyValue(Cow::Borrowed(s.0), Cow::Borrowed(s.1))
}
}

impl<'a> From<Tag<'a>> for Cow<'a, str> {
fn from(tag: Tag<'a>) -> Cow<'a, str> {
match tag {
Tag::Single(single) => single,
Tag::KeyValue(key, value) => {
let mut out = key.to_string();
out.push(':');
out.push_str(value.as_ref());
Cow::Owned(out)
}
}
}
}

0 comments on commit e5521de

Please sign in to comment.