Skip to content

Commit

Permalink
Add metric for active time of monitored futures
Browse files Browse the repository at this point in the history
  • Loading branch information
aschran committed Sep 5, 2024
1 parent bb77882 commit 3dcf8d9
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion crates/mysten-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct Metrics {
pub channel_inflight: IntGaugeVec,
pub channel_sent: IntGaugeVec,
pub channel_received: IntGaugeVec,
pub future_active_duration_ns: IntGaugeVec,
pub scope_iterations: IntGaugeVec,
pub scope_duration_ns: IntGaugeVec,
pub scope_entrance: IntGaugeVec,
Expand Down Expand Up @@ -107,6 +108,13 @@ impl Metrics {
registry,
)
.unwrap(),
future_active_duration_ns: register_int_gauge_vec_with_registry!(
"monitored_future_active_duration_ns",
"Total duration in nanosecs where the monitored future is active (consuming CPU time)",
&["name"],
registry,
)
.unwrap(),
scope_entrance: register_int_gauge_vec_with_registry!(
"monitored_scope_entrance",
"Number of entrance in the scope.",
Expand Down Expand Up @@ -349,21 +357,30 @@ impl<F: Future> MonitoredFutureExt for F {
fn in_monitored_scope(self, name: &'static str) -> MonitoredScopeFuture<Self> {
MonitoredScopeFuture {
f: Box::pin(self),
name,
_scope: monitored_scope(name),
}
}
}

pub struct MonitoredScopeFuture<F: Sized> {
f: Pin<Box<F>>,
name: &'static str,
_scope: Option<MonitoredScopeGuard>,
}

impl<F: Future> Future for MonitoredScopeFuture<F> {
type Output = F::Output;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.f.as_mut().poll(cx)
let active_timer = Instant::now();
let ret = self.f.as_mut().poll(cx);
if let Some(m) = get_metrics() {
m.future_active_duration_ns
.with_label_values(&[self.name])
.add(active_timer.elapsed().as_nanos() as i64);
}
ret
}
}

Expand Down

0 comments on commit 3dcf8d9

Please sign in to comment.