Skip to content

Commit 4f79aec

Browse files
authored
fix(django): improve getting psycopg3 connection info (#3580)
Fetch the few needed parameters manually instead of relying on `get_parameters()` which adds visible overhead due to excluding default values for parameters.
1 parent 2d2e548 commit 4f79aec

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

sentry_sdk/integrations/django/__init__.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,18 @@ def _set_db_data(span, cursor_or_db):
717717
connection_params = cursor_or_db.connection.get_dsn_parameters()
718718
else:
719719
try:
720-
# psycopg3
721-
connection_params = cursor_or_db.connection.info.get_parameters()
720+
# psycopg3, only extract needed params as get_parameters
721+
# can be slow because of the additional logic to filter out default
722+
# values
723+
connection_params = {
724+
"dbname": cursor_or_db.connection.info.dbname,
725+
"port": cursor_or_db.connection.info.port,
726+
}
727+
# PGhost returns host or base dir of UNIX socket as an absolute path
728+
# starting with /, use it only when it contains host
729+
pg_host = cursor_or_db.connection.info.host
730+
if pg_host and not pg_host.startswith("/"):
731+
connection_params["host"] = pg_host
722732
except Exception:
723733
connection_params = db.get_connection_params()
724734

0 commit comments

Comments
 (0)