Skip to content

Commit

Permalink
fix(django): improve getting psycopg3 connection info (#3580)
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 authored Oct 8, 2024
1 parent 2d2e548 commit 4f79aec
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 4f79aec

Please sign in to comment.