add: Log pg version details of listener connection
Follow-up to #4617 adding more information to log entry produced upon successful listener connection establishement.
This commit is contained in:
committed by
Steve Chavez
parent
34a767a5cc
commit
b9c8562641
+1
-1
@@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file. From versio
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Log the actual host and port of listener database connection by @mkleczek in #4617
|
- Log host, port and pg version of listener database connection by @mkleczek in #4617 #4618
|
||||||
|
|
||||||
## [14.8] - 2026-04-03
|
## [14.8] - 2026-04-03
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ import Control.Arrow ((&&&))
|
|||||||
import Data.Bitraversable (bisequence)
|
import Data.Bitraversable (bisequence)
|
||||||
import Data.Either.Combinators (whenRight)
|
import Data.Either.Combinators (whenRight)
|
||||||
import qualified Database.PostgreSQL.LibPQ as LibPQ
|
import qualified Database.PostgreSQL.LibPQ as LibPQ
|
||||||
|
import qualified Hasql.Session as SQL
|
||||||
|
import PostgREST.Config.Database (queryPgVersion)
|
||||||
|
import PostgREST.Config.PgVersion (pgvFullName)
|
||||||
import Protolude
|
import Protolude
|
||||||
|
|
||||||
-- | Starts the Listener in a thread
|
-- | Starts the Listener in a thread
|
||||||
@@ -66,6 +69,9 @@ retryingListen appState = do
|
|||||||
\case
|
\case
|
||||||
Right db -> do
|
Right db -> do
|
||||||
SQL.listen db $ SQL.toPgIdentifier dbChannel
|
SQL.listen db $ SQL.toPgIdentifier dbChannel
|
||||||
|
(pqHost, pqPort) <- SQL.withLibPQConnection db $ bisequence . (LibPQ.host &&& LibPQ.port)
|
||||||
|
pgFullName <- SQL.run (queryPgVersion False) db >>= either throwIO (pure . pgvFullName)
|
||||||
|
|
||||||
AppState.putIsListenerOn appState True
|
AppState.putIsListenerOn appState True
|
||||||
|
|
||||||
delay <- AppState.getNextListenerDelay appState
|
delay <- AppState.getNextListenerDelay appState
|
||||||
@@ -75,8 +81,8 @@ retryingListen appState = do
|
|||||||
-- reset the delay
|
-- reset the delay
|
||||||
AppState.putNextListenerDelay appState 1
|
AppState.putNextListenerDelay appState 1
|
||||||
|
|
||||||
(pqHost, pqPort) <- SQL.withLibPQConnection db $ bisequence . (LibPQ.host &&& LibPQ.port)
|
observer $ DBListenStart pqHost pqPort pgFullName dbChannel
|
||||||
observer $ DBListenStart pqHost pqPort dbChannel
|
|
||||||
-- wait for notifications
|
-- wait for notifications
|
||||||
-- this will never return, in case of an error it will throw and be caught by onError
|
-- this will never return, in case of an error it will throw and be caught by onError
|
||||||
forever $ SQL.waitForNotifications handleNotification db
|
forever $ SQL.waitForNotifications handleNotification db
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ data Observation
|
|||||||
| SchemaCacheSummaryObs Text
|
| SchemaCacheSummaryObs Text
|
||||||
| SchemaCacheLoadedObs Double
|
| SchemaCacheLoadedObs Double
|
||||||
| ConnectionRetryObs Int
|
| ConnectionRetryObs Int
|
||||||
| DBListenStart (Maybe ByteString) (Maybe ByteString) Text -- host, port, channel
|
| DBListenStart (Maybe ByteString) (Maybe ByteString) Text Text -- host, port, version string, channel
|
||||||
| DBListenFail Text (Either SQL.ConnectionError SomeException)
|
| DBListenFail Text (Either SQL.ConnectionError SomeException)
|
||||||
| DBListenRetry Int
|
| DBListenRetry Int
|
||||||
| DBListenBugHint -- https://github.com/PostgREST/postgrest/issues/3147
|
| DBListenBugHint -- https://github.com/PostgREST/postgrest/issues/3147
|
||||||
@@ -114,8 +114,8 @@ observationMessage = \case
|
|||||||
"Attempting to reconnect to the database in " <> (show delay::Text) <> " seconds..."
|
"Attempting to reconnect to the database in " <> (show delay::Text) <> " seconds..."
|
||||||
QueryPgVersionError usageErr ->
|
QueryPgVersionError usageErr ->
|
||||||
"Failed to query the PostgreSQL version. " <> jsonMessage usageErr
|
"Failed to query the PostgreSQL version. " <> jsonMessage usageErr
|
||||||
DBListenStart host port channel -> do
|
DBListenStart host port fullName channel -> do
|
||||||
"Listener connected to " <> show (fold $ host <> fmap (":" <>) port) <> " and listening for database notifications on the " <> show channel <> " channel"
|
"Listener connected to " <> fullName <> " on " <> show (fold $ host <> fmap (":" <>) port) <> " and listening for database notifications on the " <> show channel <> " channel"
|
||||||
DBListenFail channel listenErr ->
|
DBListenFail channel listenErr ->
|
||||||
"Failed listening for database notifications on the " <> show channel <> " channel. " <>
|
"Failed listening for database notifications on the " <> show channel <> " channel. " <>
|
||||||
either showListenerConnError showListenerException listenErr
|
either showListenerConnError showListenerException listenErr
|
||||||
|
|||||||
+4
-2
@@ -1667,9 +1667,11 @@ def test_log_listener_connection_start(defaultenv):
|
|||||||
}
|
}
|
||||||
|
|
||||||
with run(env=env, no_startup_stdout=False, wait_for_readiness=True) as postgrest:
|
with run(env=env, no_startup_stdout=False, wait_for_readiness=True) as postgrest:
|
||||||
output = postgrest.read_stdout(nlines=5)
|
output = postgrest.read_stdout(nlines=10)
|
||||||
|
# Check for the listener start message containing host and port
|
||||||
|
# Do not check if pg version is displayed properly as it is tricky to test it
|
||||||
assert any(
|
assert any(
|
||||||
f'Listener connected to "{defaultenv["PGHOST"]}:5432" and listening for database notifications on the "pgrst" channel'
|
f'"{defaultenv["PGHOST"]}:5432" and listening for database notifications on the "pgrst" channel'
|
||||||
in line
|
in line
|
||||||
for line in output
|
for line in output
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -206,5 +206,5 @@ waitForObs (ObsChan orig copy) t msg f =
|
|||||||
obsDiagMessage :: Observation -> Text
|
obsDiagMessage :: Observation -> Text
|
||||||
obsDiagMessage = \case
|
obsDiagMessage = \case
|
||||||
(HasqlPoolObs o) -> show o
|
(HasqlPoolObs o) -> show o
|
||||||
o@(DBListenStart host port channel) -> constrName o <> show (host, port, channel)
|
o@(DBListenStart host port name channel) -> constrName o <> show (host, port, name, channel)
|
||||||
o -> constrName o
|
o -> constrName o
|
||||||
|
|||||||
Reference in New Issue
Block a user