diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d8bf5b94..67debfab9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #3214, Log connection pool events on log-level=info - @steve-chavez - #3435, Add log-level=debug, for development purposes - @steve-chavez - #1526, Add `/metrics` endpoint on admin server - @steve-chavez - - Exposes connection pool metrics + - Exposes connection pool metrics, schema cache metrics ### Fixed diff --git a/src/PostgREST/Metrics.hs b/src/PostgREST/Metrics.hs index cbf56e03b..7c1f7d1bc 100644 --- a/src/PostgREST/Metrics.hs +++ b/src/PostgREST/Metrics.hs @@ -1,5 +1,3 @@ -{-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE NumericUnderscores #-} module PostgREST.Metrics ( init , MetricsState (..) @@ -16,24 +14,22 @@ import PostgREST.Observation import Protolude -data MetricsState = MetricsState - { poolTimeouts :: Prom.Counter - , poolAvailable :: Prom.Gauge - , poolWaiting :: Prom.Gauge - , poolMaxSize :: Prom.Gauge - } +data MetricsState = + MetricsState Prom.Counter Prom.Gauge Prom.Gauge Prom.Gauge Prom.Counter 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 +init configDbPoolSize = do + poolTimeouts <- Prom.register $ Prom.counter (Prom.Info "pgrst_db_pool_timeouts_total" "The total number of pool connection timeouts") + poolAvailable <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_available" "Available connections in the pool") + poolWaiting <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_waiting" "Requests waiting to acquire a pool connection") + poolMaxSize <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_max" "Max pool connections") + schemaCacheLoads <- Prom.register $ Prom.counter (Prom.Info "pgrst_schema_cache_loads_total" "The total number of times the schema cache was loaded") + 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{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 @@ -48,6 +44,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 () diff --git a/test/io/test_io.py b/test/io/test_io.py index 8eee3848f..d9cad71d9 100644 --- a/test/io/test_io.py +++ b/test/io/test_io.py @@ -1435,6 +1435,8 @@ def test_admin_metrics(defaultenv): with run(env=defaultenv, port=freeport()) as postgrest: response = postgrest.admin.get("/metrics") 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_waiting" in response.text assert "pgrst_db_pool_available" in response.text diff --git a/test/spec/Main.hs b/test/spec/Main.hs index 32ecba7f1..32aa7eeb3 100644 --- a/test/spec/Main.hs +++ b/test/spec/Main.hs @@ -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) -