Skip to content

Commit

Permalink
Fix metrics being emitted more than once.
Browse files Browse the repository at this point in the history
  • Loading branch information
kushudai committed Nov 23, 2024
1 parent bf854b9 commit e28c33f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

## 0.8.2 - 2024-11-22

- Fix metrics being emitted more than once.

## 0.8.1 - 2024-11-20

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rs-consul"
version = "0.8.1"
version = "0.8.2"
authors = ["Roblox"]
edition = "2021"
description = "This crate provides access to a set of strongly typed apis to interact with consul (https://www.consul.io/)"
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl Consul {
#[cfg(feature = "metrics")]
{
metrics_info_wrapper.set_status(code);
drop(metrics_info_wrapper.clone());
metrics_info_wrapper.emit_metrics();
}
ConsulError::UnexpectedResponseCode(
code,
Expand All @@ -438,7 +438,7 @@ impl Consul {
#[cfg(feature = "metrics")]
{
metrics_info_wrapper.set_status(StatusCode::OK);
drop(metrics_info_wrapper.clone());
metrics_info_wrapper.emit_metrics();
}
return Ok(response);
}
Expand Down Expand Up @@ -822,7 +822,7 @@ impl Consul {
#[cfg(feature = "metrics")]
{
metrics_info_wrapper.set_status(StatusCode::REQUEST_TIMEOUT);
drop(metrics_info_wrapper.clone());
metrics_info_wrapper.emit_metrics();
}
Err(ConsulError::TimeoutExceeded(dur))
}
Expand All @@ -833,7 +833,7 @@ impl Consul {

let response = response.inspect_err(|_| {
#[cfg(feature = "metrics")]
drop(metrics_info_wrapper.clone());
metrics_info_wrapper.emit_metrics();
})?;

#[cfg(feature = "trace")]
Expand All @@ -844,7 +844,7 @@ impl Consul {
#[cfg(feature = "metrics")]
{
metrics_info_wrapper.set_status(status);
drop(metrics_info_wrapper);
metrics_info_wrapper.emit_metrics();
}

let mut response_body = response
Expand Down
14 changes: 9 additions & 5 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,20 @@ impl MetricInfoWrapper {
pub fn set_status(&mut self, status: StatusCode) {
self.metrics.status = Some(status);
}

pub fn emit_metrics(&mut self) {
if let Some(sender) = self.sender.take() {
let mut metrics = self.metrics;
metrics.duration = Some(self.start.elapsed());
let _ = sender.send(metrics);
}
}
}

#[cfg(feature = "metrics")]
impl Drop for MetricInfoWrapper {
fn drop(&mut self) {
if let Some(sender) = self.sender.take() {
let mut metrics = self.metrics;
metrics.duration = Some(self.start.elapsed());
let _ = sender.send(self.metrics);
}
self.emit_metrics();
}
}

Expand Down

0 comments on commit e28c33f

Please sign in to comment.