Line data Source code
1 : //! A timestamp captured at process startup to identify restarts of the process, e.g., in logs and metrics.
2 :
3 : use std::fmt::Display;
4 :
5 : use chrono::Utc;
6 :
7 : use super::register_uint_gauge;
8 :
9 : pub struct LaunchTimestamp(chrono::DateTime<Utc>);
10 :
11 : impl LaunchTimestamp {
12 0 : pub fn generate() -> Self {
13 0 : LaunchTimestamp(Utc::now())
14 0 : }
15 : }
16 :
17 : impl Display for LaunchTimestamp {
18 0 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 0 : write!(f, "{}", self.0)
20 0 : }
21 : }
22 :
23 0 : pub fn set_launch_timestamp_metric(launch_ts: &'static LaunchTimestamp) {
24 0 : let millis_since_epoch: u64 = launch_ts
25 0 : .0
26 0 : .timestamp_millis()
27 0 : .try_into()
28 0 : .expect("we're after the epoch, this should be positive");
29 0 : let metric = register_uint_gauge!(
30 0 : "libmetrics_launch_timestamp",
31 0 : "Timestamp (millis since epoch) at wich the process launched."
32 0 : )
33 0 : .unwrap();
34 0 : metric.set(millis_since_epoch);
35 0 : }
|