Skip to content

Commit

Permalink
cache active time metric
Browse files Browse the repository at this point in the history
  • Loading branch information
aschran committed Sep 5, 2024
1 parent 3dcf8d9 commit 62f6f08
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions crates/mysten-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use axum::{extract::Extension, http::StatusCode, routing::get, Router};
use dashmap::DashMap;
use parking_lot::Mutex;
use prometheus::core::{AtomicI64, GenericGauge};
use simple_server_timing_header::Timer;
use std::future::Future;
use std::net::SocketAddr;
Expand Down Expand Up @@ -357,15 +358,16 @@ impl<F: Future> MonitoredFutureExt for F {
fn in_monitored_scope(self, name: &'static str) -> MonitoredScopeFuture<Self> {
MonitoredScopeFuture {
f: Box::pin(self),
name,
active_duration_metric: get_metrics()
.map(|m| m.future_active_duration_ns.with_label_values(&[name])),
_scope: monitored_scope(name),
}
}
}

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

Expand All @@ -375,10 +377,8 @@ impl<F: Future> Future for MonitoredScopeFuture<F> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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);
if let Some(m) = &self.active_duration_metric {
m.add(active_timer.elapsed().as_nanos() as i64);
}
ret
}
Expand Down

0 comments on commit 62f6f08

Please sign in to comment.