-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: connection pool metrics in admin server
- Loading branch information
1 parent
3a33a09
commit 3c61645
Showing
8 changed files
with
102 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE NumericUnderscores #-} | ||
module PostgREST.Metrics | ||
( init | ||
, MetricsState (..) | ||
, observationMetrics | ||
, metricsToText | ||
) where | ||
|
||
import qualified Data.ByteString.Lazy as LBS | ||
import qualified Hasql.Pool.Observation as SQL | ||
|
||
import qualified Prometheus as Prom | ||
|
||
import PostgREST.Observation | ||
|
||
import Protolude | ||
|
||
data MetricsState = MetricsState | ||
{ poolTimeouts :: Prom.Counter | ||
, poolAvailable :: Prom.Gauge | ||
, poolWaiting :: Prom.Gauge | ||
, poolMaxSize :: Prom.Gauge | ||
} | ||
|
||
init :: Int -> IO MetricsState | ||
init poolMaxSize = do | ||
timeouts <- Prom.register $ Prom.counter (Prom.Info "pgrst_db_pool_timeouts_total" "The total number of pool connection timeouts") | ||
available <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_available" "Available connections in the pool") | ||
waiting <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_waiting" "Requests waiting to acquire a pool connection") | ||
maxSize <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_max" "Max pool connections") | ||
Prom.setGauge maxSize (fromIntegral poolMaxSize) | ||
pure $ MetricsState timeouts available waiting maxSize | ||
|
||
observationMetrics :: MetricsState -> ObservationHandler | ||
observationMetrics MetricsState{poolTimeouts, poolAvailable, poolWaiting} obs = case obs of | ||
(PoolAcqTimeoutObs _) -> do | ||
Prom.incCounter poolTimeouts | ||
(HasqlPoolObs (SQL.ConnectionObservation _ status)) -> case status of | ||
SQL.ReadyForUseConnectionStatus -> do | ||
Prom.incGauge poolAvailable | ||
SQL.InUseConnectionStatus -> do | ||
Prom.decGauge poolAvailable | ||
SQL.TerminatedConnectionStatus _ -> do | ||
Prom.decGauge poolAvailable | ||
SQL.ConnectingConnectionStatus -> pure () | ||
PoolRequest -> | ||
Prom.incGauge poolWaiting | ||
PoolRequestFullfilled -> | ||
Prom.decGauge poolWaiting | ||
_ -> | ||
pure () | ||
|
||
metricsToText :: IO LBS.ByteString | ||
metricsToText = Prom.exportMetricsAsText |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters