Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Probe for psycopg2 and psycopg3 parameters function. #2492

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 18 additions & 9 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,20 +666,29 @@
vendor = db.vendor
span.set_data(SPANDATA.DB_SYSTEM, vendor)

if (
# Some custom backends override `__getattr__`, making it look like `cursor_or_db`
# actually has a `connection` and the `connection` has a `get_dsn_parameters`
# attribute, only to throw an error once you actually want to call it.
# Hence the `inspect` check whether `get_dsn_parameters` is an actual callable
# function.
is_psycopg2 = (

Check warning on line 674 in sentry_sdk/integrations/django/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/django/__init__.py#L674

Added line #L674 was not covered by tests
hasattr(cursor_or_db, "connection")
and hasattr(cursor_or_db.connection, "get_dsn_parameters")
and inspect.isfunction(cursor_or_db.connection.get_dsn_parameters)
):
# Some custom backends override `__getattr__`, making it look like `cursor_or_db`
# actually has a `connection` and the `connection` has a `get_dsn_parameters`
# attribute, only to throw an error once you actually want to call it.
# Hence the `inspect` check whether `get_dsn_parameters` is an actual callable
# function.
)
if is_psycopg2:
connection_params = cursor_or_db.connection.get_dsn_parameters()

else:
connection_params = db.get_connection_params()
is_psycopg3 = (

Check warning on line 682 in sentry_sdk/integrations/django/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/django/__init__.py#L682

Added line #L682 was not covered by tests
hasattr(cursor_or_db, "connection")
and hasattr(cursor_or_db.connection, "info")
and hasattr(cursor_or_db.connection.info, "get_parameters")
and inspect.isfunction(cursor_or_db.connection.info.get_parameters)
)
if is_psycopg3:
connection_params = cursor_or_db.connection.info.get_parameters()

Check warning on line 689 in sentry_sdk/integrations/django/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/django/__init__.py#L689

Added line #L689 was not covered by tests
else:
connection_params = db.get_connection_params()

Check warning on line 691 in sentry_sdk/integrations/django/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/django/__init__.py#L691

Added line #L691 was not covered by tests

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