Skip to content

Commit

Permalink
add pgrst_pool_available gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-chavez committed Apr 18, 2024
1 parent 1a57b29 commit 6e83081
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/PostgREST/Metrics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,37 @@ 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

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 ()

Expand Down

0 comments on commit 6e83081

Please sign in to comment.