mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-15 09:22:55 +00:00
This patch adds a LaunchTimestamp type to the `metrics` crate, along with a `libmetric_` Prometheus metric. The initial user is pageserver. In addition to exposing the Prometheus metric, it also reproduces the launch timestamp as a header in the API responses. The motivation for this is that we plan to scrape the pageserver's /v1/tenant/:tenant_id/timeline/:timeline_id/layer HTTP endpoint over time. It will soon expose access metrics (#3496) which reset upon process restart. We will use the pageserver's launch ID to identify a restart between two scrape points. However, there are other potential uses. For example, we could use the Prometheus metric to annotate Grafana plots whenever the launch timestamp changes.
35 lines
936 B
Rust
35 lines
936 B
Rust
//! A timestamp captured at process startup to identify restarts of the process, e.g., in logs and metrics.
|
|
|
|
use chrono::Utc;
|
|
|
|
use super::register_uint_gauge;
|
|
use std::fmt::Display;
|
|
|
|
pub struct LaunchTimestamp(chrono::DateTime<Utc>);
|
|
|
|
impl LaunchTimestamp {
|
|
pub fn generate() -> Self {
|
|
LaunchTimestamp(Utc::now())
|
|
}
|
|
}
|
|
|
|
impl Display for LaunchTimestamp {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}", self.0)
|
|
}
|
|
}
|
|
|
|
pub fn set_launch_timestamp_metric(launch_ts: &'static LaunchTimestamp) {
|
|
let millis_since_epoch: u64 = launch_ts
|
|
.0
|
|
.timestamp_millis()
|
|
.try_into()
|
|
.expect("we're after the epoch, this should be positive");
|
|
let metric = register_uint_gauge!(
|
|
"libmetrics_launch_timestamp",
|
|
"Timestamp (millis since epoch) at wich the process launched."
|
|
)
|
|
.unwrap();
|
|
metric.set(millis_since_epoch);
|
|
}
|