@@ -29,6 +29,7 @@ import qualified Feature.Auth.NoJwtSecretSpec
2929import qualified Feature.ConcurrentSpec
3030import qualified Feature.CorsSpec
3131import qualified Feature.ExtraSearchPathSpec
32+ import qualified Feature.MetricsSpec
3233import qualified Feature.NoSuperuserSpec
3334import qualified Feature.ObservabilitySpec
3435import qualified Feature.OpenApi.DisabledOpenApiSpec
@@ -68,16 +69,26 @@ import qualified Feature.Query.UpdateSpec
6869import qualified Feature.Query.UpsertSpec
6970import qualified Feature.RollbackSpec
7071import qualified Feature.RpcPreRequestGucsSpec
72+ import PostgREST.Observation (Observation (HasqlPoolObs ))
7173
7274
7375main :: IO ()
7476main = do
77+ poolChan <- newChan
78+ -- make sure poolChan is not growing indefinitely
79+ -- start a thread that drains the channel
80+ -- this is necessary because test cases operate on
81+ -- copies so poolChan is never read from
82+ void $ forkIO $ forever $ readChan poolChan
83+ metricsState <- Metrics. init (configDbPoolSize testCfg)
7584 pool <- P. acquire $ P. settings
7685 [ P. size 3
7786 , P. acquisitionTimeout 10
7887 , P. agingTimeout 60
7988 , P. idlenessTimeout 60
8089 , P. staticConnectionSettings (toUtf8 $ configDbUri testCfg)
90+ -- make sure metrics are updated and pool observations published to poolChan
91+ , P. observationHandler $ (writeChan poolChan <> Metrics. observationMetrics metricsState) . HasqlPoolObs
8192 ]
8293
8394 actualPgVersion <- either (panic . show ) id <$> P. use pool (queryPgVersion False )
@@ -86,7 +97,6 @@ main = do
8697 baseSchemaCache <- loadSCache pool testCfg
8798 sockets <- AppState. initSockets testCfg
8899 loggerState <- Logger. init
89- metricsState <- Metrics. init (configDbPoolSize testCfg)
90100
91101 let
92102 initApp sCache st config = do
@@ -95,6 +105,14 @@ main = do
95105 AppState. putSchemaCache appState (Just sCache)
96106 return (st, postgrest (configLogLevel config) appState (pure () ))
97107
108+ initObservationsApp sCache config = do
109+ -- duplicate poolChan as a starting point
110+ obsChan <- dupChan poolChan
111+ appState <- AppState. initWithPool sockets pool config loggerState metricsState (Metrics. observationMetrics metricsState <> writeChan obsChan)
112+ AppState. putPgVersion appState actualPgVersion
113+ AppState. putSchemaCache appState (Just sCache)
114+ return (((appState, metricsState), obsChan), postgrest (configLogLevel config) appState (pure () ))
115+
98116 -- For tests that run with the same schema cache
99117 app = initApp baseSchemaCache ()
100118
@@ -123,6 +141,7 @@ main = do
123141 obsApp = app testObservabilityCfg
124142 serverTiming = app testCfgServerTiming
125143 aggregatesEnabled = app testCfgAggregatesEnabled
144+ observationsApp = initObservationsApp baseSchemaCache testCfg
126145
127146 extraSearchPathApp = appDbs testCfgExtraSearchPath
128147 unicodeApp = appDbs testUnicodeCfg
@@ -278,6 +297,9 @@ main = do
278297 before (initApp baseSchemaCache metricsState testCfgJwtCache) $
279298 describe " Feature.Auth.JwtCacheSpec" Feature.Auth.JwtCacheSpec. spec
280299
300+ before observationsApp $
301+ describe " Feature.MetricsSpec" Feature.MetricsSpec. spec
302+
281303 where
282304 loadSCache pool conf =
283305 either (panic. show ) id <$> P. use pool (HT. transaction HT. ReadCommitted HT. Read $ querySchemaCache conf)
0 commit comments