LCOV - code coverage report
Current view: top level - storage_controller/src - metrics.rs (source / functions) Coverage Total Hit
Test: 1e20c4f2b28aa592527961bb32170ebbd2c9172f.info Lines: 15.2 % 66 10
Test Date: 2025-07-16 12:29:03 Functions: 10.0 % 10 1

            Line data    Source code
       1              : //!
       2              : //! This module provides metric definitions for the storage controller.
       3              : //!
       4              : //! All metrics are grouped in [`StorageControllerMetricGroup`]. [`StorageControllerMetrics`] holds
       5              : //! the mentioned metrics and their encoder. It's globally available via the [`METRICS_REGISTRY`]
       6              : //! constant.
       7              : //!
       8              : //! The rest of the code defines label group types and deals with converting outer types to labels.
       9              : //!
      10              : use std::sync::Mutex;
      11              : 
      12              : use bytes::Bytes;
      13              : use measured::label::LabelValue;
      14              : use measured::metric::histogram;
      15              : use measured::{FixedCardinalityLabel, MetricGroup};
      16              : use metrics::NeonMetrics;
      17              : use once_cell::sync::Lazy;
      18              : use strum::IntoEnumIterator;
      19              : 
      20              : use crate::persistence::{DatabaseError, DatabaseOperation};
      21              : use crate::service::LeadershipStatus;
      22              : 
      23              : pub(crate) static METRICS_REGISTRY: Lazy<StorageControllerMetrics> =
      24              :     Lazy::new(StorageControllerMetrics::default);
      25              : 
      26            0 : pub fn preinitialize_metrics() {
      27            0 :     Lazy::force(&METRICS_REGISTRY);
      28            0 : }
      29              : 
      30              : pub(crate) struct StorageControllerMetrics {
      31              :     pub(crate) metrics_group: StorageControllerMetricGroup,
      32              :     encoder: Mutex<measured::text::BufferedTextEncoder>,
      33              : }
      34              : 
      35              : #[derive(measured::MetricGroup)]
      36              : #[metric(new())]
      37              : pub(crate) struct StorageControllerMetricGroup {
      38              :     /// Count of how many times we spawn a reconcile task
      39              :     pub(crate) storage_controller_reconcile_spawn: measured::Counter,
      40              : 
      41              :     /// Size of the in-memory map of tenant shards
      42              :     pub(crate) storage_controller_tenant_shards: measured::Gauge,
      43              : 
      44              :     /// Size of the in-memory map of pageserver_nodes
      45              :     pub(crate) storage_controller_pageserver_nodes: measured::Gauge,
      46              : 
      47              :     /// Count of how many pageserver nodes from in-memory map have https configured
      48              :     pub(crate) storage_controller_https_pageserver_nodes: measured::Gauge,
      49              : 
      50              :     /// Size of the in-memory map of safekeeper_nodes
      51              :     pub(crate) storage_controller_safekeeper_nodes: measured::Gauge,
      52              : 
      53              :     /// Count of how many safekeeper nodes from in-memory map have https configured
      54              :     pub(crate) storage_controller_https_safekeeper_nodes: measured::Gauge,
      55              : 
      56              :     /// Reconciler tasks completed, broken down by success/failure/cancelled
      57              :     pub(crate) storage_controller_reconcile_complete:
      58              :         measured::CounterVec<ReconcileCompleteLabelGroupSet>,
      59              : 
      60              :     /// Count of how many times we make an optimization change to a tenant's scheduling
      61              :     pub(crate) storage_controller_schedule_optimization: measured::Counter,
      62              : 
      63              :     /// How many shards are not scheduled into their preferred AZ
      64              :     pub(crate) storage_controller_schedule_az_violation: measured::Gauge,
      65              : 
      66              :     /// How many shard locations (secondary or attached) on each node
      67              :     pub(crate) storage_controller_node_shards: measured::GaugeVec<NodeLabelGroupSet>,
      68              : 
      69              :     /// How many _attached_ shard locations on each node
      70              :     pub(crate) storage_controller_node_attached_shards: measured::GaugeVec<NodeLabelGroupSet>,
      71              : 
      72              :     /// How many _home_ shard locations on each node (i.e. the node's AZ matches the shard's
      73              :     /// preferred AZ)
      74              :     pub(crate) storage_controller_node_home_shards: measured::GaugeVec<NodeLabelGroupSet>,
      75              : 
      76              :     /// How many shards would like to reconcile but were blocked by concurrency limits
      77              :     pub(crate) storage_controller_pending_reconciles: measured::Gauge,
      78              : 
      79              :     /// How many shards are keep-failing and will be ignored when considering to run optimizations
      80              :     pub(crate) storage_controller_keep_failing_reconciles: measured::Gauge,
      81              : 
      82              :     /// HTTP request status counters for handled requests
      83              :     pub(crate) storage_controller_http_request_status:
      84              :         measured::CounterVec<HttpRequestStatusLabelGroupSet>,
      85              : 
      86              :     /// HTTP request handler latency across all status codes
      87              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
      88              :     pub(crate) storage_controller_http_request_latency:
      89              :         measured::HistogramVec<HttpRequestLatencyLabelGroupSet, 5>,
      90              : 
      91              :     /// HTTP rate limiting latency across all tenants and endpoints
      92              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 10.0))]
      93              :     pub(crate) storage_controller_http_request_rate_limited: measured::Histogram<10>,
      94              : 
      95              :     /// Count of HTTP requests to the pageserver that resulted in an error,
      96              :     /// broken down by the pageserver node id, request name and method
      97              :     pub(crate) storage_controller_pageserver_request_error:
      98              :         measured::CounterVec<PageserverRequestLabelGroupSet>,
      99              : 
     100              :     /// Count of HTTP requests to the safekeeper that resulted in an error,
     101              :     /// broken down by the safekeeper node id, request name and method
     102              :     pub(crate) storage_controller_safekeeper_request_error:
     103              :         measured::CounterVec<SafekeeperRequestLabelGroupSet>,
     104              : 
     105              :     /// Latency of HTTP requests to the pageserver, broken down by pageserver
     106              :     /// node id, request name and method. This include both successful and unsuccessful
     107              :     /// requests.
     108              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     109              :     pub(crate) storage_controller_pageserver_request_latency:
     110              :         measured::HistogramVec<PageserverRequestLabelGroupSet, 5>,
     111              : 
     112              :     /// Latency of HTTP requests to the safekeeper, broken down by safekeeper
     113              :     /// node id, request name and method. This include both successful and unsuccessful
     114              :     /// requests.
     115              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     116              :     pub(crate) storage_controller_safekeeper_request_latency:
     117              :         measured::HistogramVec<SafekeeperRequestLabelGroupSet, 5>,
     118              : 
     119              :     /// Count of pass-through HTTP requests to the pageserver that resulted in an error,
     120              :     /// broken down by the pageserver node id, request name and method
     121              :     pub(crate) storage_controller_passthrough_request_error:
     122              :         measured::CounterVec<PageserverRequestLabelGroupSet>,
     123              : 
     124              :     /// Latency of pass-through HTTP requests to the pageserver, broken down by pageserver
     125              :     /// node id, request name and method. This include both successful and unsuccessful
     126              :     /// requests.
     127              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     128              :     pub(crate) storage_controller_passthrough_request_latency:
     129              :         measured::HistogramVec<PageserverRequestLabelGroupSet, 5>,
     130              : 
     131              :     /// Count of errors in database queries, broken down by error type and operation.
     132              :     pub(crate) storage_controller_database_query_error:
     133              :         measured::CounterVec<DatabaseQueryErrorLabelGroupSet>,
     134              : 
     135              :     /// Latency of database queries, broken down by operation.
     136              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     137              :     pub(crate) storage_controller_database_query_latency:
     138              :         measured::HistogramVec<DatabaseQueryLatencyLabelGroupSet, 5>,
     139              : 
     140              :     pub(crate) storage_controller_leadership_status: measured::GaugeVec<LeadershipStatusGroupSet>,
     141              : 
     142              :     /// Indicator of stucked (long-running) reconciles, broken down by tenant, shard and sequence.
     143              :     /// The metric is automatically removed once the reconciliation completes.
     144              :     pub(crate) storage_controller_reconcile_long_running:
     145              :         measured::CounterVec<ReconcileLongRunningLabelGroupSet>,
     146              : 
     147              :     /// Indicator of safekeeper reconciler queue depth, broken down by safekeeper, excluding ongoing reconciles.
     148              :     pub(crate) storage_controller_safekeeper_reconciles_queued:
     149              :         measured::GaugeVec<SafekeeperReconcilerLabelGroupSet>,
     150              : 
     151              :     /// Indicator of completed safekeeper reconciles, broken down by safekeeper.
     152              :     pub(crate) storage_controller_safekeeper_reconciles_complete:
     153              :         measured::CounterVec<SafekeeperReconcilerLabelGroupSet>,
     154              : }
     155              : 
     156              : impl StorageControllerMetrics {
     157            0 :     pub(crate) fn encode(&self, neon_metrics: &NeonMetrics) -> Bytes {
     158            0 :         let mut encoder = self.encoder.lock().unwrap();
     159            0 :         neon_metrics
     160            0 :             .collect_group_into(&mut *encoder)
     161            0 :             .unwrap_or_else(|infallible| match infallible {});
     162            0 :         self.metrics_group
     163            0 :             .collect_group_into(&mut *encoder)
     164            0 :             .unwrap_or_else(|infallible| match infallible {});
     165            0 :         encoder.finish()
     166            0 :     }
     167              : }
     168              : 
     169              : impl Default for StorageControllerMetrics {
     170           17 :     fn default() -> Self {
     171           17 :         let mut metrics_group = StorageControllerMetricGroup::new();
     172           17 :         metrics_group
     173           17 :             .storage_controller_reconcile_complete
     174           17 :             .init_all_dense();
     175              : 
     176           17 :         Self {
     177           17 :             metrics_group,
     178           17 :             encoder: Mutex::new(measured::text::BufferedTextEncoder::new()),
     179           17 :         }
     180           17 :     }
     181              : }
     182              : 
     183              : #[derive(measured::LabelGroup, Clone)]
     184              : #[label(set = NodeLabelGroupSet)]
     185              : pub(crate) struct NodeLabelGroup<'a> {
     186              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     187              :     pub(crate) az: &'a str,
     188              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     189              :     pub(crate) node_id: &'a str,
     190              : }
     191              : 
     192              : #[derive(measured::LabelGroup)]
     193              : #[label(set = ReconcileCompleteLabelGroupSet)]
     194              : pub(crate) struct ReconcileCompleteLabelGroup {
     195              :     pub(crate) status: ReconcileOutcome,
     196              : }
     197              : 
     198              : #[derive(measured::LabelGroup)]
     199              : #[label(set = HttpRequestStatusLabelGroupSet)]
     200              : pub(crate) struct HttpRequestStatusLabelGroup<'a> {
     201              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     202              :     pub(crate) path: &'a str,
     203              :     pub(crate) method: Method,
     204              :     pub(crate) status: StatusCode,
     205              : }
     206              : 
     207              : #[derive(measured::LabelGroup)]
     208              : #[label(set = HttpRequestLatencyLabelGroupSet)]
     209              : pub(crate) struct HttpRequestLatencyLabelGroup<'a> {
     210              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     211              :     pub(crate) path: &'a str,
     212              :     pub(crate) method: Method,
     213              : }
     214              : 
     215              : #[derive(measured::LabelGroup, Clone)]
     216              : #[label(set = PageserverRequestLabelGroupSet)]
     217              : pub(crate) struct PageserverRequestLabelGroup<'a> {
     218              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     219              :     pub(crate) pageserver_id: &'a str,
     220              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     221              :     pub(crate) path: &'a str,
     222              :     pub(crate) method: Method,
     223              : }
     224              : 
     225              : #[derive(measured::LabelGroup, Clone)]
     226              : #[label(set = SafekeeperRequestLabelGroupSet)]
     227              : pub(crate) struct SafekeeperRequestLabelGroup<'a> {
     228              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     229              :     pub(crate) safekeeper_id: &'a str,
     230              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     231              :     pub(crate) path: &'a str,
     232              :     pub(crate) method: Method,
     233              : }
     234              : 
     235              : #[derive(measured::LabelGroup)]
     236              : #[label(set = DatabaseQueryErrorLabelGroupSet)]
     237              : pub(crate) struct DatabaseQueryErrorLabelGroup {
     238              :     pub(crate) error_type: DatabaseErrorLabel,
     239              :     pub(crate) operation: DatabaseOperation,
     240              : }
     241              : 
     242              : #[derive(measured::LabelGroup)]
     243              : #[label(set = DatabaseQueryLatencyLabelGroupSet)]
     244              : pub(crate) struct DatabaseQueryLatencyLabelGroup {
     245              :     pub(crate) operation: DatabaseOperation,
     246              : }
     247              : 
     248              : #[derive(measured::LabelGroup)]
     249              : #[label(set = LeadershipStatusGroupSet)]
     250              : pub(crate) struct LeadershipStatusGroup {
     251              :     pub(crate) status: LeadershipStatus,
     252              : }
     253              : 
     254              : #[derive(measured::LabelGroup, Clone)]
     255              : #[label(set = ReconcileLongRunningLabelGroupSet)]
     256              : pub(crate) struct ReconcileLongRunningLabelGroup<'a> {
     257              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     258              :     pub(crate) tenant_id: &'a str,
     259              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     260              :     pub(crate) shard_number: &'a str,
     261              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     262              :     pub(crate) sequence: &'a str,
     263              : }
     264              : 
     265              : #[derive(FixedCardinalityLabel, Clone, Copy)]
     266              : pub(crate) enum ReconcileOutcome {
     267              :     #[label(rename = "ok")]
     268              :     Success,
     269              :     Error,
     270              :     Cancel,
     271              : }
     272              : 
     273              : #[derive(FixedCardinalityLabel, Copy, Clone)]
     274              : pub(crate) enum Method {
     275              :     Get,
     276              :     Put,
     277              :     Post,
     278              :     Delete,
     279              :     Other,
     280              : }
     281              : 
     282              : #[derive(measured::LabelGroup, Clone)]
     283              : #[label(set = SafekeeperReconcilerLabelGroupSet)]
     284              : pub(crate) struct SafekeeperReconcilerLabelGroup<'a> {
     285              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     286              :     pub(crate) sk_az: &'a str,
     287              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     288              :     pub(crate) sk_node_id: &'a str,
     289              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     290              :     pub(crate) sk_hostname: &'a str,
     291              : }
     292              : 
     293              : impl From<hyper::Method> for Method {
     294            0 :     fn from(value: hyper::Method) -> Self {
     295            0 :         if value == hyper::Method::GET {
     296            0 :             Method::Get
     297            0 :         } else if value == hyper::Method::PUT {
     298            0 :             Method::Put
     299            0 :         } else if value == hyper::Method::POST {
     300            0 :             Method::Post
     301            0 :         } else if value == hyper::Method::DELETE {
     302            0 :             Method::Delete
     303              :         } else {
     304            0 :             Method::Other
     305              :         }
     306            0 :     }
     307              : }
     308              : 
     309              : #[derive(Clone, Copy)]
     310              : pub(crate) struct StatusCode(pub(crate) hyper::http::StatusCode);
     311              : 
     312              : impl LabelValue for StatusCode {
     313            0 :     fn visit<V: measured::label::LabelVisitor>(&self, v: V) -> V::Output {
     314            0 :         v.write_int(self.0.as_u16() as i64)
     315            0 :     }
     316              : }
     317              : 
     318              : impl FixedCardinalityLabel for StatusCode {
     319            0 :     fn cardinality() -> usize {
     320            0 :         (100..1000).len()
     321            0 :     }
     322              : 
     323            0 :     fn encode(&self) -> usize {
     324            0 :         self.0.as_u16() as usize
     325            0 :     }
     326              : 
     327            0 :     fn decode(value: usize) -> Self {
     328            0 :         Self(hyper::http::StatusCode::from_u16(u16::try_from(value).unwrap()).unwrap())
     329            0 :     }
     330              : }
     331              : 
     332              : #[derive(FixedCardinalityLabel, Clone, Copy)]
     333              : pub(crate) enum DatabaseErrorLabel {
     334              :     Query,
     335              :     Connection,
     336              :     ConnectionPool,
     337              :     Logical,
     338              :     Migration,
     339              :     Cas,
     340              : }
     341              : 
     342              : impl DatabaseError {
     343            0 :     pub(crate) fn error_label(&self) -> DatabaseErrorLabel {
     344            0 :         match self {
     345            0 :             Self::Query(_) => DatabaseErrorLabel::Query,
     346            0 :             Self::Connection(_) => DatabaseErrorLabel::Connection,
     347            0 :             Self::ConnectionPool(_) => DatabaseErrorLabel::ConnectionPool,
     348            0 :             Self::Logical(_) => DatabaseErrorLabel::Logical,
     349            0 :             Self::Migration(_) => DatabaseErrorLabel::Migration,
     350            0 :             Self::Cas(_) => DatabaseErrorLabel::Cas,
     351              :         }
     352            0 :     }
     353              : }
     354              : 
     355              : /// Update the leadership status metric gauges to reflect the requested status
     356            0 : pub(crate) fn update_leadership_status(status: LeadershipStatus) {
     357            0 :     let status_metric = &METRICS_REGISTRY
     358            0 :         .metrics_group
     359            0 :         .storage_controller_leadership_status;
     360              : 
     361            0 :     for s in LeadershipStatus::iter() {
     362            0 :         if s == status {
     363            0 :             status_metric.set(LeadershipStatusGroup { status: s }, 1);
     364            0 :         } else {
     365            0 :             status_metric.set(LeadershipStatusGroup { status: s }, 0);
     366            0 :         }
     367              :     }
     368            0 : }
        

Generated by: LCOV version 2.1-beta