diff --git a/src/PostgREST/Metrics.hs b/src/PostgREST/Metrics.hs index 6f40696abc..c802f9717e 100644 --- a/src/PostgREST/Metrics.hs +++ b/src/PostgREST/Metrics.hs @@ -6,7 +6,8 @@ module PostgREST.Metrics , metricsToText ) where -import qualified Data.ByteString.Lazy as LBS +import qualified Data.ByteString.Lazy as LBS +import qualified Hasql.Pool.Observation as SQL import qualified Prometheus as Prom @@ -14,19 +15,28 @@ import PostgREST.Observation import Protolude -newtype MetricsState = MetricsState - { counterPoolTimeout :: Prom.Counter +data MetricsState = MetricsState + { poolTimeouts :: Prom.Counter + , poolAvailable :: Prom.Gauge } init :: IO MetricsState init = do - counter <- Prom.register $ Prom.counter (Prom.Info "pgrst_pool_timeouts" "The number of connection timeouts in the pool") - pure $ MetricsState counter + counter <- Prom.register $ Prom.counter (Prom.Info "pgrst_pool_timeouts_total" "The number of connection timeouts in the pool") + gauge <- Prom.register $ Prom.gauge (Prom.Info "pgrst_pool_available" "The number of unused connections in the pool") + Prom.setGauge gauge 0 + pure $ MetricsState counter gauge observationMetrics :: MetricsState -> ObservationHandler -observationMetrics MetricsState{counterPoolTimeout} obs = case obs of +observationMetrics MetricsState{poolTimeouts, poolAvailable} obs = case obs of (PoolAcqTimeoutObs _) -> do - Prom.incCounter counterPoolTimeout + Prom.incCounter poolTimeouts + (HasqlPoolObs (SQL.ConnectionObservation _ status)) -> case status of + SQL.ReadyForUseConnectionStatus -> do + Prom.incGauge poolAvailable + SQL.InUseConnectionStatus -> do + Prom.decGauge poolAvailable + _ -> pure () _ -> pure ()