LCOV - code coverage report
Current view: top level - storage_controller/src - metrics.rs (source / functions) Coverage Total Hit
Test: 553e39c2773e5840c720c90d86e56f89a4330d43.info Lines: 28.6 % 77 22
Test Date: 2025-06-13 20:01:21 Functions: 75.0 % 36 27

            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           17 : #[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              :     /// HTTP request status counters for handled requests
      80              :     pub(crate) storage_controller_http_request_status:
      81              :         measured::CounterVec<HttpRequestStatusLabelGroupSet>,
      82              : 
      83              :     /// HTTP request handler latency across all status codes
      84              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
      85              :     pub(crate) storage_controller_http_request_latency:
      86              :         measured::HistogramVec<HttpRequestLatencyLabelGroupSet, 5>,
      87              : 
      88              :     /// HTTP rate limiting latency across all tenants and endpoints
      89              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 10.0))]
      90              :     pub(crate) storage_controller_http_request_rate_limited: measured::Histogram<10>,
      91              : 
      92              :     /// Count of HTTP requests to the pageserver that resulted in an error,
      93              :     /// broken down by the pageserver node id, request name and method
      94              :     pub(crate) storage_controller_pageserver_request_error:
      95              :         measured::CounterVec<PageserverRequestLabelGroupSet>,
      96              : 
      97              :     /// Count of HTTP requests to the safekeeper that resulted in an error,
      98              :     /// broken down by the safekeeper node id, request name and method
      99              :     pub(crate) storage_controller_safekeeper_request_error:
     100              :         measured::CounterVec<PageserverRequestLabelGroupSet>,
     101              : 
     102              :     /// Latency of HTTP requests to the pageserver, broken down by pageserver
     103              :     /// node id, request name and method. This include both successful and unsuccessful
     104              :     /// requests.
     105              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     106              :     pub(crate) storage_controller_pageserver_request_latency:
     107              :         measured::HistogramVec<PageserverRequestLabelGroupSet, 5>,
     108              : 
     109              :     /// Latency of HTTP requests to the safekeeper, broken down by safekeeper
     110              :     /// node id, request name and method. This include both successful and unsuccessful
     111              :     /// requests.
     112              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     113              :     pub(crate) storage_controller_safekeeper_request_latency:
     114              :         measured::HistogramVec<PageserverRequestLabelGroupSet, 5>,
     115              : 
     116              :     /// Count of pass-through HTTP requests to the pageserver that resulted in an error,
     117              :     /// broken down by the pageserver node id, request name and method
     118              :     pub(crate) storage_controller_passthrough_request_error:
     119              :         measured::CounterVec<PageserverRequestLabelGroupSet>,
     120              : 
     121              :     /// Latency of pass-through HTTP requests to the pageserver, broken down by pageserver
     122              :     /// node id, request name and method. This include both successful and unsuccessful
     123              :     /// requests.
     124              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     125              :     pub(crate) storage_controller_passthrough_request_latency:
     126              :         measured::HistogramVec<PageserverRequestLabelGroupSet, 5>,
     127              : 
     128              :     /// Count of errors in database queries, broken down by error type and operation.
     129              :     pub(crate) storage_controller_database_query_error:
     130              :         measured::CounterVec<DatabaseQueryErrorLabelGroupSet>,
     131              : 
     132              :     /// Latency of database queries, broken down by operation.
     133              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     134              :     pub(crate) storage_controller_database_query_latency:
     135              :         measured::HistogramVec<DatabaseQueryLatencyLabelGroupSet, 5>,
     136              : 
     137              :     pub(crate) storage_controller_leadership_status: measured::GaugeVec<LeadershipStatusGroupSet>,
     138              : 
     139              :     /// HTTP request status counters for handled requests
     140              :     pub(crate) storage_controller_reconcile_long_running:
     141              :         measured::CounterVec<ReconcileLongRunningLabelGroupSet>,
     142              : 
     143              :     /// Indicator of safekeeper reconciler queue depth, broken down by safekeeper, excluding ongoing reconciles.
     144              :     pub(crate) storage_controller_safekeeper_reconciles_queued:
     145              :         measured::GaugeVec<SafekeeperReconcilerLabelGroupSet>,
     146              : 
     147              :     /// Indicator of completed safekeeper reconciles, broken down by safekeeper.
     148              :     pub(crate) storage_controller_safekeeper_reconciles_complete:
     149              :         measured::CounterVec<SafekeeperReconcilerLabelGroupSet>,
     150              : }
     151              : 
     152              : impl StorageControllerMetrics {
     153            0 :     pub(crate) fn encode(&self, neon_metrics: &NeonMetrics) -> Bytes {
     154            0 :         let mut encoder = self.encoder.lock().unwrap();
     155            0 :         neon_metrics
     156            0 :             .collect_group_into(&mut *encoder)
     157            0 :             .unwrap_or_else(|infallible| match infallible {});
     158            0 :         self.metrics_group
     159            0 :             .collect_group_into(&mut *encoder)
     160            0 :             .unwrap_or_else(|infallible| match infallible {});
     161            0 :         encoder.finish()
     162            0 :     }
     163              : }
     164              : 
     165              : impl Default for StorageControllerMetrics {
     166           17 :     fn default() -> Self {
     167           17 :         let mut metrics_group = StorageControllerMetricGroup::new();
     168           17 :         metrics_group
     169           17 :             .storage_controller_reconcile_complete
     170           17 :             .init_all_dense();
     171           17 : 
     172           17 :         Self {
     173           17 :             metrics_group,
     174           17 :             encoder: Mutex::new(measured::text::BufferedTextEncoder::new()),
     175           17 :         }
     176           17 :     }
     177              : }
     178              : 
     179          102 : #[derive(measured::LabelGroup, Clone)]
     180              : #[label(set = NodeLabelGroupSet)]
     181              : pub(crate) struct NodeLabelGroup<'a> {
     182              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     183              :     pub(crate) az: &'a str,
     184              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     185              :     pub(crate) node_id: &'a str,
     186              : }
     187              : 
     188           51 : #[derive(measured::LabelGroup)]
     189              : #[label(set = ReconcileCompleteLabelGroupSet)]
     190              : pub(crate) struct ReconcileCompleteLabelGroup {
     191              :     pub(crate) status: ReconcileOutcome,
     192              : }
     193              : 
     194           34 : #[derive(measured::LabelGroup)]
     195              : #[label(set = HttpRequestStatusLabelGroupSet)]
     196              : pub(crate) struct HttpRequestStatusLabelGroup<'a> {
     197              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     198              :     pub(crate) path: &'a str,
     199              :     pub(crate) method: Method,
     200              :     pub(crate) status: StatusCode,
     201              : }
     202              : 
     203           34 : #[derive(measured::LabelGroup)]
     204              : #[label(set = HttpRequestLatencyLabelGroupSet)]
     205              : pub(crate) struct HttpRequestLatencyLabelGroup<'a> {
     206              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     207              :     pub(crate) path: &'a str,
     208              :     pub(crate) method: Method,
     209              : }
     210              : 
     211          204 : #[derive(measured::LabelGroup, Clone)]
     212              : #[label(set = PageserverRequestLabelGroupSet)]
     213              : pub(crate) struct PageserverRequestLabelGroup<'a> {
     214              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     215              :     pub(crate) pageserver_id: &'a str,
     216              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     217              :     pub(crate) path: &'a str,
     218              :     pub(crate) method: Method,
     219              : }
     220              : 
     221           68 : #[derive(measured::LabelGroup)]
     222              : #[label(set = DatabaseQueryErrorLabelGroupSet)]
     223              : pub(crate) struct DatabaseQueryErrorLabelGroup {
     224              :     pub(crate) error_type: DatabaseErrorLabel,
     225              :     pub(crate) operation: DatabaseOperation,
     226              : }
     227              : 
     228           51 : #[derive(measured::LabelGroup)]
     229              : #[label(set = DatabaseQueryLatencyLabelGroupSet)]
     230              : pub(crate) struct DatabaseQueryLatencyLabelGroup {
     231              :     pub(crate) operation: DatabaseOperation,
     232              : }
     233              : 
     234           51 : #[derive(measured::LabelGroup)]
     235              : #[label(set = LeadershipStatusGroupSet)]
     236              : pub(crate) struct LeadershipStatusGroup {
     237              :     pub(crate) status: LeadershipStatus,
     238              : }
     239              : 
     240           34 : #[derive(measured::LabelGroup, Clone)]
     241              : #[label(set = ReconcileLongRunningLabelGroupSet)]
     242              : pub(crate) struct ReconcileLongRunningLabelGroup<'a> {
     243              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     244              :     pub(crate) tenant_id: &'a str,
     245              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     246              :     pub(crate) shard_number: &'a str,
     247              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     248              :     pub(crate) sequence: &'a str,
     249              : }
     250              : 
     251              : #[derive(FixedCardinalityLabel, Clone, Copy)]
     252              : pub(crate) enum ReconcileOutcome {
     253              :     #[label(rename = "ok")]
     254              :     Success,
     255              :     Error,
     256              :     Cancel,
     257              : }
     258              : 
     259              : #[derive(FixedCardinalityLabel, Copy, Clone)]
     260              : pub(crate) enum Method {
     261              :     Get,
     262              :     Put,
     263              :     Post,
     264              :     Delete,
     265              :     Other,
     266              : }
     267              : 
     268           68 : #[derive(measured::LabelGroup, Clone)]
     269              : #[label(set = SafekeeperReconcilerLabelGroupSet)]
     270              : pub(crate) struct SafekeeperReconcilerLabelGroup<'a> {
     271              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     272              :     pub(crate) sk_az: &'a str,
     273              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     274              :     pub(crate) sk_node_id: &'a str,
     275              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     276              :     pub(crate) sk_hostname: &'a str,
     277              : }
     278              : 
     279              : impl From<hyper::Method> for Method {
     280            0 :     fn from(value: hyper::Method) -> Self {
     281            0 :         if value == hyper::Method::GET {
     282            0 :             Method::Get
     283            0 :         } else if value == hyper::Method::PUT {
     284            0 :             Method::Put
     285            0 :         } else if value == hyper::Method::POST {
     286            0 :             Method::Post
     287            0 :         } else if value == hyper::Method::DELETE {
     288            0 :             Method::Delete
     289              :         } else {
     290            0 :             Method::Other
     291              :         }
     292            0 :     }
     293              : }
     294              : 
     295              : #[derive(Clone, Copy)]
     296              : pub(crate) struct StatusCode(pub(crate) hyper::http::StatusCode);
     297              : 
     298              : impl LabelValue for StatusCode {
     299            0 :     fn visit<V: measured::label::LabelVisitor>(&self, v: V) -> V::Output {
     300            0 :         v.write_int(self.0.as_u16() as i64)
     301            0 :     }
     302              : }
     303              : 
     304              : impl FixedCardinalityLabel for StatusCode {
     305            0 :     fn cardinality() -> usize {
     306            0 :         (100..1000).len()
     307            0 :     }
     308              : 
     309            0 :     fn encode(&self) -> usize {
     310            0 :         self.0.as_u16() as usize
     311            0 :     }
     312              : 
     313            0 :     fn decode(value: usize) -> Self {
     314            0 :         Self(hyper::http::StatusCode::from_u16(u16::try_from(value).unwrap()).unwrap())
     315            0 :     }
     316              : }
     317              : 
     318              : #[derive(FixedCardinalityLabel, Clone, Copy)]
     319              : pub(crate) enum DatabaseErrorLabel {
     320              :     Query,
     321              :     Connection,
     322              :     ConnectionPool,
     323              :     Logical,
     324              :     Migration,
     325              : }
     326              : 
     327              : impl DatabaseError {
     328            0 :     pub(crate) fn error_label(&self) -> DatabaseErrorLabel {
     329            0 :         match self {
     330            0 :             Self::Query(_) => DatabaseErrorLabel::Query,
     331            0 :             Self::Connection(_) => DatabaseErrorLabel::Connection,
     332            0 :             Self::ConnectionPool(_) => DatabaseErrorLabel::ConnectionPool,
     333            0 :             Self::Logical(_) => DatabaseErrorLabel::Logical,
     334            0 :             Self::Migration(_) => DatabaseErrorLabel::Migration,
     335              :         }
     336            0 :     }
     337              : }
     338              : 
     339              : /// Update the leadership status metric gauges to reflect the requested status
     340            0 : pub(crate) fn update_leadership_status(status: LeadershipStatus) {
     341            0 :     let status_metric = &METRICS_REGISTRY
     342            0 :         .metrics_group
     343            0 :         .storage_controller_leadership_status;
     344              : 
     345            0 :     for s in LeadershipStatus::iter() {
     346            0 :         if s == status {
     347            0 :             status_metric.set(LeadershipStatusGroup { status: s }, 1);
     348            0 :         } else {
     349            0 :             status_metric.set(LeadershipStatusGroup { status: s }, 0);
     350            0 :         }
     351              :     }
     352            0 : }
        

Generated by: LCOV version 2.1-beta