TLA 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 chrono::Utc;
4 :
5 : use super::register_uint_gauge;
6 : use std::fmt::Display;
7 :
8 : pub struct LaunchTimestamp(chrono::DateTime<Utc>);
9 :
10 : impl LaunchTimestamp {
11 CBC 1262 : pub fn generate() -> Self {
12 1262 : LaunchTimestamp(Utc::now())
13 1262 : }
14 : }
15 :
16 : impl Display for LaunchTimestamp {
17 1120 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 1120 : write!(f, "{}", self.0)
19 1120 : }
20 : }
21 :
22 560 : pub fn set_launch_timestamp_metric(launch_ts: &'static LaunchTimestamp) {
23 560 : let millis_since_epoch: u64 = launch_ts
24 560 : .0
25 560 : .timestamp_millis()
26 560 : .try_into()
27 560 : .expect("we're after the epoch, this should be positive");
28 560 : let metric = register_uint_gauge!(
29 560 : "libmetrics_launch_timestamp",
30 560 : "Timestamp (millis since epoch) at wich the process launched."
31 560 : )
32 560 : .unwrap();
33 560 : metric.set(millis_since_epoch);
34 560 : }
|