feat: schema cache metrics
This commit is contained in:
committed by
Steve Chavez
parent
653c7955b2
commit
357400b2b8
+1
-1
@@ -16,7 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
- #3214, Log connection pool events on log-level=info - @steve-chavez
|
- #3214, Log connection pool events on log-level=info - @steve-chavez
|
||||||
- #3435, Add log-level=debug, for development purposes - @steve-chavez
|
- #3435, Add log-level=debug, for development purposes - @steve-chavez
|
||||||
- #1526, Add `/metrics` endpoint on admin server - @steve-chavez
|
- #1526, Add `/metrics` endpoint on admin server - @steve-chavez
|
||||||
- Exposes connection pool metrics
|
- Exposes connection pool metrics, schema cache metrics
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|||||||
+15
-16
@@ -1,5 +1,3 @@
|
|||||||
{-# LANGUAGE NamedFieldPuns #-}
|
|
||||||
{-# LANGUAGE NumericUnderscores #-}
|
|
||||||
module PostgREST.Metrics
|
module PostgREST.Metrics
|
||||||
( init
|
( init
|
||||||
, MetricsState (..)
|
, MetricsState (..)
|
||||||
@@ -16,24 +14,22 @@ import PostgREST.Observation
|
|||||||
|
|
||||||
import Protolude
|
import Protolude
|
||||||
|
|
||||||
data MetricsState = MetricsState
|
data MetricsState =
|
||||||
{ poolTimeouts :: Prom.Counter
|
MetricsState Prom.Counter Prom.Gauge Prom.Gauge Prom.Gauge Prom.Counter Prom.Gauge
|
||||||
, poolAvailable :: Prom.Gauge
|
|
||||||
, poolWaiting :: Prom.Gauge
|
|
||||||
, poolMaxSize :: Prom.Gauge
|
|
||||||
}
|
|
||||||
|
|
||||||
init :: Int -> IO MetricsState
|
init :: Int -> IO MetricsState
|
||||||
init poolMaxSize = do
|
init configDbPoolSize = do
|
||||||
timeouts <- Prom.register $ Prom.counter (Prom.Info "pgrst_db_pool_timeouts_total" "The total number of pool connection timeouts")
|
poolTimeouts <- 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")
|
poolAvailable <- 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")
|
poolWaiting <- 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")
|
poolMaxSize <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_max" "Max pool connections")
|
||||||
Prom.setGauge maxSize (fromIntegral poolMaxSize)
|
schemaCacheLoads <- Prom.register $ Prom.counter (Prom.Info "pgrst_schema_cache_loads_total" "The total number of times the schema cache was loaded")
|
||||||
pure $ MetricsState timeouts available waiting maxSize
|
schemaCacheQueryTime <- 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 poolMaxSize (fromIntegral configDbPoolSize)
|
||||||
|
pure $ MetricsState poolTimeouts poolAvailable poolWaiting poolMaxSize schemaCacheLoads schemaCacheQueryTime
|
||||||
|
|
||||||
observationMetrics :: MetricsState -> ObservationHandler
|
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
|
(PoolAcqTimeoutObs _) -> do
|
||||||
Prom.incCounter poolTimeouts
|
Prom.incCounter poolTimeouts
|
||||||
(HasqlPoolObs (SQL.ConnectionObservation _ status)) -> case status of
|
(HasqlPoolObs (SQL.ConnectionObservation _ status)) -> case status of
|
||||||
@@ -48,6 +44,9 @@ observationMetrics MetricsState{poolTimeouts, poolAvailable, poolWaiting} obs =
|
|||||||
Prom.incGauge poolWaiting
|
Prom.incGauge poolWaiting
|
||||||
PoolRequestFullfilled ->
|
PoolRequestFullfilled ->
|
||||||
Prom.decGauge poolWaiting
|
Prom.decGauge poolWaiting
|
||||||
|
SchemaCacheLoadedObs resTime -> do
|
||||||
|
Prom.incCounter schemaCacheLoads
|
||||||
|
Prom.setGauge schemaCacheQueryTime resTime
|
||||||
_ ->
|
_ ->
|
||||||
pure ()
|
pure ()
|
||||||
|
|
||||||
|
|||||||
@@ -1435,6 +1435,8 @@ def test_admin_metrics(defaultenv):
|
|||||||
with run(env=defaultenv, port=freeport()) as postgrest:
|
with run(env=defaultenv, port=freeport()) as postgrest:
|
||||||
response = postgrest.admin.get("/metrics")
|
response = postgrest.admin.get("/metrics")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
assert "pgrst_schema_cache_query_time_seconds" in response.text
|
||||||
|
assert "pgrst_schema_cache_loads_total" in response.text
|
||||||
assert "pgrst_db_pool_max" in response.text
|
assert "pgrst_db_pool_max" in response.text
|
||||||
assert "pgrst_db_pool_waiting" in response.text
|
assert "pgrst_db_pool_waiting" in response.text
|
||||||
assert "pgrst_db_pool_available" in response.text
|
assert "pgrst_db_pool_available" in response.text
|
||||||
|
|||||||
@@ -280,4 +280,3 @@ main = do
|
|||||||
where
|
where
|
||||||
loadSCache pool conf =
|
loadSCache pool conf =
|
||||||
either (panic.show) id <$> P.use pool (HT.transaction HT.ReadCommitted HT.Read $ querySchemaCache conf)
|
either (panic.show) id <$> P.use pool (HT.transaction HT.ReadCommitted HT.Read $ querySchemaCache conf)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user