Skip to content

Commit 2cd2e98

Browse files
authored
feat: gauge support (#8)
* Add support for Gauge metrics * fix typo, remove redundant macros * remove gitignore change
1 parent c39b0a6 commit 2cd2e98

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

src/core.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,93 @@ impl Counter {
9090
}
9191
}
9292

93+
/// Open Metrics [`Gauge`].
94+
#[derive(Debug, Clone)]
95+
pub struct Gauge {
96+
/// The actual prometheus gauge.
97+
#[cfg(feature = "metrics")]
98+
pub gauge: prometheus_client::metrics::gauge::Gauge,
99+
/// What this gauge tracks.
100+
pub description: &'static str,
101+
}
102+
impl Gauge {
103+
/// Constructs a new gauge, based on the given `description`.
104+
pub fn new(description: &'static str) -> Self {
105+
Self {
106+
#[cfg(feature = "metrics")]
107+
gauge: Default::default(),
108+
description,
109+
}
110+
}
111+
112+
/// Increase the [`Gauge`] by 1, returning the previous value.
113+
pub fn inc(&self) -> i64 {
114+
#[cfg(feature = "metrics")]
115+
{
116+
self.gauge.inc()
117+
}
118+
#[cfg(not(feature = "metrics"))]
119+
0
120+
}
121+
/// Increase the [`Gauge`] by `i64`, returning the previous value.
122+
#[cfg(feature = "metrics")]
123+
pub fn inc_by(&self, v: i64) -> i64 {
124+
self.gauge.inc_by(v)
125+
}
126+
/// Increase the [`Gauge`] by `i64`, returning the previous value.
127+
#[cfg(not(feature = "metrics"))]
128+
pub fn inc_by(&self, _v: u64) -> u64 {
129+
0
130+
}
131+
132+
/// Decrease the [`Gauge`] by 1, returning the previous value.
133+
pub fn dec(&self) -> i64 {
134+
#[cfg(feature = "metrics")]
135+
{
136+
self.gauge.dec()
137+
}
138+
#[cfg(not(feature = "metrics"))]
139+
0
140+
}
141+
/// Decrease the [`Gauge`] by `i64`, returning the previous value.
142+
#[cfg(feature = "metrics")]
143+
pub fn dec_by(&self, v: i64) -> i64 {
144+
self.gauge.dec_by(v)
145+
}
146+
/// Decrease the [`Gauge`] by `i64`, returning the previous value.
147+
#[cfg(not(feature = "metrics"))]
148+
pub fn dec_by(&self, _v: u64) -> u64 {
149+
0
150+
}
151+
152+
/// Set the [`Gauge`] value.
153+
#[cfg(feature = "metrics")]
154+
pub fn set(&self, v: i64) -> i64 {
155+
self.gauge
156+
.inner()
157+
.store(v, std::sync::atomic::Ordering::Relaxed);
158+
v
159+
}
160+
/// Set the [`Gauge`] value.
161+
#[cfg(not(feature = "metrics"))]
162+
pub fn set(&self, _v: i64) -> i64 {
163+
0
164+
}
165+
166+
/// Get the [`Gauge`] value.
167+
#[cfg(feature = "metrics")]
168+
pub fn get(&self) -> i64 {
169+
self.gauge
170+
.inner()
171+
.load(std::sync::atomic::Ordering::Relaxed)
172+
}
173+
/// Get the [`Gauge`] value.
174+
#[cfg(not(feature = "metrics"))]
175+
pub fn get(&self) -> i64 {
176+
0
177+
}
178+
}
179+
93180
/// Description of a group of metrics.
94181
pub trait Metric:
95182
Default + struct_iterable::Iterable + Sized + std::fmt::Debug + 'static + Send + Sync

src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,46 @@ pub enum Error {
2626
Io(#[from] std::io::Error),
2727
}
2828

29-
/// Increment the given counter by 1.
29+
/// Increment the given counter or gauge by 1.
3030
#[macro_export]
3131
macro_rules! inc {
3232
($m:ty, $f:ident) => {
3333
<$m as $crate::core::Metric>::with_metric(|m| m.$f.inc());
3434
};
3535
}
3636

37-
/// Increment the given counter `n`.
37+
/// Increment the given counter or gauge by `n`.
3838
#[macro_export]
3939
macro_rules! inc_by {
4040
($m:ty, $f:ident, $n:expr) => {
4141
<$m as $crate::core::Metric>::with_metric(|m| m.$f.inc_by($n));
4242
};
4343
}
4444

45-
/// Set the given counter to `n`.
45+
/// Set the given counter or gauge to `n`.
4646
#[macro_export]
4747
macro_rules! set {
4848
($m:ty, $f:ident, $n:expr) => {
4949
<$m as $crate::core::Metric>::with_metric(|m| m.$f.set($n));
5050
};
5151
}
5252

53+
/// Decrement the given gauge by 1.
54+
#[macro_export]
55+
macro_rules! dec {
56+
($m:ty, $f:ident) => {
57+
<$m as $crate::core::Metric>::with_metric(|m| m.$f.dec());
58+
};
59+
}
60+
61+
/// Decrement the given gauge `n`.
62+
#[macro_export]
63+
macro_rules! dec_by {
64+
($m:ty, $f:ident, $n:expr) => {
65+
<$m as $crate::core::Metric>::with_metric(|m| m.$f.dec_by($n));
66+
};
67+
}
68+
5369
/// Parse Prometheus metrics from a string.
5470
pub fn parse_prometheus_metrics(data: &str) -> HashMap<String, f64> {
5571
let mut metrics = HashMap::new();

0 commit comments

Comments
 (0)