diff --git a/pageserver/src/metrics.rs b/pageserver/src/metrics.rs index 3eb70ffac2..3b3522c36a 100644 --- a/pageserver/src/metrics.rs +++ b/pageserver/src/metrics.rs @@ -1053,6 +1053,15 @@ pub(crate) static TENANT_STATE_METRIC: Lazy = Lazy::new(|| { .expect("Failed to register pageserver_tenant_states_count metric") }); +pub(crate) static TIMELINE_STATE_METRIC: Lazy = Lazy::new(|| { + register_uint_gauge_vec!( + "pageserver_timeline_states_count", + "Count of timelines per state", + &["state"] + ) + .expect("Failed to register pageserver_timeline_states_count metric") +}); + /// A set of broken tenants. /// /// These are expected to be so rare that a set is fine. Set as in a new timeseries per each broken @@ -3325,6 +3334,8 @@ impl TimelineMetrics { &timeline_id, ); + TIMELINE_STATE_METRIC.with_label_values(&["active"]).inc(); + TimelineMetrics { tenant_id, shard_id, @@ -3479,6 +3490,8 @@ impl TimelineMetrics { return; } + TIMELINE_STATE_METRIC.with_label_values(&["active"]).dec(); + let tenant_id = &self.tenant_id; let timeline_id = &self.timeline_id; let shard_id = &self.shard_id; diff --git a/pageserver/src/tenant.rs b/pageserver/src/tenant.rs index f9fdc143b4..98a6bc2387 100644 --- a/pageserver/src/tenant.rs +++ b/pageserver/src/tenant.rs @@ -89,7 +89,8 @@ use crate::l0_flush::L0FlushGlobalState; use crate::metrics::{ BROKEN_TENANTS_SET, CIRCUIT_BREAKERS_BROKEN, CIRCUIT_BREAKERS_UNBROKEN, CONCURRENT_INITDBS, INITDB_RUN_TIME, INITDB_SEMAPHORE_ACQUISITION_TIME, TENANT, TENANT_OFFLOADED_TIMELINES, - TENANT_STATE_METRIC, TENANT_SYNTHETIC_SIZE_METRIC, remove_tenant_metrics, + TENANT_STATE_METRIC, TENANT_SYNTHETIC_SIZE_METRIC, TIMELINE_STATE_METRIC, + remove_tenant_metrics, }; use crate::task_mgr::TaskKind; use crate::tenant::config::LocationMode; @@ -544,6 +545,28 @@ pub struct OffloadedTimeline { /// Part of the `OffloadedTimeline` object's lifecycle: this needs to be set before we drop it pub deleted_from_ancestor: AtomicBool, + + _metrics_guard: OffloadedTimelineMetricsGuard, +} + +/// Increases the offloaded timeline count metric when created, and decreases when dropped. +struct OffloadedTimelineMetricsGuard; + +impl OffloadedTimelineMetricsGuard { + fn new() -> Self { + TIMELINE_STATE_METRIC + .with_label_values(&["offloaded"]) + .inc(); + Self + } +} + +impl Drop for OffloadedTimelineMetricsGuard { + fn drop(&mut self) { + TIMELINE_STATE_METRIC + .with_label_values(&["offloaded"]) + .dec(); + } } impl OffloadedTimeline { @@ -576,6 +599,8 @@ impl OffloadedTimeline { delete_progress: timeline.delete_progress.clone(), deleted_from_ancestor: AtomicBool::new(false), + + _metrics_guard: OffloadedTimelineMetricsGuard::new(), }) } fn from_manifest(tenant_shard_id: TenantShardId, manifest: &OffloadedTimelineManifest) -> Self { @@ -595,6 +620,7 @@ impl OffloadedTimeline { archived_at, delete_progress: TimelineDeleteProgress::default(), deleted_from_ancestor: AtomicBool::new(false), + _metrics_guard: OffloadedTimelineMetricsGuard::new(), } } fn manifest(&self) -> OffloadedTimelineManifest {