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 gauge to proxy metrics #3176

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion linkerd/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod prom {
metrics::{
counter::{ConstCounter, Counter},
family::Family,
gauge::{ConstGauge, Gauge},
gauge::{Atomic as GaugeAtomic, ConstGauge, Gauge},
histogram::Histogram,
info::Info,
},
Expand Down
46 changes: 46 additions & 0 deletions linkerd/metrics/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ pub fn register(reg: &mut prom::Registry) {
prom::ConstGauge::new(start_time_from_epoch.as_secs_f64()),
);

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

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

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

impl prom::GaugeAtomic<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 crate::prom::{self, encoding::EncodeMetric};
Expand Down
Loading