Define MetricsState as record and use Applicative to initialize one

This commit is contained in:
Michał Kłeczek
2025-06-02 11:49:01 -05:00
committed by Steve Chavez
parent 9084d8c358
commit 7e3fb2ba08
+19 -10
View File
@@ -1,3 +1,4 @@
{-# LANGUAGE RecordWildCards #-}
{-| {-|
Module : PostgREST.Logger Module : PostgREST.Logger
Description : Metrics based on the Observation module. See Observation.hs. Description : Metrics based on the Observation module. See Observation.hs.
@@ -19,22 +20,30 @@ import PostgREST.Observation
import Protolude import Protolude
data MetricsState = data MetricsState =
MetricsState Counter Gauge Gauge Gauge (Vector Label1 Counter) Gauge MetricsState {
poolTimeouts :: Counter,
poolAvailable :: Gauge,
poolWaiting :: Gauge,
poolMaxSize :: Gauge,
schemaCacheLoads :: Vector Label1 Counter,
schemaCacheQueryTime :: Gauge
}
init :: Int -> IO MetricsState init :: Int -> IO MetricsState
init configDbPoolSize = do init configDbPoolSize = do
poolTimeouts <- register $ counter (Info "pgrst_db_pool_timeouts_total" "The total number of pool connection timeouts") metricState <- MetricsState <$>
poolAvailable <- register $ gauge (Info "pgrst_db_pool_available" "Available connections in the pool") register (counter (Info "pgrst_db_pool_timeouts_total" "The total number of pool connection timeouts")) <*>
poolWaiting <- register $ gauge (Info "pgrst_db_pool_waiting" "Requests waiting to acquire a pool connection") register (gauge (Info "pgrst_db_pool_available" "Available connections in the pool")) <*>
poolMaxSize <- register $ gauge (Info "pgrst_db_pool_max" "Max pool connections") register (gauge (Info "pgrst_db_pool_waiting" "Requests waiting to acquire a pool connection")) <*>
schemaCacheLoads <- register $ vector "status" $ counter (Info "pgrst_schema_cache_loads_total" "The total number of times the schema cache was loaded") register (gauge (Info "pgrst_db_pool_max" "Max pool connections")) <*>
schemaCacheQueryTime <- register $ gauge (Info "pgrst_schema_cache_query_time_seconds" "The query time in seconds of the last schema cache load") register (vector "status" $ counter (Info "pgrst_schema_cache_loads_total" "The total number of times the schema cache was loaded")) <*>
setGauge poolMaxSize (fromIntegral configDbPoolSize) register (gauge (Info "pgrst_schema_cache_query_time_seconds" "The query time in seconds of the last schema cache load"))
pure $ MetricsState poolTimeouts poolAvailable poolWaiting poolMaxSize schemaCacheLoads schemaCacheQueryTime setGauge (poolMaxSize metricState) (fromIntegral configDbPoolSize)
pure metricState
-- Only some observations are used as metrics -- Only some observations are used as metrics
observationMetrics :: MetricsState -> ObservationHandler observationMetrics :: MetricsState -> ObservationHandler
observationMetrics (MetricsState poolTimeouts poolAvailable poolWaiting _ schemaCacheLoads schemaCacheQueryTime) obs = case obs of observationMetrics MetricsState{..} obs = case obs of
(PoolAcqTimeoutObs _) -> do (PoolAcqTimeoutObs _) -> do
incCounter poolTimeouts incCounter poolTimeouts
(HasqlPoolObs (SQL.ConnectionObservation _ status)) -> case status of (HasqlPoolObs (SQL.ConnectionObservation _ status)) -> case status of