From 9ed1c9b36c522d0315e6f5915aa42b36025ed1b7 Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Thu, 9 Oct 2025 11:11:50 +0200 Subject: [PATCH] feat: metric for own version and startup timestamp --- Cargo.lock | 1 + anchor/client/Cargo.toml | 1 + anchor/client/src/lib.rs | 5 +++++ anchor/client/src/metrics.rs | 37 ++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 anchor/client/src/metrics.rs diff --git a/Cargo.lock b/Cargo.lock index e4ae63c85..a75ff8ed5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1837,6 +1837,7 @@ dependencies = [ "message_receiver", "message_sender", "message_validator", + "metrics", "multiaddr", "network", "network_utils", diff --git a/anchor/client/Cargo.toml b/anchor/client/Cargo.toml index 73ab41055..1a8bbbec2 100644 --- a/anchor/client/Cargo.toml +++ b/anchor/client/Cargo.toml @@ -28,6 +28,7 @@ logging = { workspace = true } message_receiver = { workspace = true } message_sender = { workspace = true } message_validator = { workspace = true } +metrics = { workspace = true } multiaddr = { workspace = true } network = { workspace = true } network_utils = { workspace = true } diff --git a/anchor/client/src/lib.rs b/anchor/client/src/lib.rs index 0e6e1492a..93be44d29 100644 --- a/anchor/client/src/lib.rs +++ b/anchor/client/src/lib.rs @@ -1,6 +1,7 @@ pub mod cli; pub mod config; mod key; +mod metrics; mod notifier; use std::{ @@ -156,6 +157,10 @@ impl Client { let metrics_future = http_metrics::serve(listener, shared_state.clone(), exit); executor.spawn_without_exit(metrics_future, "metrics-http"); + + metrics::expose_anchor_version(); + metrics::expose_process_start_time(); + Some(shared_state) } else { info!("HTTP metrics server is disabled"); diff --git a/anchor/client/src/metrics.rs b/anchor/client/src/metrics.rs new file mode 100644 index 000000000..07602625c --- /dev/null +++ b/anchor/client/src/metrics.rs @@ -0,0 +1,37 @@ +use std::{ + sync::LazyLock, + time::{SystemTime, UNIX_EPOCH}, +}; + +use metrics::*; +use tracing::error; +use version::VERSION; + +pub static PROCESS_START_TIME_SECONDS: LazyLock> = LazyLock::new(|| { + try_create_int_gauge( + "process_start_time_seconds", + "The unix timestamp at which the process was started", + ) +}); + +pub static ANCHOR_VERSION: LazyLock> = LazyLock::new(|| { + try_create_int_gauge_vec( + "anchor_info", + "The build of Anchor running on the server", + &["version"], + ) +}); + +pub fn expose_process_start_time() { + match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(duration) => set_gauge(&PROCESS_START_TIME_SECONDS, duration.as_secs() as i64), + Err(e) => error!( + error = %e, + "Failed to read system time" + ), + } +} + +pub fn expose_anchor_version() { + set_gauge_vec(&ANCHOR_VERSION, &[VERSION], 1); +}