Skip to content

Commit

Permalink
feat: metrics NG
Browse files Browse the repository at this point in the history
  • Loading branch information
xDarksome committed Jul 1, 2024
1 parent 3d85923 commit 62cfec1
Show file tree
Hide file tree
Showing 19 changed files with 1,560 additions and 902 deletions.
589 changes: 448 additions & 141 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ collections = ["dep:collections"]
future = ["dep:future"]
geoblock = ["geoip/middleware"]
geoip = ["dep:geoip"]
metrics = ["dep:metrics", "future/metrics", "alloc/metrics"]
future_metrics = ["dep:future_metrics"]
metrics = ["dep:metrics", "alloc/metrics"]
alloc_metrics = ["alloc/metrics"]
profiler = ["alloc/profiler"]
rate_limit = ["dep:rate_limit"]
Expand All @@ -46,7 +45,6 @@ collections = { path = "./crates/collections", optional = true }
future = { path = "./crates/future", optional = true }
geoip = { path = "./crates/geoip", optional = true }
metrics = { path = "./crates/metrics", optional = true }
future_metrics = { path = "./crates/future_metrics", optional = true }
rate_limit = { path = "./crates/rate_limit", optional = true }

[dev-dependencies]
Expand All @@ -65,10 +63,6 @@ required-features = ["alloc", "profiler"]
name = "alloc_stats"
required-features = ["alloc", "metrics"]

[[example]]
name = "metrics"
required-features = ["metrics", "future"]

[[example]]
name = "geoblock"
required-features = ["geoblock"]
2 changes: 1 addition & 1 deletion crates/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ profiler = ["dep:dhat", "dep:tokio"]
metrics = ["dep:metrics"]

[dependencies]
metrics = { version = "0.23", optional = true }
metrics = { path = "../metrics", optional = true }
tikv-jemallocator = { version = "0.5", features = ["stats"] }
tikv-jemalloc-ctl = { version = "0.5", features = ["use_std"] }
serde = { version = "1", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/alloc/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn collect_jemalloc_stats() -> Result<JemallocStats, Error> {

#[cfg(feature = "metrics")]
pub fn update_jemalloc_metrics() -> Result<(), Error> {
use metrics::gauge;
use metrics::backend::gauge;

let stats = collect_jemalloc_stats()?;
let total = &stats.total;
Expand Down
3 changes: 0 additions & 3 deletions crates/future/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ edition = "2021"

[features]
default = []
full = ["metrics"]
metrics = ["dep:metrics"]

[dependencies]
metrics = { path = "../metrics", optional = true }
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "time", "macros"] }
tokio-util = { version = "0.7", default-features = false }
pin-project = "1"
Expand Down
67 changes: 8 additions & 59 deletions crates/future/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,6 @@ pub trait FutureExt {
self,
token: CancellationToken,
) -> CancellationFuture<Self::Future, Ready<()>>;

/// Consumes the future, returning a new future that records the metrics of
/// the inner future's async task execution.
///
/// # Example
///
/// ```rust
/// use {future::FutureExt, metrics::OtelTaskMetricsRecorder, std::time::Duration};
///
/// # async fn example() {
/// let recorder = OtelTaskMetricsRecorder::new("custom_task").with_name("specific_task_name");
///
/// async {
/// tokio::time::sleep(Duration::from_millis(500)).await;
/// }
/// .with_metrics(recorder)
/// .await
/// # }
///
/// # #[tokio::main]
/// # async fn main() {
/// # example().await;
/// # }
/// ```
#[cfg(feature = "metrics")]
fn with_metrics<R>(self, recorder: R) -> metrics::TaskMetricsFuture<Self::Future, R>
where
R: metrics::TaskMetricsRecorder;
}

pub trait StaticFutureExt {
Expand All @@ -248,7 +220,7 @@ pub trait StaticFutureExt {
/// tokio::time::sleep(Duration::from_millis(500)).await;
/// 42
/// }
/// .spawn("");
/// .spawn();
///
/// assert!(matches!(join_handle.await, Ok(42)));
/// # }
Expand All @@ -258,15 +230,7 @@ pub trait StaticFutureExt {
/// # example().await;
/// # }
/// ```
#[cfg(feature = "metrics")]
fn spawn(self, name: &'static str) -> JoinHandle<<Self::Future as Future>::Output>;

/// Same as [`StaticFutureExt::spawn`], but it won't monitor long running
/// futures.
///
/// Use this only if your future is expected to be long running (ex.
/// singleton).
fn spawn_and_forget(self) -> JoinHandle<<Self::Future as Future>::Output>;
fn spawn(self) -> JoinHandle<<Self::Future as Future>::Output>;
}

impl<T> FutureExt for T
Expand All @@ -292,14 +256,6 @@ where
on_cancel: ready(()),
}
}

#[cfg(feature = "metrics")]
fn with_metrics<R>(self, recorder: R) -> metrics::TaskMetricsFuture<Self::Future, R>
where
R: metrics::TaskMetricsRecorder,
{
metrics::TaskMetricsFuture::new(self, recorder)
}
}

impl<T> StaticFutureExt for T
Expand All @@ -309,19 +265,12 @@ where
{
type Future = T;

#[cfg(feature = "metrics")]
fn spawn(self, name: &'static str) -> JoinHandle<<Self::Future as Future>::Output> {
static METRICS: metrics::TaskMetrics = metrics::TaskMetrics::new("spawned_task");

tokio::spawn(self.with_metrics(METRICS.with_name(name)))
}

fn spawn_and_forget(self) -> JoinHandle<<Self::Future as Future>::Output> {
fn spawn(self) -> JoinHandle<<Self::Future as Future>::Output> {
tokio::spawn(self)
}
}

#[cfg(all(test, feature = "metrics"))]
#[cfg(test)]
mod test {
use {
super::*,
Expand Down Expand Up @@ -356,7 +305,7 @@ mod test {
tokio::time::sleep(Duration::from_millis(100)).await;
b.fetch_add(1, Ordering::SeqCst);
})
.spawn("")
.spawn()
};

tokio::time::sleep(Duration::from_millis(200)).await;
Expand Down Expand Up @@ -385,7 +334,7 @@ mod test {
tokio::time::sleep(Duration::from_millis(100)).await;
b.fetch_add(1, Ordering::Relaxed);
})
.spawn("")
.spawn()
};

tokio::time::sleep(Duration::from_millis(200)).await;
Expand Down Expand Up @@ -416,7 +365,7 @@ mod test {
tokio::time::sleep(Duration::from_millis(100)).await;
b.fetch_add(1, Ordering::Relaxed);
})
.spawn("")
.spawn()
};

assert_eq!(handle.await.unwrap(), Err(Error::Timeout));
Expand All @@ -441,7 +390,7 @@ mod test {
tokio::time::sleep(Duration::from_millis(100)).await;
b.fetch_add(1, Ordering::Relaxed);
})
.spawn("")
.spawn()
};

assert_eq!(handle.await.unwrap(), Ok(42));
Expand Down
8 changes: 0 additions & 8 deletions crates/future_metrics/Cargo.toml

This file was deleted.

Loading

0 comments on commit 62cfec1

Please sign in to comment.