Skip to content

Commit c8154be

Browse files
authored
Probe for psycopg2 and psycopg3 parameters function. (#2492)
1 parent c5b915d commit c8154be

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

sentry_sdk/integrations/django/__init__.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -666,20 +666,29 @@ def _set_db_data(span, cursor_or_db):
666666
vendor = db.vendor
667667
span.set_data(SPANDATA.DB_SYSTEM, vendor)
668668

669-
if (
669+
# Some custom backends override `__getattr__`, making it look like `cursor_or_db`
670+
# actually has a `connection` and the `connection` has a `get_dsn_parameters`
671+
# attribute, only to throw an error once you actually want to call it.
672+
# Hence the `inspect` check whether `get_dsn_parameters` is an actual callable
673+
# function.
674+
is_psycopg2 = (
670675
hasattr(cursor_or_db, "connection")
671676
and hasattr(cursor_or_db.connection, "get_dsn_parameters")
672677
and inspect.isfunction(cursor_or_db.connection.get_dsn_parameters)
673-
):
674-
# Some custom backends override `__getattr__`, making it look like `cursor_or_db`
675-
# actually has a `connection` and the `connection` has a `get_dsn_parameters`
676-
# attribute, only to throw an error once you actually want to call it.
677-
# Hence the `inspect` check whether `get_dsn_parameters` is an actual callable
678-
# function.
678+
)
679+
if is_psycopg2:
679680
connection_params = cursor_or_db.connection.get_dsn_parameters()
680-
681681
else:
682-
connection_params = db.get_connection_params()
682+
is_psycopg3 = (
683+
hasattr(cursor_or_db, "connection")
684+
and hasattr(cursor_or_db.connection, "info")
685+
and hasattr(cursor_or_db.connection.info, "get_parameters")
686+
and inspect.isfunction(cursor_or_db.connection.info.get_parameters)
687+
)
688+
if is_psycopg3:
689+
connection_params = cursor_or_db.connection.info.get_parameters()
690+
else:
691+
connection_params = db.get_connection_params()
683692

684693
db_name = connection_params.get("dbname") or connection_params.get("database")
685694
if db_name is not None:

0 commit comments

Comments
 (0)