Skip to content

feat: gauge support #8

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
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/target
.helix
87 changes: 87 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,93 @@ impl Counter {
}
}

/// Open Metrics [`Gauge`].
#[derive(Debug, Clone)]
pub struct Gauge {
/// The actual prometheus gauge.
#[cfg(feature = "metrics")]
pub gauge: prometheus_client::metrics::gauge::Gauge,
/// What this gauge tracks.
pub description: &'static str,
}
impl Gauge {
/// Constructs a new gauge, based on the given `description`.
pub fn new(description: &'static str) -> Self {
Self {
#[cfg(feature = "metrics")]
gauge: Default::default(),
description,
}
}

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

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

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

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

/// Description of a group of metrics.
pub trait Metric:
Default + struct_iterable::Iterable + Sized + std::fmt::Debug + 'static + Send + Sync
Expand Down
22 changes: 19 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,46 @@ pub enum Error {
Io(#[from] std::io::Error),
}

/// Increment the given counter by 1.
/// Increment the given counter or gauge by 1.
#[macro_export]
macro_rules! inc {
($m:ty, $f:ident) => {
<$m as $crate::core::Metric>::with_metric(|m| m.$f.inc());
};
}

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

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

/// Decrement the given gauge by 1.
#[macro_export]
macro_rules! dec {
($m:ty, $f:ident) => {
<$m as $crate::core::Metric>::with_metric(|m| m.$f.dec());
};
}

/// Decrement the given gauge `n`.
#[macro_export]
macro_rules! dec_by {
($m:ty, $f:ident, $n:expr) => {
<$m as $crate::core::Metric>::with_metric(|m| m.$f.dec_by($n));
};
}

/// Parse Prometheus metrics from a string.
pub fn parse_prometheus_metrics(data: &str) -> HashMap<String, f64> {
let mut metrics = HashMap::new();
Expand Down
Loading