Skip to content

Commit e590998

Browse files
authored
fix: cleanup metrics feature flag usage (#20)
Make it consistent how the `metrics` feature flag is applied to metrics methods.
1 parent ff4ac2a commit e590998

File tree

1 file changed

+54
-59
lines changed

1 file changed

+54
-59
lines changed

src/metrics.rs

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,33 @@ impl Counter {
104104
}
105105

106106
/// Increases the [`Counter`] by `u64`, returning the previous value.
107-
#[cfg(feature = "metrics")]
108107
pub fn inc_by(&self, v: u64) -> u64 {
109-
self.counter.inc_by(v)
108+
#[cfg(feature = "metrics")]
109+
{
110+
self.counter.inc_by(v)
111+
}
112+
#[cfg(not(feature = "metrics"))]
113+
{
114+
let _ = v;
115+
0
116+
}
110117
}
111118

112-
/// Sets the [`Counter`] value.
119+
/// Sets the [`Counter`] value, returning the previous value.
113120
///
114121
/// Warning: this is not default behavior for a counter that should always be monotonically increasing.
115-
#[cfg(feature = "metrics")]
116122
pub fn set(&self, v: u64) -> u64 {
117-
self.counter
118-
.inner()
119-
.store(v, std::sync::atomic::Ordering::Relaxed);
120-
v
121-
}
122-
123-
/// Sets the [`Counter`] value.
124-
///
125-
/// Warning: this is not default behavior for a counter that should always be monotonically increasing.
126-
#[cfg(not(feature = "metrics"))]
127-
pub fn set(&self, _v: u64) -> u64 {
128-
0
129-
}
130-
131-
/// Increases the [`Counter`] by `u64`, returning the previous value.
132-
#[cfg(not(feature = "metrics"))]
133-
pub fn inc_by(&self, _v: u64) -> u64 {
134-
0
123+
#[cfg(feature = "metrics")]
124+
{
125+
self.counter
126+
.inner()
127+
.swap(v, std::sync::atomic::Ordering::Relaxed)
128+
}
129+
#[cfg(not(feature = "metrics"))]
130+
{
131+
let _ = v;
132+
0
133+
}
135134
}
136135

137136
/// Returns the current value of the [`Counter`].
@@ -194,15 +193,16 @@ impl Gauge {
194193
}
195194

196195
/// Increases the [`Gauge`] by `i64`, returning the previous value.
197-
#[cfg(feature = "metrics")]
198196
pub fn inc_by(&self, v: i64) -> i64 {
199-
self.gauge.inc_by(v)
200-
}
201-
202-
/// Increases the [`Gauge`] by `i64`, returning the previous value.
203-
#[cfg(not(feature = "metrics"))]
204-
pub fn inc_by(&self, _v: u64) -> u64 {
205-
0
197+
#[cfg(feature = "metrics")]
198+
{
199+
self.gauge.inc_by(v)
200+
}
201+
#[cfg(not(feature = "metrics"))]
202+
{
203+
let _ = v;
204+
0
205+
}
206206
}
207207

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

218218
/// Decreases the [`Gauge`] by `i64`, returning the previous value.
219-
#[cfg(feature = "metrics")]
220219
pub fn dec_by(&self, v: i64) -> i64 {
221-
self.gauge.dec_by(v)
222-
}
223-
224-
/// Decreases the [`Gauge`] by `i64`, returning the previous value.
225-
#[cfg(not(feature = "metrics"))]
226-
pub fn dec_by(&self, _v: u64) -> u64 {
227-
0
220+
#[cfg(feature = "metrics")]
221+
{
222+
self.gauge.dec_by(v)
223+
}
224+
#[cfg(not(feature = "metrics"))]
225+
{
226+
let _ = v;
227+
0
228+
}
228229
}
229230

230-
/// Sets the [`Gauge`] value.
231-
#[cfg(feature = "metrics")]
231+
/// Sets the [`Gauge`] to `v`, returning the previous value.
232232
pub fn set(&self, v: i64) -> i64 {
233-
self.gauge
234-
.inner()
235-
.store(v, std::sync::atomic::Ordering::Relaxed);
236-
v
237-
}
238-
239-
/// Sets the [`Gauge`] value.
240-
#[cfg(not(feature = "metrics"))]
241-
pub fn set(&self, _v: i64) -> i64 {
242-
0
243-
}
244-
245-
/// Returns the [`Gauge`] value.
246-
#[cfg(feature = "metrics")]
247-
pub fn get(&self) -> i64 {
248-
self.gauge
249-
.inner()
250-
.load(std::sync::atomic::Ordering::Relaxed)
233+
#[cfg(feature = "metrics")]
234+
{
235+
self.gauge.set(v)
236+
}
237+
#[cfg(not(feature = "metrics"))]
238+
{
239+
let _ = v;
240+
0
241+
}
251242
}
252243

253244
/// Returns the [`Gauge`] value.
254-
#[cfg(not(feature = "metrics"))]
255245
pub fn get(&self) -> i64 {
246+
#[cfg(feature = "metrics")]
247+
{
248+
self.gauge.get()
249+
}
250+
#[cfg(not(feature = "metrics"))]
256251
0
257252
}
258253
}

0 commit comments

Comments
 (0)