Skip to content

Commit 28c1255

Browse files
authored
feat(metrics): add futures_in_flight gauge (#27)
1 parent 3d7640f commit 28c1255

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

crates/metrics/src/future.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub mod name {
5353
pub const FUTURES_FINISHED: &str = "futures_finished_count";
5454
pub const FUTURES_CANCELLED: &str = "futures_cancelled_count";
5555

56+
pub const FUTURES_IN_FLIGHT: &str = "futures_in_flight";
57+
5658
pub const FUTURE_POLL_DURATION: &str = "future_poll_duration";
5759
pub const FUTURE_POLL_DURATION_MAX: &str = "future_poll_duration_max";
5860
pub const FUTURE_POLLS: &str = "future_polls_count";
@@ -68,6 +70,8 @@ pub struct Metrics {
6870
finished: Counter,
6971
cancelled: Counter,
7072

73+
in_flight: Gauge,
74+
7175
poll_duration: Histogram,
7276
poll_duration_max: Gauge,
7377
polls: Counter,
@@ -86,6 +90,7 @@ impl Metric for Metrics {
8690
started: counter!(name::FUTURES_STARTED, labels.iter()),
8791
finished: counter!(name::FUTURES_FINISHED, labels.iter()),
8892
cancelled: counter!(name::FUTURES_CANCELLED, labels.iter()),
93+
in_flight: gauge!(name::FUTURES_IN_FLIGHT, labels.iter()),
8994
poll_duration: histogram!(name::FUTURE_POLL_DURATION, labels.iter()),
9095
poll_duration_max: gauge!(name::FUTURE_POLL_DURATION_MAX, labels.iter()),
9196
polls: counter!(name::FUTURE_POLLS, labels.iter()),
@@ -160,6 +165,7 @@ impl<F: Future> Future for Metered<F> {
160165
if state.started_at.is_none() {
161166
state.started_at = Some(Instant::now());
162167
state.metrics.started.increment(1);
168+
state.metrics.in_flight.increment(1);
163169
}
164170

165171
let poll_started_at = Instant::now();
@@ -186,6 +192,10 @@ impl<F: Future> Future for Metered<F> {
186192

187193
impl Drop for State {
188194
fn drop(&mut self) {
195+
if self.started_at.is_some() {
196+
self.metrics.in_flight.decrement(1);
197+
}
198+
189199
if !self.is_finished {
190200
self.metrics.cancelled.increment(1);
191201

0 commit comments

Comments
 (0)