Skip to content

Commit cc939a1

Browse files
committed
updated fallback
1 parent e4c1308 commit cc939a1

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import threading
33
import weakref
4+
import inspect
45
from importlib import import_module
56

67
import sentry_sdk
@@ -763,24 +764,52 @@ def _set_db_data(span, cursor_or_db):
763764
span.set_data(SPANDATA.SERVER_SOCKET_ADDRESS, cached_config["unix_socket"])
764765
return
765766

766-
# Fallback to using get_connection_params()
767+
# Fallback to using the database connection to get the data.
768+
# This is the edge case where db configuration is not in Django's `DATABASES` setting.
767769
try:
768-
connection_params = db.get_connection_params()
770+
# Some custom backends override `__getattr__`, making it look like `cursor_or_db`
771+
# actually has a `connection` and the `connection` has a `get_dsn_parameters`
772+
# attribute, only to throw an error once you actually want to call it.
773+
# Hence the `inspect` check whether `get_dsn_parameters` is an actual callable
774+
# function.
775+
is_psycopg2 = (
776+
hasattr(cursor_or_db, "connection")
777+
and hasattr(cursor_or_db.connection, "get_dsn_parameters")
778+
and inspect.isroutine(cursor_or_db.connection.get_dsn_parameters)
779+
)
780+
if is_psycopg2:
781+
connection_params = cursor_or_db.connection.get_dsn_parameters()
782+
else:
783+
try:
784+
# psycopg3, only extract needed params as get_parameters
785+
# can be slow because of the additional logic to filter out default
786+
# values
787+
connection_params = {
788+
"dbname": cursor_or_db.connection.info.dbname,
789+
"port": cursor_or_db.connection.info.port,
790+
}
791+
# PGhost returns host or base dir of UNIX socket as an absolute path
792+
# starting with /, use it only when it contains host
793+
host = cursor_or_db.connection.info.host
794+
if host and not host.startswith("/"):
795+
connection_params["host"] = host
796+
except Exception:
797+
connection_params = db.get_connection_params()
769798

770799
db_name = connection_params.get("dbname") or connection_params.get("database")
771-
if db_name:
800+
if db_name is not None:
772801
span.set_data(SPANDATA.DB_NAME, db_name)
773802

774803
host = connection_params.get("host")
775-
if host:
804+
if host is not None:
776805
span.set_data(SPANDATA.SERVER_ADDRESS, host)
777806

778807
port = connection_params.get("port")
779-
if port:
808+
if port is not None:
780809
span.set_data(SPANDATA.SERVER_PORT, str(port))
781810

782811
unix_socket = connection_params.get("unix_socket")
783-
if unix_socket:
812+
if unix_socket is not None:
784813
span.set_data(SPANDATA.SERVER_SOCKET_ADDRESS, unix_socket)
785814

786815
except (KeyError, ImproperlyConfigured, AttributeError) as e:

0 commit comments

Comments
 (0)