LCOV - code coverage report
Current view: top level - storage_controller/src - metrics.rs (source / functions) Coverage Total Hit
Test: 727bdccc1d7d53837da843959afb612f56da4e79.info Lines: 27.6 % 76 21
Test Date: 2025-01-30 15:18:43 Functions: 73.5 % 34 25

            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 bytes::Bytes;
      11              : use measured::{label::LabelValue, metric::histogram, FixedCardinalityLabel, MetricGroup};
      12              : use metrics::NeonMetrics;
      13              : use once_cell::sync::Lazy;
      14              : use std::sync::Mutex;
      15              : use strum::IntoEnumIterator;
      16              : 
      17              : use crate::{
      18              :     persistence::{DatabaseError, DatabaseOperation},
      19              :     service::LeadershipStatus,
      20              : };
      21              : 
      22              : pub(crate) static METRICS_REGISTRY: Lazy<StorageControllerMetrics> =
      23              :     Lazy::new(StorageControllerMetrics::default);
      24              : 
      25            0 : pub fn preinitialize_metrics() {
      26            0 :     Lazy::force(&METRICS_REGISTRY);
      27            0 : }
      28              : 
      29              : pub(crate) struct StorageControllerMetrics {
      30              :     pub(crate) metrics_group: StorageControllerMetricGroup,
      31              :     encoder: Mutex<measured::text::BufferedTextEncoder>,
      32              : }
      33              : 
      34           13 : #[derive(measured::MetricGroup)]
      35              : #[metric(new())]
      36              : pub(crate) struct StorageControllerMetricGroup {
      37              :     /// Count of how many times we spawn a reconcile task
      38              :     pub(crate) storage_controller_reconcile_spawn: measured::Counter,
      39              : 
      40              :     /// Size of the in-memory map of tenant shards
      41              :     pub(crate) storage_controller_tenant_shards: measured::Gauge,
      42              : 
      43              :     /// Size of the in-memory map of pageserver_nodes
      44              :     pub(crate) storage_controller_pageserver_nodes: measured::Gauge,
      45              : 
      46              :     /// Reconciler tasks completed, broken down by success/failure/cancelled
      47              :     pub(crate) storage_controller_reconcile_complete:
      48              :         measured::CounterVec<ReconcileCompleteLabelGroupSet>,
      49              : 
      50              :     /// Count of how many times we make an optimization change to a tenant's scheduling
      51              :     pub(crate) storage_controller_schedule_optimization: measured::Counter,
      52              : 
      53              :     /// How many shards are not scheduled into their preferred AZ
      54              :     pub(crate) storage_controller_schedule_az_violation: measured::Gauge,
      55              : 
      56              :     /// How many shard locations (secondary or attached) on each node
      57              :     pub(crate) storage_controller_node_shards: measured::GaugeVec<NodeLabelGroupSet>,
      58              : 
      59              :     /// How many _attached_ shard locations on each node
      60              :     pub(crate) storage_controller_node_attached_shards: measured::GaugeVec<NodeLabelGroupSet>,
      61              : 
      62              :     /// How many _home_ shard locations on each node (i.e. the node's AZ matches the shard's
      63              :     /// preferred AZ)
      64              :     pub(crate) storage_controller_node_home_shards: measured::GaugeVec<NodeLabelGroupSet>,
      65              : 
      66              :     /// How many shards would like to reconcile but were blocked by concurrency limits
      67              :     pub(crate) storage_controller_pending_reconciles: measured::Gauge,
      68              : 
      69              :     /// HTTP request status counters for handled requests
      70              :     pub(crate) storage_controller_http_request_status:
      71              :         measured::CounterVec<HttpRequestStatusLabelGroupSet>,
      72              : 
      73              :     /// HTTP request handler latency across all status codes
      74              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
      75              :     pub(crate) storage_controller_http_request_latency:
      76              :         measured::HistogramVec<HttpRequestLatencyLabelGroupSet, 5>,
      77              : 
      78              :     /// Count of HTTP requests to the pageserver that resulted in an error,
      79              :     /// broken down by the pageserver node id, request name and method
      80              :     pub(crate) storage_controller_pageserver_request_error:
      81              :         measured::CounterVec<PageserverRequestLabelGroupSet>,
      82              : 
      83              :     /// Latency of HTTP requests to the pageserver, broken down by pageserver
      84              :     /// node id, request name and method. This include both successful and unsuccessful
      85              :     /// requests.
      86              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
      87              :     pub(crate) storage_controller_pageserver_request_latency:
      88              :         measured::HistogramVec<PageserverRequestLabelGroupSet, 5>,
      89              : 
      90              :     /// Count of pass-through HTTP requests to the pageserver that resulted in an error,
      91              :     /// broken down by the pageserver node id, request name and method
      92              :     pub(crate) storage_controller_passthrough_request_error:
      93              :         measured::CounterVec<PageserverRequestLabelGroupSet>,
      94              : 
      95              :     /// Latency of pass-through HTTP requests to the pageserver, broken down by pageserver
      96              :     /// node id, request name and method. This include both successful and unsuccessful
      97              :     /// requests.
      98              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
      99              :     pub(crate) storage_controller_passthrough_request_latency:
     100              :         measured::HistogramVec<PageserverRequestLabelGroupSet, 5>,
     101              : 
     102              :     /// Count of errors in database queries, broken down by error type and operation.
     103              :     pub(crate) storage_controller_database_query_error:
     104              :         measured::CounterVec<DatabaseQueryErrorLabelGroupSet>,
     105              : 
     106              :     /// Latency of database queries, broken down by operation.
     107              :     #[metric(metadata = histogram::Thresholds::exponential_buckets(0.1, 2.0))]
     108              :     pub(crate) storage_controller_database_query_latency:
     109              :         measured::HistogramVec<DatabaseQueryLatencyLabelGroupSet, 5>,
     110              : 
     111              :     pub(crate) storage_controller_leadership_status: measured::GaugeVec<LeadershipStatusGroupSet>,
     112              : 
     113              :     /// HTTP request status counters for handled requests
     114              :     pub(crate) storage_controller_reconcile_long_running:
     115              :         measured::CounterVec<ReconcileLongRunningLabelGroupSet>,
     116              : }
     117              : 
     118              : impl StorageControllerMetrics {
     119            0 :     pub(crate) fn encode(&self, neon_metrics: &NeonMetrics) -> Bytes {
     120            0 :         let mut encoder = self.encoder.lock().unwrap();
     121            0 :         neon_metrics
     122            0 :             .collect_group_into(&mut *encoder)
     123            0 :             .unwrap_or_else(|infallible| match infallible {});
     124            0 :         self.metrics_group
     125            0 :             .collect_group_into(&mut *encoder)
     126            0 :             .unwrap_or_else(|infallible| match infallible {});
     127            0 :         encoder.finish()
     128            0 :     }
     129              : }
     130              : 
     131              : impl Default for StorageControllerMetrics {
     132           13 :     fn default() -> Self {
     133           13 :         let mut metrics_group = StorageControllerMetricGroup::new();
     134           13 :         metrics_group
     135           13 :             .storage_controller_reconcile_complete
     136           13 :             .init_all_dense();
     137           13 : 
     138           13 :         Self {
     139           13 :             metrics_group,
     140           13 :             encoder: Mutex::new(measured::text::BufferedTextEncoder::new()),
     141           13 :         }
     142           13 :     }
     143              : }
     144              : 
     145           78 : #[derive(measured::LabelGroup, Clone)]
     146              : #[label(set = NodeLabelGroupSet)]
     147              : pub(crate) struct NodeLabelGroup<'a> {
     148              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     149              :     pub(crate) az: &'a str,
     150              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     151              :     pub(crate) node_id: &'a str,
     152              : }
     153              : 
     154           39 : #[derive(measured::LabelGroup)]
     155              : #[label(set = ReconcileCompleteLabelGroupSet)]
     156              : pub(crate) struct ReconcileCompleteLabelGroup {
     157              :     pub(crate) status: ReconcileOutcome,
     158              : }
     159              : 
     160           26 : #[derive(measured::LabelGroup)]
     161              : #[label(set = HttpRequestStatusLabelGroupSet)]
     162              : pub(crate) struct HttpRequestStatusLabelGroup<'a> {
     163              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     164              :     pub(crate) path: &'a str,
     165              :     pub(crate) method: Method,
     166              :     pub(crate) status: StatusCode,
     167              : }
     168              : 
     169           26 : #[derive(measured::LabelGroup)]
     170              : #[label(set = HttpRequestLatencyLabelGroupSet)]
     171              : pub(crate) struct HttpRequestLatencyLabelGroup<'a> {
     172              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     173              :     pub(crate) path: &'a str,
     174              :     pub(crate) method: Method,
     175              : }
     176              : 
     177          104 : #[derive(measured::LabelGroup, Clone)]
     178              : #[label(set = PageserverRequestLabelGroupSet)]
     179              : pub(crate) struct PageserverRequestLabelGroup<'a> {
     180              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     181              :     pub(crate) pageserver_id: &'a str,
     182              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     183              :     pub(crate) path: &'a str,
     184              :     pub(crate) method: Method,
     185              : }
     186              : 
     187           52 : #[derive(measured::LabelGroup)]
     188              : #[label(set = DatabaseQueryErrorLabelGroupSet)]
     189              : pub(crate) struct DatabaseQueryErrorLabelGroup {
     190              :     pub(crate) error_type: DatabaseErrorLabel,
     191              :     pub(crate) operation: DatabaseOperation,
     192              : }
     193              : 
     194           39 : #[derive(measured::LabelGroup)]
     195              : #[label(set = DatabaseQueryLatencyLabelGroupSet)]
     196              : pub(crate) struct DatabaseQueryLatencyLabelGroup {
     197              :     pub(crate) operation: DatabaseOperation,
     198              : }
     199              : 
     200           39 : #[derive(measured::LabelGroup)]
     201              : #[label(set = LeadershipStatusGroupSet)]
     202              : pub(crate) struct LeadershipStatusGroup {
     203              :     pub(crate) status: LeadershipStatus,
     204              : }
     205              : 
     206           26 : #[derive(measured::LabelGroup, Clone)]
     207              : #[label(set = ReconcileLongRunningLabelGroupSet)]
     208              : pub(crate) struct ReconcileLongRunningLabelGroup<'a> {
     209              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     210              :     pub(crate) tenant_id: &'a str,
     211              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     212              :     pub(crate) shard_number: &'a str,
     213              :     #[label(dynamic_with = lasso::ThreadedRodeo, default)]
     214              :     pub(crate) sequence: &'a str,
     215              : }
     216              : 
     217              : #[derive(FixedCardinalityLabel, Clone, Copy)]
     218              : pub(crate) enum ReconcileOutcome {
     219              :     #[label(rename = "ok")]
     220              :     Success,
     221              :     Error,
     222              :     Cancel,
     223              : }
     224              : 
     225              : #[derive(FixedCardinalityLabel, Copy, Clone)]
     226              : pub(crate) enum Method {
     227              :     Get,
     228              :     Put,
     229              :     Post,
     230              :     Delete,
     231              :     Other,
     232              : }
     233              : 
     234              : impl From<hyper::Method> for Method {
     235            0 :     fn from(value: hyper::Method) -> Self {
     236            0 :         if value == hyper::Method::GET {
     237            0 :             Method::Get
     238            0 :         } else if value == hyper::Method::PUT {
     239            0 :             Method::Put
     240            0 :         } else if value == hyper::Method::POST {
     241            0 :             Method::Post
     242            0 :         } else if value == hyper::Method::DELETE {
     243            0 :             Method::Delete
     244              :         } else {
     245            0 :             Method::Other
     246              :         }
     247            0 :     }
     248              : }
     249              : 
     250              : #[derive(Clone, Copy)]
     251              : pub(crate) struct StatusCode(pub(crate) hyper::http::StatusCode);
     252              : 
     253              : impl LabelValue for StatusCode {
     254            0 :     fn visit<V: measured::label::LabelVisitor>(&self, v: V) -> V::Output {
     255            0 :         v.write_int(self.0.as_u16() as i64)
     256            0 :     }
     257              : }
     258              : 
     259              : impl FixedCardinalityLabel for StatusCode {
     260            0 :     fn cardinality() -> usize {
     261            0 :         (100..1000).len()
     262            0 :     }
     263              : 
     264            0 :     fn encode(&self) -> usize {
     265            0 :         self.0.as_u16() as usize
     266            0 :     }
     267              : 
     268            0 :     fn decode(value: usize) -> Self {
     269            0 :         Self(hyper::http::StatusCode::from_u16(u16::try_from(value).unwrap()).unwrap())
     270            0 :     }
     271              : }
     272              : 
     273              : #[derive(FixedCardinalityLabel, Clone, Copy)]
     274              : pub(crate) enum DatabaseErrorLabel {
     275              :     Query,
     276              :     Connection,
     277              :     ConnectionPool,
     278              :     Logical,
     279              :     Migration,
     280              : }
     281              : 
     282              : impl DatabaseError {
     283            0 :     pub(crate) fn error_label(&self) -> DatabaseErrorLabel {
     284            0 :         match self {
     285            0 :             Self::Query(_) => DatabaseErrorLabel::Query,
     286            0 :             Self::Connection(_) => DatabaseErrorLabel::Connection,
     287            0 :             Self::ConnectionPool(_) => DatabaseErrorLabel::ConnectionPool,
     288            0 :             Self::Logical(_) => DatabaseErrorLabel::Logical,
     289            0 :             Self::Migration(_) => DatabaseErrorLabel::Migration,
     290              :         }
     291            0 :     }
     292              : }
     293              : 
     294              : /// Update the leadership status metric gauges to reflect the requested status
     295            0 : pub(crate) fn update_leadership_status(status: LeadershipStatus) {
     296            0 :     let status_metric = &METRICS_REGISTRY
     297            0 :         .metrics_group
     298            0 :         .storage_controller_leadership_status;
     299              : 
     300            0 :     for s in LeadershipStatus::iter() {
     301            0 :         if s == status {
     302            0 :             status_metric.set(LeadershipStatusGroup { status: s }, 1);
     303            0 :         } else {
     304            0 :             status_metric.set(LeadershipStatusGroup { status: s }, 0);
     305            0 :         }
     306              :     }
     307            0 : }
        

Generated by: LCOV version 2.1-beta