diff --git a/CHANGELOG.md b/CHANGELOG.md index 4864b9c8a..0c61e38a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ All notable changes to this project will be documented in this file. From versio - Log error when `db-schemas` config contains schema `pg_catalog` or `information_schema` by @taimoorzaeem in #4359 - Add a `HINT` when the LISTEN channel stops working due to a PostgreSQL bug by @laurenceisla in #4581 - Add string slicing operator for `jwt-role-claim-key` by @taimoorzaeem in #4599 -- 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 ### Fixed diff --git a/src/PostgREST/Listener.hs b/src/PostgREST/Listener.hs index 13eae3c86..9fcf77263 100644 --- a/src/PostgREST/Listener.hs +++ b/src/PostgREST/Listener.hs @@ -17,10 +17,13 @@ import PostgREST.Version (prettyVersion) import qualified PostgREST.AppState as AppState import qualified PostgREST.Config as Config -import Control.Arrow ((&&&)) -import Data.Bitraversable (bisequence) -import Data.Either.Combinators (whenRight) -import qualified Database.PostgreSQL.LibPQ as LibPQ +import Control.Arrow ((&&&)) +import Data.Bitraversable (bisequence) +import Data.Either.Combinators (whenRight) +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 -- | Starts the Listener in a thread @@ -64,6 +67,9 @@ retryingListen appState = do \case Right db -> do 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 delay <- AppState.getNextListenerDelay appState @@ -73,8 +79,7 @@ retryingListen appState = do -- reset the delay AppState.putNextListenerDelay appState 1 - (pqHost, pqPort) <- SQL.withLibPQConnection db $ bisequence . (LibPQ.host &&& LibPQ.port) - observer $ DBListenStart pqHost pqPort dbChannel + observer $ DBListenStart pqHost pqPort pgFullName dbChannel SQL.waitForNotifications handleNotification db diff --git a/src/PostgREST/Observation.hs b/src/PostgREST/Observation.hs index 03a8e6d95..9f1e646e4 100644 --- a/src/PostgREST/Observation.hs +++ b/src/PostgREST/Observation.hs @@ -44,7 +44,7 @@ data Observation | SchemaCacheSummaryObs Text | SchemaCacheLoadedObs Double | 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 (Either SomeException ())) | DBListenRetry Int | DBListenBugHint -- https://github.com/PostgREST/postgrest/issues/3147 @@ -110,8 +110,8 @@ observationMessage = \case "Attempting to reconnect to the database in " <> (show delay::Text) <> " seconds..." QueryPgVersionError usageErr -> "Failed to query the PostgreSQL version. " <> jsonMessage usageErr - DBListenStart host port channel -> do - "Listener connected to " <> show (fold $ host <> fmap (":" <>) port) <> " and listening for database notifications on the " <> show channel <> " channel" + DBListenStart host port fullName channel -> do + "Listener connected to " <> fullName <> " on " <> show (fold $ host <> fmap (":" <>) port) <> " and listening for database notifications on the " <> show channel <> " channel" DBListenFail channel listenErr -> "Failed listening for database notifications on the " <> show channel <> " channel. " <> either showListenerConnError showListenerException listenErr diff --git a/test/io/test_io.py b/test/io/test_io.py index 46f6414a9..e384a28fe 100644 --- a/test/io/test_io.py +++ b/test/io/test_io.py @@ -1649,9 +1649,11 @@ def test_log_listener_connection_start(defaultenv): } 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( - 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 for line in output )