Skip to content

Commit

Permalink
feat: schema cache metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-chavez committed Apr 20, 2024
1 parent 3c61645 commit 91979e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/PostgREST/Metrics.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
module PostgREST.Metrics
( init
, MetricsState (..)
Expand All @@ -21,6 +20,8 @@ data MetricsState = MetricsState
, poolAvailable :: Prom.Gauge
, poolWaiting :: Prom.Gauge
, poolMaxSize :: Prom.Gauge
, schemaCacheLoads :: Prom.Counter
, schemaCacheQueryTime :: Prom.Gauge

Check warning on line 24 in src/PostgREST/Metrics.hs

View check run for this annotation

Codecov / codecov/patch

src/PostgREST/Metrics.hs#L19-L24

Added lines #L19 - L24 were not covered by tests
}

init :: Int -> IO MetricsState
Expand All @@ -29,11 +30,13 @@ init poolMaxSize = do
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")
sCacheLoads <- Prom.register $ Prom.counter (Prom.Info "pgrst_schema_cache_loads_total" "The total number of times the schema cache was loaded")
sCacheQTime <- Prom.register $ Prom.gauge (Prom.Info "pgrst_schema_cache_query_time_seconds" "The query time in seconds of the last schema cache load")
Prom.setGauge maxSize (fromIntegral poolMaxSize)
pure $ MetricsState timeouts available waiting maxSize
pure $ MetricsState timeouts available waiting maxSize sCacheLoads sCacheQTime

observationMetrics :: MetricsState -> ObservationHandler
observationMetrics MetricsState{poolTimeouts, poolAvailable, poolWaiting} obs = case obs of
observationMetrics MetricsState{poolTimeouts, poolAvailable, poolWaiting, schemaCacheLoads, schemaCacheQueryTime} obs = case obs of
(PoolAcqTimeoutObs _) -> do
Prom.incCounter poolTimeouts
(HasqlPoolObs (SQL.ConnectionObservation _ status)) -> case status of
Expand All @@ -48,6 +51,9 @@ observationMetrics MetricsState{poolTimeouts, poolAvailable, poolWaiting} obs =
Prom.incGauge poolWaiting
PoolRequestFullfilled ->
Prom.decGauge poolWaiting
SchemaCacheLoadedObs resTime -> do
Prom.incCounter schemaCacheLoads
Prom.setGauge schemaCacheQueryTime resTime
_ ->
pure ()

Expand Down
1 change: 0 additions & 1 deletion test/spec/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,3 @@ main = do
where
loadSCache pool conf =
either (panic.show) id <$> P.use pool (HT.transaction HT.ReadCommitted HT.Read $ querySchemaCache conf)

0 comments on commit 91979e3

Please sign in to comment.