Skip to content

Commit

Permalink
Merge pull request #51 from Roblox/fix-double-metrics
Browse files Browse the repository at this point in the history
Fix metrics being emitted more than once.
  • Loading branch information
kushudai authored Nov 23, 2024
2 parents bf854b9 + 17350f2 commit 33a3428
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,19 +65,23 @@ impl MetricInfoWrapper {
pub fn set_status(&mut self, status: StatusCode) {
self.metrics.status = Some(status);
}
}

#[cfg(feature = "metrics")]
impl Drop for MetricInfoWrapper {
fn drop(&mut self) {
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(self.metrics);
let _ = sender.send(metrics);
}
}
}

#[cfg(feature = "metrics")]
impl Drop for MetricInfoWrapper {
fn drop(&mut self) {
self.emit_metrics();
}
}

/// The HTTP methods supported by the consul API.
#[derive(Debug, Clone, Copy)]
pub enum HttpMethod {
Expand Down

0 comments on commit 33a3428

Please sign in to comment.