Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change return type for time function #47

Merged
merged 1 commit into from
May 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@
)]
extern crate chrono;

use chrono::Utc;
use std::borrow::Cow;
use std::future::Future;
use std::net::UdpSocket;

mod error;
pub use self::error::DogstatsdError;
use chrono::Utc;

mod metrics;
pub use self::error::DogstatsdError;
use self::metrics::*;

pub use self::metrics::{ServiceCheckOptions, ServiceStatus};

mod error;
mod metrics;

/// A type alias for returning a unit type or an error
pub type DogstatsdResult = Result<(), DogstatsdError>;

Expand Down Expand Up @@ -394,7 +394,12 @@ impl Client {
/// thread::sleep(Duration::from_millis(200))
/// }).unwrap_or_else(|e| println!("Encountered error: {}", e))
/// ```
pub fn time<'a, F, O, I, S, T>(&self, stat: S, tags: I, block: F) -> Result<O, DogstatsdError>
pub fn time<'a, F, O, I, S, T>(
&self,
stat: S,
tags: I,
block: F,
) -> Result<O, (O, DogstatsdError)>
where
F: FnOnce() -> O,
I: IntoIterator<Item = T>,
Expand All @@ -404,11 +409,12 @@ impl Client {
let start_time = Utc::now();
let output = block();
let end_time = Utc::now();
self.send(
&TimeMetric::new(stat.into().as_ref(), &start_time, &end_time),
tags,
)?;
Ok(output)
let stat = stat.into();
let metric = TimeMetric::new(stat.as_ref(), &start_time, &end_time);
match self.send(&metric, tags) {
Ok(()) => Ok(output),
Err(error) => Err((output, error)),
}
}

/// Time how long it takes for an async block of code to execute.
Expand All @@ -428,27 +434,30 @@ impl Client {
/// .unwrap_or_else(|e| println!("Encountered error: {}", e))
/// }
/// ```
pub async fn async_time<'a, Fn, Fut, Out, I, S, T>(
pub async fn async_time<'a, Fn, Fut, O, I, S, T>(
&self,
stat: S,
tags: I,
block: Fn,
) -> Result<Out, DogstatsdError>
) -> Result<O, (O, DogstatsdError)>
where
Fn: FnOnce() -> Fut,
Fut: Future<Output = Out>,
Fut: Future<Output = O>,
I: IntoIterator<Item = T>,
S: Into<Cow<'a, str>>,
T: AsRef<str>,
{
let start_time = Utc::now();
let result: Out = block().await;
let output = block().await;
let end_time = Utc::now();
self.send(
&TimeMetric::new(stat.into().as_ref(), &start_time, &end_time),
let stat = stat.into();
match self.send(
&TimeMetric::new(stat.as_ref(), &start_time, &end_time),
tags,
)?;
Ok(result)
) {
Ok(()) => Ok(output),
Err(error) => Err((output, error)),
}
}

/// Send your own timing metric in milliseconds
Expand Down Expand Up @@ -651,6 +660,8 @@ impl Client {

#[cfg(test)]
mod tests {
use metrics::GaugeMetric;

use super::*;

#[test]
Expand Down Expand Up @@ -731,7 +742,6 @@ mod tests {
assert_eq!(expected_client, client)
}

use metrics::GaugeMetric;
#[test]
fn test_send() {
let options = Options::new("127.0.0.1:9001", "127.0.0.1:9002", "", vec![]);
Expand All @@ -749,9 +759,11 @@ mod tests {
#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;
use self::test::Bencher;

use super::*;

use self::test::Bencher;

#[bench]
fn bench_incr(b: &mut Bencher) {
let options = Options::default();
Expand Down