pub struct Duration { /* private fields */ }Expand description
A wrapper around time::Duration that is used to deserialize
responses or verify time durations before sending them to Prometheus.
use prometheus_http_query::{Duration, Client};
use std::str::FromStr;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let q = "prometheus_http_requests_total";
let d = Duration::from_str("1m")?;
// Use parsed duration by converting to `f64`.
let response = client.query_range(q, 1648373100, 1648373300, d.into()).get().await?;
assert!(response.data().as_matrix().is_some());
// Durations converted from numeric types can also be printed back to
// a Prometheus duration string.
let d = Duration::from(50.312);
assert_eq!(&d.to_string(), "50s312ms");
// However durations that were parsed from a string retain their original
// string representation, even when they could be expressed more succinctly.
// For example this could also be "2m".
let d = Duration::from_str("120s")?;
assert_eq!(&d.to_string(), "120s");
Ok(())
}Trait Implementations§
impl Eq for Duration
Source§impl From<f64> for Duration
Convert fractional seconds to Duration.
impl From<f64> for Duration
Convert fractional seconds to Duration.
use prometheus_http_query::Duration;
use std::str::FromStr;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
{
let a = Duration::from_str("1s")?;
let b = Duration::from(1.0);
assert_eq!(a, b);
}
{
let a = Duration::from_str("312ms")?;
let b = Duration::from(0.312);
assert_eq!(a, b);
}
Ok(())
}Source§impl From<i64> for Duration
Convert seconds to Duration.
impl From<i64> for Duration
Convert seconds to Duration.
use prometheus_http_query::Duration;
use std::str::FromStr;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
{
let a = Duration::from_str("2m")?;
let b = Duration::from(120);
assert_eq!(a, b);
}
{
let a = Duration::from_str("1h")?;
let b = Duration::from(3600);
assert_eq!(a, b);
}
Ok(())
}impl StructuralPartialEq for Duration
Auto Trait Implementations§
impl Forget for Duration
impl Freeze for Duration
impl Move for Duration
impl RefUnwindSafe for Duration
impl Send for Durationwhere
RawVecInner: Send,
impl Sync for Durationwhere
RawVecInner: Sync,
impl Unpin for Duration
impl UnsafeUnpin for Duration
impl UnwindSafe for Durationwhere
RawVecInner: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more