From b30ff6affc7a9c84a6748a8f431f8c7066accfa7 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 2 Mar 2021 22:35:47 +0000 Subject: [PATCH] Expose the startup timestamp to Prometheus (#2233) ## Issue Addressed Resolves #1788 ## Proposed Changes As per #1788, expose the time at which the process started via the `process_start_time_seconds` Prometheus metric. This will help users track uptime. ## Additional Info NA Co-authored-by: Michael Sproul --- Cargo.lock | 2 ++ lighthouse/Cargo.toml | 2 ++ lighthouse/src/main.rs | 5 +++++ lighthouse/src/metrics.rs | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 lighthouse/src/metrics.rs diff --git a/Cargo.lock b/Cargo.lock index 84096da4c..7f0362d94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3721,6 +3721,8 @@ dependencies = [ "environment", "eth2_network_config", "futures 0.3.13", + "lazy_static", + "lighthouse_metrics", "lighthouse_version", "logging", "remote_signer", diff --git a/lighthouse/Cargo.toml b/lighthouse/Cargo.toml index 185002e8b..2a4efae62 100644 --- a/lighthouse/Cargo.toml +++ b/lighthouse/Cargo.toml @@ -41,6 +41,8 @@ directory = { path = "../common/directory" } lighthouse_version = { path = "../common/lighthouse_version" } account_utils = { path = "../common/account_utils" } remote_signer = { "path" = "../remote_signer" } +lighthouse_metrics = { path = "../common/lighthouse_metrics" } +lazy_static = "1.4.0" [dev-dependencies] tempfile = "3.1.0" diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index 53f3f2c10..c282c687d 100644 --- a/lighthouse/src/main.rs +++ b/lighthouse/src/main.rs @@ -1,3 +1,5 @@ +mod metrics; + use beacon_node::{get_eth2_network_config, ProductionBeaconNode}; use clap::{App, Arg, ArgMatches}; use env_logger::{Builder, Env}; @@ -219,6 +221,9 @@ fn run( let log = environment.core_context().log().clone(); + // Allow Prometheus to export the time at which the process was started. + metrics::expose_process_start_time(&log); + if matches.is_present("spec") { warn!( log, diff --git a/lighthouse/src/metrics.rs b/lighthouse/src/metrics.rs new file mode 100644 index 000000000..ac880602f --- /dev/null +++ b/lighthouse/src/metrics.rs @@ -0,0 +1,22 @@ +use lazy_static::lazy_static; +pub use lighthouse_metrics::*; +use slog::{error, Logger}; +use std::time::{SystemTime, UNIX_EPOCH}; + +lazy_static! { + pub static ref PROCESS_START_TIME_SECONDS: Result = try_create_int_gauge( + "process_start_time_seconds", + "The unix timestamp at which the process was started" + ); +} + +pub fn expose_process_start_time(log: &Logger) { + match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(duration) => set_gauge(&PROCESS_START_TIME_SECONDS, duration.as_secs() as i64), + Err(e) => error!( + log, + "Failed to read system time"; + "error" => %e + ), + } +}