Skip to content
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

Add wall clock process metric #267

Merged
merged 1 commit into from
Sep 6, 2024
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
52 changes: 51 additions & 1 deletion kubert-prometheus-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
use prometheus_client::{
collector::Collector,
encoding::{DescriptorEncoder, EncodeMetric},
metrics::{counter::ConstCounter, gauge::ConstGauge, MetricType},
metrics::{
counter::ConstCounter,
gauge::{self, ConstGauge, Gauge},
MetricType,
},
registry::{Registry, Unit},
};
use std::time::{Instant, SystemTime, UNIX_EPOCH};
Expand All @@ -64,6 +68,14 @@ pub fn register(reg: &mut Registry) -> std::io::Result<()> {
ConstGauge::new(start_time_from_epoch.as_secs_f64()),
);

let clock_time_ts = Gauge::<f64, ClockMetric>::default();
reg.register_with_unit(
"clock_time",
"Current system time for this proxy",
Unit::Seconds,
clock_time_ts,
);

reg.register_collector(Box::new(ProcessCollector {
start_time,
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -102,6 +114,44 @@ impl Collector for ProcessCollector {
}
}

// Metric that always reports the current system time on a call to [`get`].
#[derive(Copy, Clone, Debug, Default)]
struct ClockMetric;

impl gauge::Atomic<f64> for ClockMetric {
fn inc(&self) -> f64 {
self.get()
}

fn inc_by(&self, _v: f64) -> f64 {
self.get()
}

fn dec(&self) -> f64 {
self.get()
}

fn dec_by(&self, _v: f64) -> f64 {
self.get()
}

fn set(&self, _v: f64) -> f64 {
self.get()
}

fn get(&self) -> f64 {
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(elapsed) => elapsed.as_secs_f64().floor(),
Err(e) => {
tracing::warn!(
"System time is before the UNIX epoch; reporting negative timestamp"
);
-e.duration().as_secs_f64().floor()
}
}
}
}

#[cfg(target_os = "linux")]
mod linux {
use super::*;
Expand Down