Skip to content

Commit 2f3a402

Browse files
authored
In Postgres take the connection params from the connection (#2308)
* In Postgres take the connection params from the connection and not the db. (On Mysql and SQLite this is unfortunately not possible because not exposed by the libs) * Make port always string to be consistent
1 parent 6c2a86d commit 2f3a402

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

scripts/runtox.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ ENV="$($TOXPATH -l | grep "$searchstring" | tr $'\n' ',')"
2323
if [ "$ENV" = py2.7-common, ] || [ "$ENV" = py2.7-gevent, ]; then
2424
exec $TOXPATH -vv -e "$ENV" -- "${@:2}"
2525
else
26-
exec $TOXPATH -vv -p auto -e "$ENV" -- "${@:2}"
26+
exec $TOXPATH -vv -e "$ENV" -- "${@:2}"
2727
fi

sentry_sdk/integrations/django/__init__.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import weakref
77
from importlib import import_module
88

9-
from sentry_sdk._compat import string_types
9+
from sentry_sdk._compat import string_types, text_type
1010
from sentry_sdk._types import TYPE_CHECKING
1111
from sentry_sdk.consts import OP, SPANDATA
1212
from sentry_sdk.hub import Hub, _should_send_default_pii
@@ -612,7 +612,7 @@ def execute(self, sql, params=None):
612612
with record_sql_queries(
613613
hub, self.cursor, sql, params, paramstyle="format", executemany=False
614614
) as span:
615-
_set_db_data(span, self.db.vendor, self.db.get_connection_params())
615+
_set_db_data(span, self)
616616
return real_execute(self, sql, params)
617617

618618
def executemany(self, sql, param_list):
@@ -624,7 +624,7 @@ def executemany(self, sql, param_list):
624624
with record_sql_queries(
625625
hub, self.cursor, sql, param_list, paramstyle="format", executemany=True
626626
) as span:
627-
_set_db_data(span, self.db.vendor, self.db.get_connection_params())
627+
_set_db_data(span, self)
628628
return real_executemany(self, sql, param_list)
629629

630630
def connect(self):
@@ -637,7 +637,7 @@ def connect(self):
637637
hub.add_breadcrumb(message="connect", category="query")
638638

639639
with hub.start_span(op=OP.DB, description="connect") as span:
640-
_set_db_data(span, self.vendor, self.get_connection_params())
640+
_set_db_data(span, self)
641641
return real_connect(self)
642642

643643
CursorWrapper.execute = execute
@@ -646,10 +646,19 @@ def connect(self):
646646
ignore_logger("django.db.backends")
647647

648648

649-
def _set_db_data(span, vendor, connection_params):
650-
# type: (Span, str, Dict[str, str]) -> None
649+
def _set_db_data(span, cursor_or_db):
650+
# type: (Span, Any) -> None
651+
652+
db = cursor_or_db.db if hasattr(cursor_or_db, "db") else cursor_or_db
653+
vendor = db.vendor
651654
span.set_data(SPANDATA.DB_SYSTEM, vendor)
652655

656+
connection_params = (
657+
cursor_or_db.connection.get_dsn_parameters()
658+
if hasattr(cursor_or_db, "connection")
659+
and hasattr(cursor_or_db.connection, "get_dsn_parameters")
660+
else db.get_connection_params()
661+
)
653662
db_name = connection_params.get("dbname") or connection_params.get("database")
654663
if db_name is not None:
655664
span.set_data(SPANDATA.DB_NAME, db_name)
@@ -660,7 +669,7 @@ def _set_db_data(span, vendor, connection_params):
660669

661670
server_port = connection_params.get("port")
662671
if server_port is not None:
663-
span.set_data(SPANDATA.SERVER_PORT, server_port)
672+
span.set_data(SPANDATA.SERVER_PORT, text_type(server_port))
664673

665674
server_socket_address = connection_params.get("unix_socket")
666675
if server_socket_address is not None:

tests/integrations/django/test_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def test_db_connection_span_data(sentry_init, client, capture_events):
653653
assert data.get(SPANDATA.SERVER_ADDRESS) == os.environ.get(
654654
"SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost"
655655
)
656-
assert data.get(SPANDATA.SERVER_PORT) == 5432
656+
assert data.get(SPANDATA.SERVER_PORT) == "5432"
657657

658658

659659
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)