Skip to content

Commit

Permalink
healthchecks latencies
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimryndin committed May 21, 2024
1 parent 7176aa0 commit 1b43ab2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* 0.1.6
* safe numbers conversions
* ids collision tests
* latency measurements for healthchecks

* 0.1.5
* increase max body size for latest release check
Expand Down
17 changes: 14 additions & 3 deletions src/services/healthcheck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::time::Duration;
use std::time::{Duration, Instant};
use tokio::net::TcpSocket;
use tokio::process::Command;
use tokio::sync::mpsc::{self, error::TrySendError};
Expand Down Expand Up @@ -243,6 +243,7 @@ impl HealthcheckService {
fn create_datarow(
is_alive: bool,
text: String,
latency: Duration,
liveness: &Liveness,
probe_time: DateTime<Utc>,
) -> Datarow {
Expand All @@ -251,6 +252,10 @@ impl HealthcheckService {
probe_time.naive_utc(),
vec![
("is_alive".to_string(), Datavalue::Bool(is_alive)),
(
"latency_ms".to_string(),
Datavalue::Integer(latency.as_millis() as u32),
), // SAFE: cap latency at u32::MAX
("output".to_string(), Datavalue::Text(text)),
],
)
Expand All @@ -270,11 +275,12 @@ impl HealthcheckService {
tokio::select! {
_ = interval.tick() => {
let probe_time = Utc::now();
let start = Instant::now();
let result = Self::is_alive(&liveness).await
.map(|t| Data::Single(Self::create_datarow(true, t, &liveness, probe_time)))
.map(|t| Data::Single(Self::create_datarow(true, t, start.elapsed(), &liveness, probe_time)))
.map_err(|t| {
tracing::debug!("liveness check for {:?} failed with output `{}`", liveness, t);
Data::Single(Self::create_datarow(false, t, &liveness, probe_time))
Data::Single(Self::create_datarow(false, t, start.elapsed(), &liveness, probe_time))
});
match sender.try_send(TaskResult{id: index, result}) {
Err(TrySendError::Full(res)) => {
Expand Down Expand Up @@ -666,6 +672,11 @@ mod tests {
datarow.keys_values().get("output"),
Some(&Datavalue::Text(HEALTHY_REPLY.to_string()))
);
let Datavalue::Integer(latency) = *datarow.keys_values().get("latency_ms").unwrap()
else {
panic!("latency is an integer in ms")
};
assert!(latency < 100);
} else {
panic!("test assert: at least one successfull probe should be collected");
}
Expand Down

0 comments on commit 1b43ab2

Please sign in to comment.