Skip to content

Commit

Permalink
Merge pull request #37 from fpco/error-count
Browse files Browse the repository at this point in the history
Track total error count
  • Loading branch information
snoyberg authored Jul 22, 2024
2 parents 1aa3998 + a7406e4 commit 399afa1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 11 additions & 2 deletions packages/cosmos/src/client/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ struct NodeInner {
pub(crate) struct QueryCount {
pub(crate) first_request: Option<DateTime<Utc>>,
pub(crate) total_query_count: u64,
pub(crate) total_error_count: u64,
}

impl QueryCount {
pub(crate) fn incr(&mut self) {
pub(crate) fn incr(&mut self, is_error: bool) {
if self.first_request.is_none() {
self.first_request = Some(Utc::now());
}
self.total_query_count += 1;
if is_error {
self.total_error_count += 1;
}
}
}

Expand Down Expand Up @@ -188,7 +192,10 @@ impl Node {
}

pub(super) fn log_query_result(&self, res: QueryResult) {
self.node_inner.query_count.write().incr();
self.node_inner.query_count.write().incr(match res {
QueryResult::Success => false,
QueryResult::NetworkError { .. } | QueryResult::OtherError => true,
});
let mut guard = self.node_inner.last_error.write();
match res {
QueryResult::Success | QueryResult::OtherError => {
Expand Down Expand Up @@ -227,6 +234,7 @@ impl Node {
let QueryCount {
first_request,
total_query_count,
total_error_count,
} = *self.node_inner.query_count.read();
SingleNodeHealthReport {
grpc_url: self.node_inner.grpc_url.clone(),
Expand All @@ -253,6 +261,7 @@ impl Node {
}),
first_request,
total_query_count,
total_error_count,
}
}

Expand Down
16 changes: 14 additions & 2 deletions packages/cosmos/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ pub struct SingleNodeHealthReport {
pub error_count: usize,
pub first_request: Option<DateTime<Utc>>,
pub total_query_count: u64,
pub total_error_count: u64,
}

/// Describes the health status of an individual node.
Expand Down Expand Up @@ -1133,11 +1134,22 @@ impl Display for SingleNodeHealthReport {
ConversionError::Overflow => "Overflow when converting since".to_owned(),
ConversionError::DivideByZero => "since is 0".to_owned(),
});
let errors_per_minute = (|| {
let since = u64::try_from(since).map_err(|_| ConversionError::Overflow)?;
self.total_error_count
.checked_div(since)
.ok_or(ConversionError::DivideByZero)
.map(|item| item.to_string())
})()
.unwrap_or_else(|err| match err {
ConversionError::Overflow => "Overflow when converting since".to_owned(),
ConversionError::DivideByZero => "since is 0".to_owned(),
});

write!(
f,
". First request: {} (Since {} minutes). Total queries: {} (RPM: {})",
first_request, since, self.total_query_count, rate_per_minute
". First request: {} (Since {} minutes). Total queries: {} (RPM: {}). Total errors: {} (RPM: {})",
first_request, since, self.total_query_count, rate_per_minute, self.total_error_count, errors_per_minute
)?;
}
Ok(())
Expand Down

0 comments on commit 399afa1

Please sign in to comment.