Skip to content

fix: cleanup metrics feature flag usage #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 54 additions & 59 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,33 @@ impl Counter {
}

/// Increases the [`Counter`] by `u64`, returning the previous value.
#[cfg(feature = "metrics")]
pub fn inc_by(&self, v: u64) -> u64 {
self.counter.inc_by(v)
#[cfg(feature = "metrics")]
{
self.counter.inc_by(v)
}
#[cfg(not(feature = "metrics"))]
{
let _ = v;
0
}
}

/// Sets the [`Counter`] value.
/// Sets the [`Counter`] value, returning the previous value.
///
/// Warning: this is not default behavior for a counter that should always be monotonically increasing.
#[cfg(feature = "metrics")]
pub fn set(&self, v: u64) -> u64 {
self.counter
.inner()
.store(v, std::sync::atomic::Ordering::Relaxed);
v
}

/// Sets the [`Counter`] value.
///
/// Warning: this is not default behavior for a counter that should always be monotonically increasing.
#[cfg(not(feature = "metrics"))]
pub fn set(&self, _v: u64) -> u64 {
0
}

/// Increases the [`Counter`] by `u64`, returning the previous value.
#[cfg(not(feature = "metrics"))]
pub fn inc_by(&self, _v: u64) -> u64 {
0
#[cfg(feature = "metrics")]
{
self.counter
.inner()
.swap(v, std::sync::atomic::Ordering::Relaxed)
}
#[cfg(not(feature = "metrics"))]
{
let _ = v;
0
}
}

/// Returns the current value of the [`Counter`].
Expand Down Expand Up @@ -194,15 +193,16 @@ impl Gauge {
}

/// Increases the [`Gauge`] by `i64`, returning the previous value.
#[cfg(feature = "metrics")]
pub fn inc_by(&self, v: i64) -> i64 {
self.gauge.inc_by(v)
}

/// Increases the [`Gauge`] by `i64`, returning the previous value.
#[cfg(not(feature = "metrics"))]
pub fn inc_by(&self, _v: u64) -> u64 {
0
#[cfg(feature = "metrics")]
{
self.gauge.inc_by(v)
}
#[cfg(not(feature = "metrics"))]
{
let _ = v;
0
}
}

/// Decreases the [`Gauge`] by 1, returning the previous value.
Expand All @@ -216,43 +216,38 @@ impl Gauge {
}

/// Decreases the [`Gauge`] by `i64`, returning the previous value.
#[cfg(feature = "metrics")]
pub fn dec_by(&self, v: i64) -> i64 {
self.gauge.dec_by(v)
}

/// Decreases the [`Gauge`] by `i64`, returning the previous value.
#[cfg(not(feature = "metrics"))]
pub fn dec_by(&self, _v: u64) -> u64 {
0
#[cfg(feature = "metrics")]
{
self.gauge.dec_by(v)
}
#[cfg(not(feature = "metrics"))]
{
let _ = v;
0
}
}

/// Sets the [`Gauge`] value.
#[cfg(feature = "metrics")]
/// Sets the [`Gauge`] to `v`, returning the previous value.
pub fn set(&self, v: i64) -> i64 {
self.gauge
.inner()
.store(v, std::sync::atomic::Ordering::Relaxed);
v
}

/// Sets the [`Gauge`] value.
#[cfg(not(feature = "metrics"))]
pub fn set(&self, _v: i64) -> i64 {
0
}

/// Returns the [`Gauge`] value.
#[cfg(feature = "metrics")]
pub fn get(&self) -> i64 {
self.gauge
.inner()
.load(std::sync::atomic::Ordering::Relaxed)
#[cfg(feature = "metrics")]
{
self.gauge.set(v)
}
#[cfg(not(feature = "metrics"))]
{
let _ = v;
0
}
}

/// Returns the [`Gauge`] value.
#[cfg(not(feature = "metrics"))]
pub fn get(&self) -> i64 {
#[cfg(feature = "metrics")]
{
self.gauge.get()
}
#[cfg(not(feature = "metrics"))]
0
}
}
Loading