Skip to content

Commit

Permalink
Only consume timing metrics if set
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-cattermole committed May 13, 2024
1 parent 984ffab commit f3fea4b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions limitador-server/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Timings {
idle: u64,
busy: u64,
last: Instant,
updated: bool,
}

impl Timings {
Expand All @@ -19,6 +20,7 @@ impl Timings {
idle: 0,
busy: 0,
last: Instant::now(),
updated: false,
}
}
}
Expand All @@ -31,6 +33,7 @@ impl ops::Add for Timings {
busy: self.busy + rhs.busy,
idle: self.idle + rhs.idle,
last: self.last.max(rhs.last),
updated: self.updated || rhs.updated,
}
}
}
Expand Down Expand Up @@ -160,6 +163,7 @@ where
let now = Instant::now();
timings.idle += (now - timings.last).as_nanos() as u64;
timings.last = now;
timings.updated = true;
}
}

Expand All @@ -171,6 +175,7 @@ where
let now = Instant::now();
timings.busy += (now - timings.last).as_nanos() as u64;
timings.last = now;
timings.updated = true;
}
}

Expand Down Expand Up @@ -210,7 +215,9 @@ where
}
// IF we are aggregator call consume function
if let Some(metrics_group) = self.groups.get(name) {
(metrics_group.consumer)(*span_state.group_times.get(name).unwrap())
if let Some(t) = span_state.group_times.get(name).filter(|&t| t.updated) {
(metrics_group.consumer)(*t);
}
}
}
}
Expand All @@ -228,19 +235,22 @@ mod tests {
idle: 5,
busy: 5,
last: now,
updated: false,
};
let t2 = Timings {
idle: 3,
busy: 5,
last: now,
updated: false,
};
let t3 = t1 + t2;
assert_eq!(
t3,
Timings {
idle: 8,
busy: 10,
last: now
last: now,
updated: false,
}
)
}
Expand All @@ -252,19 +262,22 @@ mod tests {
idle: 5,
busy: 5,
last: now,
updated: false,
};
let t2 = Timings {
idle: 3,
busy: 5,
last: now,
updated: false,
};
t1 += t2;
assert_eq!(
t1,
Timings {
idle: 8,
busy: 10,
last: now
last: now,
updated: false,
}
)
}
Expand All @@ -277,6 +290,7 @@ mod tests {
idle: 5,
busy: 5,
last: Instant::now(),
updated: true,
};
span_state.increment(&group, t1);
assert_eq!(span_state.group_times.get(&group).unwrap().idle, t1.idle);
Expand Down

0 comments on commit f3fea4b

Please sign in to comment.