From 62c713c6c71d600c5429c6bdb0fa864222e2a139 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Jul 2023 13:07:12 +0200 Subject: [PATCH 01/26] Added connection data to sqlalchemy spans. --- sentry_sdk/consts.py | 31 +++++++++++++++++++++++++++ sentry_sdk/integrations/sqlalchemy.py | 16 +++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index 4c05b36d84..ee99210341 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -68,6 +68,12 @@ class SPANDATA: See: https://develop.sentry.dev/sdk/performance/span-data-conventions/ """ + DB_NAME = "db.name" + """ + The name of the database being accessed. For commands that switch the database, this should be set to the target database (even if the command fails). + Example: myDatabase + """ + DB_OPERATION = "db.operation" """ The name of the operation being executed, e.g. the MongoDB command name such as findAndModify, or the SQL keyword. @@ -118,6 +124,31 @@ class SPANDATA: Example: 418 """ + SERVER_ADDRESS = "server.address" + """ + Name of the database host. + Example: example.com + """ + + SERVER_PORT = "server.port" + """ + Logical server port number + Example: 80; 8080; 443 + """ + + SERVER_SOCKET_ADDRESS = "server.socket.address" + """ + Physical server IP address or Unix socket address. + Example: 10.5.3.2 + """ + + SERVER_SOCKET_PORT = "server.socket.port" + """ + Physical server port. + Recommended: If different than server.port. + Example: 16456 + """ + class OP: CACHE_GET_ITEM = "cache.get_item" diff --git a/sentry_sdk/integrations/sqlalchemy.py b/sentry_sdk/integrations/sqlalchemy.py index 168aca9e04..9853bea78b 100644 --- a/sentry_sdk/integrations/sqlalchemy.py +++ b/sentry_sdk/integrations/sqlalchemy.py @@ -67,9 +67,7 @@ def _before_cursor_execute( span = ctx_mgr.__enter__() if span is not None: - db_system = _get_db_system(conn.engine.name) - if db_system is not None: - span.set_data(SPANDATA.DB_SYSTEM, db_system) + set_db_data(span, conn) context._sentry_sql_span = span @@ -128,3 +126,15 @@ def _get_db_system(name): return "oracle" return None + + +def set_db_data(span, conn): + # type: (Span, Any) -> None + span.set_data(SPANDATA.SERVER_ADDRESS, conn.engine.url.host) + span.set_data(SPANDATA.SERVER_PORT, conn.engine.url.port) + + db_system = _get_db_system(conn.engine.name) + if db_system is not None: + span.set_data(SPANDATA.DB_SYSTEM, db_system) + + span.set_data(SPANDATA.DB_NAME, conn.engine.url.database) From f8dcc415bdab7194699445bd314e4dfb035b9a59 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Jul 2023 13:33:20 +0200 Subject: [PATCH 02/26] Add connection data for mongo db spans --- sentry_sdk/integrations/pymongo.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/pymongo.py b/sentry_sdk/integrations/pymongo.py index 391219c75e..225ca90ebe 100644 --- a/sentry_sdk/integrations/pymongo.py +++ b/sentry_sdk/integrations/pymongo.py @@ -121,10 +121,13 @@ def started(self, event): pass data = {"operation_ids": {}} # type: Dict[str, Any] - data["operation_ids"]["operation"] = event.operation_id data["operation_ids"]["request"] = event.request_id + data[SPANDATA.DB_SYSTEM] = "mongodb" + data[SPANDATA.DB_NAME] = event.database_name + data[SPANDATA.SERVER_ADDRESS] = event.connection_id[0] + data[SPANDATA.SERVER_PORT] = event.connection_id[1] try: lsid = command.pop("lsid")["id"] From 2590506b59bb234d6371acd69ba1d18c7df60717 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Jul 2023 13:50:33 +0200 Subject: [PATCH 03/26] Add connection data to django db spans. --- sentry_sdk/integrations/django/__init__.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 75b529062e..cc85142fde 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -612,7 +612,7 @@ def execute(self, sql, params=None): with record_sql_queries( hub, self.cursor, sql, params, paramstyle="format", executemany=False ) as span: - _set_db_system_on_span(span, self.db.vendor) + set_db_data(span, self.db.vendor, self.db.get_connection_params()) return real_execute(self, sql, params) def executemany(self, sql, param_list): @@ -624,7 +624,7 @@ def executemany(self, sql, param_list): with record_sql_queries( hub, self.cursor, sql, param_list, paramstyle="format", executemany=True ) as span: - _set_db_system_on_span(span, self.db.vendor) + set_db_data(span, self.db.vendor, self.db.get_connection_params()) return real_executemany(self, sql, param_list) def connect(self): @@ -637,7 +637,7 @@ def connect(self): hub.add_breadcrumb(message="connect", category="query") with hub.start_span(op=OP.DB, description="connect") as span: - _set_db_system_on_span(span, self.vendor) + set_db_data(span, self.vendor, self.get_connection_params()) return real_connect(self) CursorWrapper.execute = execute @@ -646,8 +646,9 @@ def connect(self): ignore_logger("django.db.backends") -# https://github.com/django/django/blob/6a0dc2176f4ebf907e124d433411e52bba39a28e/django/db/backends/base/base.py#L29 -# Avaliable in Django 1.8+ -def _set_db_system_on_span(span, vendor): - # type: (Span, str) -> None +def set_db_data(span, vendor, connection_params): + # type: (Span, str, Dict[str, str]) -> None span.set_data(SPANDATA.DB_SYSTEM, vendor) + span.set_data(SPANDATA.DB_NAME, connection_params.get("database")) + span.set_data(SPANDATA.SERVER_ADDRESS, connection_params.get("host")) + span.set_data(SPANDATA.SERVER_PORT, connection_params.get("port")) From 97ee7e53ffd18d00524b88a594de7b7757b42e06 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Jul 2023 13:52:34 +0200 Subject: [PATCH 04/26] Cleanup --- sentry_sdk/integrations/sqlalchemy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/sqlalchemy.py b/sentry_sdk/integrations/sqlalchemy.py index 9853bea78b..f724b6ea53 100644 --- a/sentry_sdk/integrations/sqlalchemy.py +++ b/sentry_sdk/integrations/sqlalchemy.py @@ -130,11 +130,11 @@ def _get_db_system(name): def set_db_data(span, conn): # type: (Span, Any) -> None - span.set_data(SPANDATA.SERVER_ADDRESS, conn.engine.url.host) - span.set_data(SPANDATA.SERVER_PORT, conn.engine.url.port) - db_system = _get_db_system(conn.engine.name) if db_system is not None: span.set_data(SPANDATA.DB_SYSTEM, db_system) span.set_data(SPANDATA.DB_NAME, conn.engine.url.database) + + span.set_data(SPANDATA.SERVER_ADDRESS, conn.engine.url.host) + span.set_data(SPANDATA.SERVER_PORT, conn.engine.url.port) From 62d31c25eac4009f7e7ede8525c5dee63e295348 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Jul 2023 13:54:30 +0200 Subject: [PATCH 05/26] Cleanup --- sentry_sdk/integrations/django/__init__.py | 8 ++++---- sentry_sdk/integrations/sqlalchemy.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index cc85142fde..2661970aae 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -612,7 +612,7 @@ def execute(self, sql, params=None): with record_sql_queries( hub, self.cursor, sql, params, paramstyle="format", executemany=False ) as span: - set_db_data(span, self.db.vendor, self.db.get_connection_params()) + _set_db_data(span, self.db.vendor, self.db.get_connection_params()) return real_execute(self, sql, params) def executemany(self, sql, param_list): @@ -624,7 +624,7 @@ def executemany(self, sql, param_list): with record_sql_queries( hub, self.cursor, sql, param_list, paramstyle="format", executemany=True ) as span: - set_db_data(span, self.db.vendor, self.db.get_connection_params()) + _set_db_data(span, self.db.vendor, self.db.get_connection_params()) return real_executemany(self, sql, param_list) def connect(self): @@ -637,7 +637,7 @@ def connect(self): hub.add_breadcrumb(message="connect", category="query") with hub.start_span(op=OP.DB, description="connect") as span: - set_db_data(span, self.vendor, self.get_connection_params()) + _set_db_data(span, self.vendor, self.get_connection_params()) return real_connect(self) CursorWrapper.execute = execute @@ -646,7 +646,7 @@ def connect(self): ignore_logger("django.db.backends") -def set_db_data(span, vendor, connection_params): +def _set_db_data(span, vendor, connection_params): # type: (Span, str, Dict[str, str]) -> None span.set_data(SPANDATA.DB_SYSTEM, vendor) span.set_data(SPANDATA.DB_NAME, connection_params.get("database")) diff --git a/sentry_sdk/integrations/sqlalchemy.py b/sentry_sdk/integrations/sqlalchemy.py index f724b6ea53..68fa1404e3 100644 --- a/sentry_sdk/integrations/sqlalchemy.py +++ b/sentry_sdk/integrations/sqlalchemy.py @@ -67,7 +67,7 @@ def _before_cursor_execute( span = ctx_mgr.__enter__() if span is not None: - set_db_data(span, conn) + _set_db_data(span, conn) context._sentry_sql_span = span @@ -128,7 +128,7 @@ def _get_db_system(name): return None -def set_db_data(span, conn): +def _set_db_data(span, conn): # type: (Span, Any) -> None db_system = _get_db_system(conn.engine.name) if db_system is not None: From ac3e3f5393c55d6990570aa0203197111c253a02 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Jul 2023 16:19:31 +0200 Subject: [PATCH 06/26] Added db connection data to redis spans --- sentry_sdk/integrations/redis/__init__.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/redis/__init__.py b/sentry_sdk/integrations/redis/__init__.py index 45409a22d9..2fad3673be 100644 --- a/sentry_sdk/integrations/redis/__init__.py +++ b/sentry_sdk/integrations/redis/__init__.py @@ -13,7 +13,7 @@ from sentry_sdk._types import TYPE_CHECKING if TYPE_CHECKING: - from typing import Any, Sequence + from typing import Any, Dict, Sequence from sentry_sdk.tracing import Span _SINGLE_KEY_COMMANDS = frozenset( @@ -99,6 +99,7 @@ def sentry_patched_execute(self, *args, **kwargs): op=OP.DB_REDIS, description="redis.pipeline.execute" ) as span: with capture_internal_exceptions(): + _set_db_data(span, self.connection_pool.connection_kwargs) _set_pipeline_data( span, is_cluster, @@ -106,7 +107,6 @@ def sentry_patched_execute(self, *args, **kwargs): self.transaction, self.command_stack, ) - span.set_data(SPANDATA.DB_SYSTEM, "redis") return old_execute(self, *args, **kwargs) @@ -220,7 +220,6 @@ def _get_span_description(name, *args): def _set_client_data(span, is_cluster, name, *args): # type: (Span, bool, str, *Any) -> None - span.set_data(SPANDATA.DB_SYSTEM, "redis") span.set_tag("redis.is_cluster", is_cluster) if name: span.set_tag("redis.command", name) @@ -234,6 +233,14 @@ def _set_client_data(span, is_cluster, name, *args): span.set_tag("redis.key", args[0]) +def _set_db_data(span, connection_params): + # type: (Span, Dict[str, Any]) -> None + span.set_data(SPANDATA.DB_SYSTEM, "redis") + span.set_data(SPANDATA.DB_NAME, connection_params.get("db")) + span.set_data(SPANDATA.SERVER_ADDRESS, connection_params.get("host")) + span.set_data(SPANDATA.SERVER_PORT, connection_params.get("port")) + + def patch_redis_client(cls, is_cluster): # type: (Any, bool) -> None """ @@ -259,6 +266,7 @@ def sentry_patched_execute_command(self, name, *args, **kwargs): description = description[: integration.max_data_size - len("...")] + "..." with hub.start_span(op=OP.DB_REDIS, description=description) as span: + _set_db_data(span, self.connection_pool.connection_kwargs) _set_client_data(span, is_cluster, name, *args) return old_execute_command(self, name, *args, **kwargs) From 561c50fe22e93d6b8b9cc5d6546da6ef5b742da9 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Jul 2023 16:28:18 +0200 Subject: [PATCH 07/26] Fixed tests --- tests/integrations/redis/test_redis.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/integrations/redis/test_redis.py b/tests/integrations/redis/test_redis.py index e5d760b018..f6fdd3c208 100644 --- a/tests/integrations/redis/test_redis.py +++ b/tests/integrations/redis/test_redis.py @@ -73,6 +73,11 @@ def test_redis_pipeline( "first_ten": expected_first_ten, }, SPANDATA.DB_SYSTEM: "redis", + SPANDATA.DB_NAME: "0", + SPANDATA.SERVER_ADDRESS: connection.connection_pool.connection_kwargs.get( + "host" + ), + SPANDATA.SERVER_PORT: 6379, } assert span["tags"] == { "redis.transaction": is_transaction, From 41182c01ed5b45928d018da0b1a17f9b1471993d Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Jul 2023 16:32:09 +0200 Subject: [PATCH 08/26] Made redis db name a string --- sentry_sdk/integrations/redis/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/redis/__init__.py b/sentry_sdk/integrations/redis/__init__.py index 2fad3673be..613381ba75 100644 --- a/sentry_sdk/integrations/redis/__init__.py +++ b/sentry_sdk/integrations/redis/__init__.py @@ -2,6 +2,7 @@ from sentry_sdk import Hub from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk._compat import text_type from sentry_sdk.hub import _should_send_default_pii from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, @@ -236,7 +237,7 @@ def _set_client_data(span, is_cluster, name, *args): def _set_db_data(span, connection_params): # type: (Span, Dict[str, Any]) -> None span.set_data(SPANDATA.DB_SYSTEM, "redis") - span.set_data(SPANDATA.DB_NAME, connection_params.get("db")) + span.set_data(SPANDATA.DB_NAME, text_type(connection_params.get("db"))) span.set_data(SPANDATA.SERVER_ADDRESS, connection_params.get("host")) span.set_data(SPANDATA.SERVER_PORT, connection_params.get("port")) From 0163e8be816461f7e4a336f12d4e27fdd98ac66a Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 08:14:57 +0200 Subject: [PATCH 09/26] Fixed some tests and some redis tinkering --- sentry_sdk/integrations/redis/__init__.py | 23 +++++++++++-------- tests/integrations/pymongo/test_pymongo.py | 3 +++ .../rediscluster/test_rediscluster.py | 16 ++++++++++++- .../sqlalchemy/test_sqlalchemy.py | 3 +++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/sentry_sdk/integrations/redis/__init__.py b/sentry_sdk/integrations/redis/__init__.py index 613381ba75..eb92de356a 100644 --- a/sentry_sdk/integrations/redis/__init__.py +++ b/sentry_sdk/integrations/redis/__init__.py @@ -99,15 +99,20 @@ def sentry_patched_execute(self, *args, **kwargs): with hub.start_span( op=OP.DB_REDIS, description="redis.pipeline.execute" ) as span: - with capture_internal_exceptions(): - _set_db_data(span, self.connection_pool.connection_kwargs) - _set_pipeline_data( - span, - is_cluster, - get_command_args_fn, - self.transaction, - self.command_stack, - ) + # with capture_internal_exceptions(): + # import ipdb; ipdb.set_trace() + print("#####################################") + print(dir(self.connection_pool)) + print("#####################################") + print(self.connection_pool) + _set_db_data(span, self.connection_pool.connection_kwargs) + _set_pipeline_data( + span, + is_cluster, + get_command_args_fn, + self.transaction, + self.command_stack, + ) return old_execute(self, *args, **kwargs) diff --git a/tests/integrations/pymongo/test_pymongo.py b/tests/integrations/pymongo/test_pymongo.py index 786c775e41..b556db2fd0 100644 --- a/tests/integrations/pymongo/test_pymongo.py +++ b/tests/integrations/pymongo/test_pymongo.py @@ -57,6 +57,9 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii): } for span in find, insert_success, insert_fail: assert span["data"][SPANDATA.DB_SYSTEM] == "mongodb" + assert span["data"][SPANDATA.DB_NAME] == "test_db" + assert span["data"][SPANDATA.SERVER_ADDRESS] == "" + assert span["data"][SPANDATA.SERVER_PORT] == "" for field, value in common_tags.items(): assert span["tags"][field] == value diff --git a/tests/integrations/rediscluster/test_rediscluster.py b/tests/integrations/rediscluster/test_rediscluster.py index 32eb8c4fa5..49b5e40e01 100644 --- a/tests/integrations/rediscluster/test_rediscluster.py +++ b/tests/integrations/rediscluster/test_rediscluster.py @@ -5,6 +5,7 @@ from sentry_sdk.integrations.redis import RedisIntegration import rediscluster +from fakeredis import FakeStrictRedis rediscluster_classes = [rediscluster.RedisCluster] @@ -69,7 +70,17 @@ def test_rediscluster_pipeline( ) events = capture_events() - rc = rediscluster.RedisCluster(connection_pool=True) + connection = FakeStrictRedis() + startup_nodes = [ + { + "host": connection.connection_pool.connection_kwargs.get("host"), + "port": connection.connection_pool.connection_kwargs.get("port"), + } + ] + print("////////////") + print(startup_nodes) + + rc = rediscluster.RedisCluster(startup_nodes=startup_nodes) with start_transaction(): pipeline = rc.pipeline() pipeline.get("foo") @@ -87,6 +98,9 @@ def test_rediscluster_pipeline( "first_ten": expected_first_ten, }, SPANDATA.DB_SYSTEM: "redis", + SPANDATA.DB_NAME: "0", + SPANDATA.SERVER_ADDRESS: rc.connection_pool.connection_kwargs.get("host"), + SPANDATA.SERVER_PORT: 6379, } assert span["tags"] == { "redis.transaction": False, # For Cluster, this is always False diff --git a/tests/integrations/sqlalchemy/test_sqlalchemy.py b/tests/integrations/sqlalchemy/test_sqlalchemy.py index b5e8254f62..3e2c2b207b 100644 --- a/tests/integrations/sqlalchemy/test_sqlalchemy.py +++ b/tests/integrations/sqlalchemy/test_sqlalchemy.py @@ -122,6 +122,9 @@ class Address(Base): for span in event["spans"]: assert span["data"][SPANDATA.DB_SYSTEM] == "sqlite" + assert span["data"][SPANDATA.DB_NAME] == "sqlite" + assert span["data"][SPANDATA.SERVER_ADDRESS] == "sqlite" + assert span["data"][SPANDATA.SERVER_PORT] == "sqlite" assert ( render_span_tree(event) From 5fcdee7e5901940075eea22002b2b8eaf5dca5d1 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 08:18:23 +0200 Subject: [PATCH 10/26] Reverted redis changes (will do in a separate pr) --- sentry_sdk/integrations/redis/__init__.py | 36 ++++++------------- .../rediscluster/test_rediscluster.py | 16 +-------- 2 files changed, 12 insertions(+), 40 deletions(-) diff --git a/sentry_sdk/integrations/redis/__init__.py b/sentry_sdk/integrations/redis/__init__.py index eb92de356a..45409a22d9 100644 --- a/sentry_sdk/integrations/redis/__init__.py +++ b/sentry_sdk/integrations/redis/__init__.py @@ -2,7 +2,6 @@ from sentry_sdk import Hub from sentry_sdk.consts import OP, SPANDATA -from sentry_sdk._compat import text_type from sentry_sdk.hub import _should_send_default_pii from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, @@ -14,7 +13,7 @@ from sentry_sdk._types import TYPE_CHECKING if TYPE_CHECKING: - from typing import Any, Dict, Sequence + from typing import Any, Sequence from sentry_sdk.tracing import Span _SINGLE_KEY_COMMANDS = frozenset( @@ -99,20 +98,15 @@ def sentry_patched_execute(self, *args, **kwargs): with hub.start_span( op=OP.DB_REDIS, description="redis.pipeline.execute" ) as span: - # with capture_internal_exceptions(): - # import ipdb; ipdb.set_trace() - print("#####################################") - print(dir(self.connection_pool)) - print("#####################################") - print(self.connection_pool) - _set_db_data(span, self.connection_pool.connection_kwargs) - _set_pipeline_data( - span, - is_cluster, - get_command_args_fn, - self.transaction, - self.command_stack, - ) + with capture_internal_exceptions(): + _set_pipeline_data( + span, + is_cluster, + get_command_args_fn, + self.transaction, + self.command_stack, + ) + span.set_data(SPANDATA.DB_SYSTEM, "redis") return old_execute(self, *args, **kwargs) @@ -226,6 +220,7 @@ def _get_span_description(name, *args): def _set_client_data(span, is_cluster, name, *args): # type: (Span, bool, str, *Any) -> None + span.set_data(SPANDATA.DB_SYSTEM, "redis") span.set_tag("redis.is_cluster", is_cluster) if name: span.set_tag("redis.command", name) @@ -239,14 +234,6 @@ def _set_client_data(span, is_cluster, name, *args): span.set_tag("redis.key", args[0]) -def _set_db_data(span, connection_params): - # type: (Span, Dict[str, Any]) -> None - span.set_data(SPANDATA.DB_SYSTEM, "redis") - span.set_data(SPANDATA.DB_NAME, text_type(connection_params.get("db"))) - span.set_data(SPANDATA.SERVER_ADDRESS, connection_params.get("host")) - span.set_data(SPANDATA.SERVER_PORT, connection_params.get("port")) - - def patch_redis_client(cls, is_cluster): # type: (Any, bool) -> None """ @@ -272,7 +259,6 @@ def sentry_patched_execute_command(self, name, *args, **kwargs): description = description[: integration.max_data_size - len("...")] + "..." with hub.start_span(op=OP.DB_REDIS, description=description) as span: - _set_db_data(span, self.connection_pool.connection_kwargs) _set_client_data(span, is_cluster, name, *args) return old_execute_command(self, name, *args, **kwargs) diff --git a/tests/integrations/rediscluster/test_rediscluster.py b/tests/integrations/rediscluster/test_rediscluster.py index 49b5e40e01..32eb8c4fa5 100644 --- a/tests/integrations/rediscluster/test_rediscluster.py +++ b/tests/integrations/rediscluster/test_rediscluster.py @@ -5,7 +5,6 @@ from sentry_sdk.integrations.redis import RedisIntegration import rediscluster -from fakeredis import FakeStrictRedis rediscluster_classes = [rediscluster.RedisCluster] @@ -70,17 +69,7 @@ def test_rediscluster_pipeline( ) events = capture_events() - connection = FakeStrictRedis() - startup_nodes = [ - { - "host": connection.connection_pool.connection_kwargs.get("host"), - "port": connection.connection_pool.connection_kwargs.get("port"), - } - ] - print("////////////") - print(startup_nodes) - - rc = rediscluster.RedisCluster(startup_nodes=startup_nodes) + rc = rediscluster.RedisCluster(connection_pool=True) with start_transaction(): pipeline = rc.pipeline() pipeline.get("foo") @@ -98,9 +87,6 @@ def test_rediscluster_pipeline( "first_ten": expected_first_ten, }, SPANDATA.DB_SYSTEM: "redis", - SPANDATA.DB_NAME: "0", - SPANDATA.SERVER_ADDRESS: rc.connection_pool.connection_kwargs.get("host"), - SPANDATA.SERVER_PORT: 6379, } assert span["tags"] == { "redis.transaction": False, # For Cluster, this is always False From a47b99364831a02969df4f1d841ea40ad9ef15e3 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 08:27:39 +0200 Subject: [PATCH 11/26] Fixing hosts in tests --- tests/integrations/pymongo/test_pymongo.py | 2 +- tests/integrations/sqlalchemy/test_sqlalchemy.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integrations/pymongo/test_pymongo.py b/tests/integrations/pymongo/test_pymongo.py index b556db2fd0..a1a20ba1e7 100644 --- a/tests/integrations/pymongo/test_pymongo.py +++ b/tests/integrations/pymongo/test_pymongo.py @@ -58,7 +58,7 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii): for span in find, insert_success, insert_fail: assert span["data"][SPANDATA.DB_SYSTEM] == "mongodb" assert span["data"][SPANDATA.DB_NAME] == "test_db" - assert span["data"][SPANDATA.SERVER_ADDRESS] == "" + assert span["data"][SPANDATA.SERVER_ADDRESS] == "localhost" assert span["data"][SPANDATA.SERVER_PORT] == "" for field, value in common_tags.items(): assert span["tags"][field] == value diff --git a/tests/integrations/sqlalchemy/test_sqlalchemy.py b/tests/integrations/sqlalchemy/test_sqlalchemy.py index 3e2c2b207b..6877b642fc 100644 --- a/tests/integrations/sqlalchemy/test_sqlalchemy.py +++ b/tests/integrations/sqlalchemy/test_sqlalchemy.py @@ -122,7 +122,7 @@ class Address(Base): for span in event["spans"]: assert span["data"][SPANDATA.DB_SYSTEM] == "sqlite" - assert span["data"][SPANDATA.DB_NAME] == "sqlite" + assert span["data"][SPANDATA.DB_NAME] == ":memory:" assert span["data"][SPANDATA.SERVER_ADDRESS] == "sqlite" assert span["data"][SPANDATA.SERVER_PORT] == "sqlite" From a0641909b8bed8d3f1603af9213a21313b79cd2b Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 08:34:35 +0200 Subject: [PATCH 12/26] Fixing tests --- tests/integrations/pymongo/test_pymongo.py | 2 +- tests/integrations/redis/test_redis.py | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/integrations/pymongo/test_pymongo.py b/tests/integrations/pymongo/test_pymongo.py index a1a20ba1e7..89701c9f3a 100644 --- a/tests/integrations/pymongo/test_pymongo.py +++ b/tests/integrations/pymongo/test_pymongo.py @@ -59,7 +59,7 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii): assert span["data"][SPANDATA.DB_SYSTEM] == "mongodb" assert span["data"][SPANDATA.DB_NAME] == "test_db" assert span["data"][SPANDATA.SERVER_ADDRESS] == "localhost" - assert span["data"][SPANDATA.SERVER_PORT] == "" + assert span["data"][SPANDATA.SERVER_PORT] == mongo_server.port for field, value in common_tags.items(): assert span["tags"][field] == value diff --git a/tests/integrations/redis/test_redis.py b/tests/integrations/redis/test_redis.py index f6fdd3c208..e5d760b018 100644 --- a/tests/integrations/redis/test_redis.py +++ b/tests/integrations/redis/test_redis.py @@ -73,11 +73,6 @@ def test_redis_pipeline( "first_ten": expected_first_ten, }, SPANDATA.DB_SYSTEM: "redis", - SPANDATA.DB_NAME: "0", - SPANDATA.SERVER_ADDRESS: connection.connection_pool.connection_kwargs.get( - "host" - ), - SPANDATA.SERVER_PORT: 6379, } assert span["tags"] == { "redis.transaction": is_transaction, From c7aa22320a8496785ee830a1f1b2f2a8b380957a Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 08:39:49 +0200 Subject: [PATCH 13/26] Fixing tests --- tests/integrations/sqlalchemy/test_sqlalchemy.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/integrations/sqlalchemy/test_sqlalchemy.py b/tests/integrations/sqlalchemy/test_sqlalchemy.py index 6877b642fc..ba26c088f5 100644 --- a/tests/integrations/sqlalchemy/test_sqlalchemy.py +++ b/tests/integrations/sqlalchemy/test_sqlalchemy.py @@ -123,8 +123,12 @@ class Address(Base): for span in event["spans"]: assert span["data"][SPANDATA.DB_SYSTEM] == "sqlite" assert span["data"][SPANDATA.DB_NAME] == ":memory:" - assert span["data"][SPANDATA.SERVER_ADDRESS] == "sqlite" - assert span["data"][SPANDATA.SERVER_PORT] == "sqlite" + assert ( + span["data"][SPANDATA.SERVER_ADDRESS] is None + ) # sqlite does not have a server + assert ( + span["data"][SPANDATA.SERVER_PORT] is None + ) # sqlite does not have a server assert ( render_span_tree(event) From e6991b973a84189590c7b6b752af2349706312a1 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 09:45:07 +0200 Subject: [PATCH 14/26] Updated chalice versions --- tox.ini | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tox.ini b/tox.ini index 6800120050..02c028da76 100644 --- a/tox.ini +++ b/tox.ini @@ -231,11 +231,12 @@ deps = {py2.7,py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-celery: newrelic # Chalice - chalice-v1.16: chalice>=1.16.0,<1.17.0 - chalice-v1.17: chalice>=1.17.0,<1.18.0 chalice-v1.18: chalice>=1.18.0,<1.19.0 - chalice-v1.19: chalice>=1.19.0,<1.20.0 chalice-v1.20: chalice>=1.20.0,<1.21.0 + chalice-v1.22: chalice>=1.22.0,<1.23.0 + chalice-v1.24: chalice>=1.24.0,<1.25.0 + chalice-v1.26: chalice>=1.26.0,<1.27.0 + chalice-v1.28: chalice>=1.28.0,<1.29.0 chalice: pytest-chalice==0.0.5 # Django From c0330e40907284c7551e491ed6ebb5d639ccd901 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 10:14:17 +0200 Subject: [PATCH 15/26] Updated chalice deps --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 02c028da76..919c794166 100644 --- a/tox.ini +++ b/tox.ini @@ -50,7 +50,7 @@ envlist = # TODO: enable when celery is ready {py3.7,py3.8,py3.9,py3.10,py3.11}-celery-v{5.3} # Chalice - {py3.6,py3.7,py3.8}-chalice-v{1.16,1.17,1.18,1.19,1.20} + {py3.6,py3.7,py3.8}-chalice-v{1.18,1.20,1.22,1.24,1.26,1.28} # Cloud Resource Context {py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-cloud_resource_context From 6da2894ef2d3742bd13c74e336361e713b55461e Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 10:49:22 +0200 Subject: [PATCH 16/26] Removed mockupdb from general deps because it is only used in mongodb tests (and is in tox.ini already) --- test-requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index 4c43718bb1..4b04d1bcad 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -13,4 +13,3 @@ asttokens responses pysocks ipdb -mockupdb From 23581dacfd370871b35cb83b61ac5edc59d853de Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 10:56:41 +0200 Subject: [PATCH 17/26] Do not test newest chalice versions --- tox.ini | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tox.ini b/tox.ini index 919c794166..f16f8b4be1 100644 --- a/tox.ini +++ b/tox.ini @@ -50,7 +50,7 @@ envlist = # TODO: enable when celery is ready {py3.7,py3.8,py3.9,py3.10,py3.11}-celery-v{5.3} # Chalice - {py3.6,py3.7,py3.8}-chalice-v{1.18,1.20,1.22,1.24,1.26,1.28} + {py3.6,py3.7,py3.8}-chalice-v{1.18,1.20,1.22,1.24} # Cloud Resource Context {py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-cloud_resource_context @@ -235,8 +235,6 @@ deps = chalice-v1.20: chalice>=1.20.0,<1.21.0 chalice-v1.22: chalice>=1.22.0,<1.23.0 chalice-v1.24: chalice>=1.24.0,<1.25.0 - chalice-v1.26: chalice>=1.26.0,<1.27.0 - chalice-v1.28: chalice>=1.28.0,<1.29.0 chalice: pytest-chalice==0.0.5 # Django From 5e3a07fb624656f69c6486604906a360049eea7e Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 11:26:38 +0200 Subject: [PATCH 18/26] Better fetching of db attributes --- sentry_sdk/integrations/django/__init__.py | 19 +++++++++++++--- sentry_sdk/integrations/pymongo.py | 26 ++++++++++++++++++---- sentry_sdk/integrations/sqlalchemy.py | 13 ++++++++--- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 2661970aae..0e67ad1eae 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -649,6 +649,19 @@ def connect(self): def _set_db_data(span, vendor, connection_params): # type: (Span, str, Dict[str, str]) -> None span.set_data(SPANDATA.DB_SYSTEM, vendor) - span.set_data(SPANDATA.DB_NAME, connection_params.get("database")) - span.set_data(SPANDATA.SERVER_ADDRESS, connection_params.get("host")) - span.set_data(SPANDATA.SERVER_PORT, connection_params.get("port")) + + db_name = connection_params.get("dbname") or connection_params.get("database") + if db_name is not None: + span.set_data(SPANDATA.DB_NAME, db_name) + + server_address = connection_params.get("host") + if server_address is not None: + span.set_data(SPANDATA.SERVER_ADDRESS, server_address) + + server_port = connection_params.get("port") + if server_port is not None: + span.set_data(SPANDATA.SERVER_PORT, server_port) + + server_socket_address = connection_params.get("unix_socket") + if server_socket_address is not None: + span.set_data(SPANDATA.SERVER_SOCKET_ADDRESS, server_socket_address) diff --git a/sentry_sdk/integrations/pymongo.py b/sentry_sdk/integrations/pymongo.py index 225ca90ebe..f34ff71a4e 100644 --- a/sentry_sdk/integrations/pymongo.py +++ b/sentry_sdk/integrations/pymongo.py @@ -85,6 +85,27 @@ def _strip_pii(command): return command +def _get_db_data(event): + # type: (Union[CommandFailedEvent, CommandStartedEvent, CommandSucceededEvent]) -> Dict[str, Any] + data = {} + + data[SPANDATA.DB_SYSTEM] = "mongodb" + + db_name = event.database_name + if db_name is None: + data[SPANDATA.DB_NAME] = db_name + + server_address = event.connection_id[0] + if server_address is not None: + data[SPANDATA.SERVER_ADDRESS] = server_address + + server_port = event.connection_id[1] + if server_port is not None: + data[SPANDATA.SERVER_PORT] = server_port + + return data + + class CommandTracer(monitoring.CommandListener): def __init__(self): # type: () -> None @@ -124,10 +145,7 @@ def started(self, event): data["operation_ids"]["operation"] = event.operation_id data["operation_ids"]["request"] = event.request_id - data[SPANDATA.DB_SYSTEM] = "mongodb" - data[SPANDATA.DB_NAME] = event.database_name - data[SPANDATA.SERVER_ADDRESS] = event.connection_id[0] - data[SPANDATA.SERVER_PORT] = event.connection_id[1] + data.update(_get_db_data(event)) try: lsid = command.pop("lsid")["id"] diff --git a/sentry_sdk/integrations/sqlalchemy.py b/sentry_sdk/integrations/sqlalchemy.py index 68fa1404e3..bd65141e2c 100644 --- a/sentry_sdk/integrations/sqlalchemy.py +++ b/sentry_sdk/integrations/sqlalchemy.py @@ -134,7 +134,14 @@ def _set_db_data(span, conn): if db_system is not None: span.set_data(SPANDATA.DB_SYSTEM, db_system) - span.set_data(SPANDATA.DB_NAME, conn.engine.url.database) + db_name = conn.engine.url.database + if db_name is not None: + span.set_data(SPANDATA.DB_NAME, db_name) - span.set_data(SPANDATA.SERVER_ADDRESS, conn.engine.url.host) - span.set_data(SPANDATA.SERVER_PORT, conn.engine.url.port) + server_address = conn.engine.url.host + if server_address is not None: + span.set_data(SPANDATA.SERVER_ADDRESS, server_address) + + server_port = conn.engine.url.port + if server_port is not None: + span.set_data(SPANDATA.SERVER_PORT, server_port) From dfa12a4f81045a9f83fde7a14c12109a7b31b825 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 11:37:11 +0200 Subject: [PATCH 19/26] fix --- sentry_sdk/integrations/pymongo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/pymongo.py b/sentry_sdk/integrations/pymongo.py index f34ff71a4e..d64f4c8223 100644 --- a/sentry_sdk/integrations/pymongo.py +++ b/sentry_sdk/integrations/pymongo.py @@ -92,7 +92,7 @@ def _get_db_data(event): data[SPANDATA.DB_SYSTEM] = "mongodb" db_name = event.database_name - if db_name is None: + if db_name is not None: data[SPANDATA.DB_NAME] = db_name server_address = event.connection_id[0] From 0be3b1b1b249127fb094da7b73a744f7002b9c12 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 28 Jul 2023 11:45:15 +0200 Subject: [PATCH 20/26] fix tests --- sentry_sdk/integrations/pymongo.py | 2 +- tests/integrations/sqlalchemy/test_sqlalchemy.py | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/pymongo.py b/sentry_sdk/integrations/pymongo.py index d64f4c8223..59001bb937 100644 --- a/sentry_sdk/integrations/pymongo.py +++ b/sentry_sdk/integrations/pymongo.py @@ -86,7 +86,7 @@ def _strip_pii(command): def _get_db_data(event): - # type: (Union[CommandFailedEvent, CommandStartedEvent, CommandSucceededEvent]) -> Dict[str, Any] + # type: (Any) -> Dict[str, Any] data = {} data[SPANDATA.DB_SYSTEM] = "mongodb" diff --git a/tests/integrations/sqlalchemy/test_sqlalchemy.py b/tests/integrations/sqlalchemy/test_sqlalchemy.py index ba26c088f5..eb1792b3be 100644 --- a/tests/integrations/sqlalchemy/test_sqlalchemy.py +++ b/tests/integrations/sqlalchemy/test_sqlalchemy.py @@ -123,12 +123,8 @@ class Address(Base): for span in event["spans"]: assert span["data"][SPANDATA.DB_SYSTEM] == "sqlite" assert span["data"][SPANDATA.DB_NAME] == ":memory:" - assert ( - span["data"][SPANDATA.SERVER_ADDRESS] is None - ) # sqlite does not have a server - assert ( - span["data"][SPANDATA.SERVER_PORT] is None - ) # sqlite does not have a server + assert SPANDATA.SERVER_ADDRESS not in span["data"] + assert SPANDATA.SERVER_PORT not in span["data"] assert ( render_span_tree(event) From 8e9c8baacef160018c2f74b19f15be57fed6959f Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Fri, 28 Jul 2023 14:19:32 +0200 Subject: [PATCH 21/26] remove old unused test args --- tests/integrations/django/test_basic.py | 34 +++++-------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 0af5909fe7..0e86bf202e 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -584,9 +584,7 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t @pytest.mark.forked @pytest_mark_django_db_decorator(transaction=True) -def test_django_connect_breadcrumbs( - sentry_init, client, capture_events, render_span_tree -): +def test_django_connect_breadcrumbs(sentry_init, capture_events): """ Verify we record a breadcrumb when opening a new database. """ @@ -1058,13 +1056,7 @@ def dummy(a, b): @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_disabled_middleware( - sentry_init, - client, - capture_events, - use_django_caching_with_middlewares, - settings, -): +def test_cache_spans_disabled_middleware(sentry_init, client, capture_events): sentry_init( integrations=[ DjangoIntegration( @@ -1088,9 +1080,7 @@ def test_cache_spans_disabled_middleware( @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_disabled_decorator( - sentry_init, client, capture_events, use_django_caching -): +def test_cache_spans_disabled_decorator(sentry_init, client, capture_events): sentry_init( integrations=[ DjangoIntegration( @@ -1114,9 +1104,7 @@ def test_cache_spans_disabled_decorator( @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_disabled_templatetag( - sentry_init, client, capture_events, use_django_caching -): +def test_cache_spans_disabled_templatetag(sentry_init, client, capture_events): sentry_init( integrations=[ DjangoIntegration( @@ -1140,13 +1128,7 @@ def test_cache_spans_disabled_templatetag( @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_middleware( - sentry_init, - client, - capture_events, - use_django_caching_with_middlewares, - settings, -): +def test_cache_spans_middleware(sentry_init, client, capture_events): sentry_init( integrations=[ DjangoIntegration( @@ -1190,7 +1172,7 @@ def test_cache_spans_middleware( @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_decorator(sentry_init, client, capture_events, use_django_caching): +def test_cache_spans_decorator(sentry_init, client, capture_events): sentry_init( integrations=[ DjangoIntegration( @@ -1232,9 +1214,7 @@ def test_cache_spans_decorator(sentry_init, client, capture_events, use_django_c @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_templatetag( - sentry_init, client, capture_events, use_django_caching -): +def test_cache_spans_templatetag(sentry_init, client, capture_events): sentry_init( integrations=[ DjangoIntegration( From f0f9770f0b0de0f5b752145ca8ff44f747812914 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Fri, 28 Jul 2023 15:32:14 +0200 Subject: [PATCH 22/26] ok, maybe they were not unused --- tests/integrations/django/test_basic.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 0e86bf202e..4adbb357aa 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -1056,7 +1056,9 @@ def dummy(a, b): @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_disabled_middleware(sentry_init, client, capture_events): +def test_cache_spans_disabled_middleware( + sentry_init, client, capture_events, use_django_caching_with_middlewares +): sentry_init( integrations=[ DjangoIntegration( @@ -1080,7 +1082,9 @@ def test_cache_spans_disabled_middleware(sentry_init, client, capture_events): @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_disabled_decorator(sentry_init, client, capture_events): +def test_cache_spans_disabled_decorator( + sentry_init, client, capture_events, use_django_caching +): sentry_init( integrations=[ DjangoIntegration( @@ -1104,7 +1108,9 @@ def test_cache_spans_disabled_decorator(sentry_init, client, capture_events): @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_disabled_templatetag(sentry_init, client, capture_events): +def test_cache_spans_disabled_templatetag( + sentry_init, client, capture_events, use_django_caching +): sentry_init( integrations=[ DjangoIntegration( @@ -1128,7 +1134,9 @@ def test_cache_spans_disabled_templatetag(sentry_init, client, capture_events): @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_middleware(sentry_init, client, capture_events): +def test_cache_spans_middleware( + sentry_init, client, capture_events, use_django_caching_with_middlewares +): sentry_init( integrations=[ DjangoIntegration( @@ -1172,7 +1180,7 @@ def test_cache_spans_middleware(sentry_init, client, capture_events): @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_decorator(sentry_init, client, capture_events): +def test_cache_spans_decorator(sentry_init, client, capture_events, use_django_caching): sentry_init( integrations=[ DjangoIntegration( @@ -1214,7 +1222,9 @@ def test_cache_spans_decorator(sentry_init, client, capture_events): @pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") -def test_cache_spans_templatetag(sentry_init, client, capture_events): +def test_cache_spans_templatetag( + sentry_init, client, capture_events, use_django_caching +): sentry_init( integrations=[ DjangoIntegration( From 2914c0ca511ccce52d9a4169e474b2bb25eed692 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Fri, 28 Jul 2023 16:32:49 +0200 Subject: [PATCH 23/26] add django test --- tests/integrations/django/test_basic.py | 40 ++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 4adbb357aa..8842926e58 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -1,9 +1,10 @@ from __future__ import absolute_import import json +import os +import random import re import pytest -import random from functools import partial from werkzeug.test import Client @@ -618,6 +619,43 @@ def test_django_connect_breadcrumbs(sentry_init, capture_events): ] +@pytest.mark.forked +@pytest_mark_django_db_decorator(transaction=True) +def test_db_connection_span_data(sentry_init, client, capture_events): + sentry_init( + integrations=[DjangoIntegration()], + send_default_pii=True, + traces_sample_rate=1.0, + ) + from django.db import connections + + if "postgres" not in connections: + pytest.skip("postgres tests disabled") + + # trigger Django to open a new connection by marking the existing one as None. + connections["postgres"].connection = None + + events = capture_events() + + content, status, headers = client.get(reverse("postgres_select")) + assert status == "200 OK" + + (event,) = events + + for span in event["spans"]: + if span.get("op") == "db": + data = span.get("data") + assert data.get(SPANDATA.DB_SYSTEM) == "postgresql" + assert ( + data.get(SPANDATA.DB_NAME) + == "test_" + os.environ["SENTRY_PYTHON_TEST_POSTGRES_NAME"] + ) + assert data.get(SPANDATA.SERVER_ADDRESS) == os.environ.get( + "SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost" + ) + assert data.get(SPANDATA.SERVER_PORT) == 5432 + + @pytest.mark.parametrize( "transaction_style,client_url,expected_transaction,expected_source,expected_response", [ From ce0089e0d0bf3de6b4f83acb4ef963f2c87f9336 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Fri, 28 Jul 2023 16:34:35 +0200 Subject: [PATCH 24/26] hello, github? From b170287615d30ac65f7eb2011ae24c12ef13c4e9 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Fri, 28 Jul 2023 17:15:33 +0200 Subject: [PATCH 25/26] fix test --- tests/integrations/django/test_basic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 8842926e58..78cd16a027 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -648,7 +648,7 @@ def test_db_connection_span_data(sentry_init, client, capture_events): assert data.get(SPANDATA.DB_SYSTEM) == "postgresql" assert ( data.get(SPANDATA.DB_NAME) - == "test_" + os.environ["SENTRY_PYTHON_TEST_POSTGRES_NAME"] + == connections["postgres"].get_connection_params()["database"] ) assert data.get(SPANDATA.SERVER_ADDRESS) == os.environ.get( "SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost" From 50735cefcef5ec9dfe51cb1703a8269ec3054de7 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Fri, 28 Jul 2023 17:18:31 +0200 Subject: [PATCH 26/26] github having a day again