Skip to content

Commit

Permalink
fix(django): improve getting psycopg3 connection info
Browse files Browse the repository at this point in the history
Fetch the few needed parameters manually instead of relying on `get_parameters()` which adds visible overhead due to excluding default values for parameters.
  • Loading branch information
nijel committed Oct 2, 2024
1 parent 5080c76 commit 17f676e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,18 @@ def _set_db_data(span, cursor_or_db):
connection_params = cursor_or_db.connection.get_dsn_parameters()
else:
try:
# psycopg3
connection_params = cursor_or_db.connection.info.get_parameters()
# psycopg3, only extract needed params as get_parameters
# can be slow because of the additional logic to filter out default
# values
connection_params = {
"dbname": cursor_or_db.connection.info.dbname,
"port": cursor_or_db.connection.info.port,
}
# PGhost returns host or base dir of UNIX socket as an absolute path
# starting with /, use it only when it contains host
pg_host = cursor_or_db.connection.info.host
if pg_host and not pg_host.startswith("/"):
connection_params["host"] = pg_host
except Exception:
connection_params = db.get_connection_params()

Expand Down

0 comments on commit 17f676e

Please sign in to comment.