Skip to content

Commit

Permalink
Merge pull request #47 from cottinisimone/change-time-functions-retur…
Browse files Browse the repository at this point in the history
…n-type

Change return type for time function
  • Loading branch information
mcasper authored May 3, 2023
2 parents 2cbd16d + 67e8d22 commit 49d0ff0
Showing 1 changed file with 33 additions and 21 deletions.
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

0 comments on commit 49d0ff0

Please sign in to comment.