@@ -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,15 @@ 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+ stateObsChan <- newObsChan obsChan
112+ appState <- AppState. initWithPool sockets pool config loggerState metricsState (Metrics. observationMetrics metricsState <> writeChan obsChan)
113+ AppState. putPgVersion appState actualPgVersion
114+ AppState. putSchemaCache appState (Just sCache)
115+ return ((appState, metricsState, stateObsChan), postgrest (configLogLevel config) appState (pure () ))
116+
98117 -- For tests that run with the same schema cache
99118 app = initApp baseSchemaCache ()
100119
@@ -123,6 +142,7 @@ main = do
123142 obsApp = app testObservabilityCfg
124143 serverTiming = app testCfgServerTiming
125144 aggregatesEnabled = app testCfgAggregatesEnabled
145+ observationsApp = initObservationsApp baseSchemaCache testCfg
126146
127147 extraSearchPathApp = appDbs testCfgExtraSearchPath
128148 unicodeApp = appDbs testUnicodeCfg
@@ -278,6 +298,9 @@ main = do
278298 before (initApp baseSchemaCache metricsState testCfgJwtCache) $
279299 describe " Feature.Auth.JwtCacheSpec" Feature.Auth.JwtCacheSpec. spec
280300
301+ before observationsApp $
302+ describe " Feature.MetricsSpec" Feature.MetricsSpec. spec
303+
281304 where
282305 loadSCache pool conf =
283306 either (panic. show ) id <$> P. use pool (HT. transaction HT. ReadCommitted HT. Read $ querySchemaCache conf)
0 commit comments