From 8ff5063551790f5d1049a8f78fd8de401586fea3 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Wed, 29 May 2024 13:03:53 -0400 Subject: [PATCH] fix(django): Fix psycopg3 Fixes GH-3061 --- sentry_sdk/integrations/django/__init__.py | 11 +++-------- tests/integrations/django/test_basic.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 3a6a075c70..6be0113241 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -695,15 +695,10 @@ def _set_db_data(span, cursor_or_db): if is_psycopg2: connection_params = cursor_or_db.connection.get_dsn_parameters() else: - is_psycopg3 = ( - hasattr(cursor_or_db, "connection") - and hasattr(cursor_or_db.connection, "info") - and hasattr(cursor_or_db.connection.info, "get_parameters") - and inspect.isroutine(cursor_or_db.connection.info.get_parameters) - ) - if is_psycopg3: + try: + # psycopg3 connection_params = cursor_or_db.connection.info.get_parameters() - else: + except Exception: connection_params = db.get_connection_params() db_name = connection_params.get("dbname") or connection_params.get("database") diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 5e1529c762..b18de73592 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -647,6 +647,18 @@ def get_connection_params(self): pytest.fail("A TypeError was raised") +def test_connection_reconnect_does_not_error(sentry_init): + sentry_init(integrations=[DjangoIntegration()]) + + from django.db import connections + + if "postgres" not in connections: + pytest.skip("postgres tests disabled") + + connections["postgres"].close() + connections["postgres"].connect() + + @pytest.mark.parametrize( "transaction_style,client_url,expected_transaction,expected_source,expected_response", [