feat: log connection pool events on log-level=info

This commit is contained in:
steve-chavez
2024-04-15 18:31:51 -05:00
committed by Steve Chavez
parent 9d1dc783bf
commit 1bf0c54dd6
11 changed files with 93 additions and 45 deletions
+16 -7
View File
@@ -593,13 +593,13 @@ def test_pool_acquisition_timeout(level, defaultenv, metapostgrest):
assert data["message"] == "Timed out acquiring connection from connection pool."
# ensure the message appears on the logs as well
output = sorted(postgrest.read_stdout(nlines=2))
output = sorted(postgrest.read_stdout(nlines=3))
if level == "crit":
assert len(output) == 0
else:
elif level == "info":
assert " 504 " in output[0]
assert "Timed out acquiring connection from connection pool." in output[1]
assert "Timed out acquiring connection from connection pool." in output[2]
def test_change_statement_timeout_held_connection(defaultenv, metapostgrest):
@@ -792,7 +792,7 @@ def test_log_level(level, defaultenv):
response = postgrest.session.get("/")
assert response.status_code == 200
output = sorted(postgrest.read_stdout(nlines=3))
output = sorted(postgrest.read_stdout(nlines=7))
if level == "crit":
assert len(output) == 0
@@ -825,7 +825,11 @@ def test_log_level(level, defaultenv):
r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 - "" "python-requests/.+"',
output[2],
)
assert len(output) == 3
assert "Connection" and "is available" in output[3]
assert "Connection" and "is available" in output[4]
assert "Connection" and "is used" in output[5]
assert "Connection" and "is used" in output[6]
assert len(output) == 7
def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
@@ -1083,12 +1087,14 @@ def test_get_pgrst_version_with_keyval_connection_string(defaultenv):
def test_log_postgrest_version(defaultenv):
"Should show the PostgREST version in the logs"
env = {**defaultenv, "PGRST_LOG_LEVEL": "crit"}
with run(env=defaultenv, no_startup_stdout=False) as postgrest:
version = postgrest.session.head("/").headers["Server"].split("/")[1]
output = sorted(postgrest.read_stdout(nlines=5))
assert "Starting PostgREST %s..." % version in output[3]
assert "Starting PostgREST %s..." % version in output[4]
def test_succeed_w_role_having_superuser_settings(defaultenv):
@@ -1371,10 +1377,13 @@ def test_db_error_logging_to_stderr(level, defaultenv, metapostgrest):
assert response.status_code == 500
# ensure the message appears on the logs
output = sorted(postgrest.read_stdout(nlines=2))
output = sorted(postgrest.read_stdout(nlines=4))
if level == "crit":
assert len(output) == 0
elif level == "info":
assert " 500 " in output[0]
assert "canceling statement due to statement timeout" in output[3]
else:
assert " 500 " in output[0]
assert "canceling statement due to statement timeout" in output[1]
+8 -1
View File
@@ -1,6 +1,7 @@
module Main where
import qualified Hasql.Pool as P
import qualified Hasql.Pool.Config as P
import qualified Hasql.Transaction.Sessions as HT
import Data.Function (id)
@@ -70,7 +71,13 @@ import qualified Feature.RpcPreRequestGucsSpec
main :: IO ()
main = do
let observer = const $ pure ()
pool <- P.acquire 3 10 60 60 $ toUtf8 $ configDbUri testCfg
pool <- P.acquire $ P.settings
[ P.size 3
, P.acquisitionTimeout 10
, P.agingTimeout 60
, P.idlenessTimeout 60
, P.staticConnectionSettings (toUtf8 $ configDbUri testCfg)
]
actualPgVersion <- either (panic . show) id <$> P.use pool (queryPgVersion False)