From 0a73f19ae048f622901666325e08bf33782f33e0 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Tue, 25 Apr 2023 14:29:26 +0200 Subject: [PATCH 01/17] feat(django): Add `db.system` --- sentry_sdk/integrations/django/__init__.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index ab68a396c7..71bf9e0b83 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -6,7 +6,7 @@ import weakref from sentry_sdk._types import TYPE_CHECKING -from sentry_sdk.consts import OP +from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.hub import Hub, _should_send_default_pii from sentry_sdk.scope import add_global_event_processor from sentry_sdk.serializer import add_global_repr_processor @@ -64,6 +64,7 @@ from django.http.request import QueryDict from django.utils.datastructures import MultiValueDict + from sentry_sdk.tracing import Span from sentry_sdk.scope import Scope from sentry_sdk.integrations.wsgi import _ScopedResponse from sentry_sdk._types import Event, Hint, EventProcessor, NotImplementedType @@ -578,7 +579,8 @@ 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) return real_execute(self, sql, params) def executemany(self, sql, param_list): @@ -589,7 +591,8 @@ 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) return real_executemany(self, sql, param_list) def connect(self): @@ -601,10 +604,18 @@ def connect(self): with capture_internal_exceptions(): hub.add_breadcrumb(message="connect", category="query") - with hub.start_span(op=OP.DB, description="connect"): + with hub.start_span(op=OP.DB, description="connect") as span: + _set_db_system_on_span(span, self.vendor) return real_connect(self) CursorWrapper.execute = execute CursorWrapper.executemany = executemany BaseDatabaseWrapper.connect = connect 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 + span.set_data(SPANDATA.DB_SYSTEM, vendor) From 3d1edad76b99a28e049628a26e9ece632b5e1328 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Tue, 25 Apr 2023 14:55:36 +0200 Subject: [PATCH 02/17] Add tests for django --- tests/integrations/django/test_basic.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index bc464af836..6278be30a3 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -18,6 +18,7 @@ from sentry_sdk._compat import PY2, PY310 from sentry_sdk import capture_message, capture_exception, configure_scope +from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name from sentry_sdk.integrations.executing import ExecutingIntegration @@ -442,12 +443,15 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t # trigger Django to open a new connection by marking the existing one as None. connections["postgres"].connection = None - events = capture_events() + (event,) = capture_events() content, status, headers = client.get(reverse("postgres_select")) assert status == "200 OK" - assert '- op="db": description="connect"' in render_span_tree(events[0]) + for span in event["spans"]: + assert span["data"][SPANDATA.DB_SYSTEM] == "postgres" + + assert '- op="db": description="connect"' in render_span_tree(event) @pytest.mark.forked From da0d0c316b2509792252973ab5b12456143b52e3 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 26 Apr 2023 13:16:36 +0200 Subject: [PATCH 03/17] change event logic --- tests/integrations/django/test_basic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 6278be30a3..ec184d4811 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -443,11 +443,12 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t # trigger Django to open a new connection by marking the existing one as None. connections["postgres"].connection = None - (event,) = capture_events() + events = capture_events() content, status, headers = client.get(reverse("postgres_select")) assert status == "200 OK" + (event,) = events for span in event["spans"]: assert span["data"][SPANDATA.DB_SYSTEM] == "postgres" From 4724c99eace244991a1f33e6b3cf75ca6a521858 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 26 Apr 2023 14:07:06 +0200 Subject: [PATCH 04/17] test why event doesn't have data --- tests/integrations/django/test_basic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index ec184d4811..009e528e39 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -449,6 +449,9 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t assert status == "200 OK" (event,) = events + + print(event) + for span in event["spans"]: assert span["data"][SPANDATA.DB_SYSTEM] == "postgres" From ed53ea633081d0d3bc688f4768b680360753a9aa Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 26 Apr 2023 14:07:42 +0200 Subject: [PATCH 05/17] only assert on db spans --- tests/integrations/django/test_basic.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 009e528e39..a7fb9dfcdd 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -450,10 +450,9 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t (event,) = events - print(event) - for span in event["spans"]: - assert span["data"][SPANDATA.DB_SYSTEM] == "postgres" + if span.op == "db": + assert span["data"][SPANDATA.DB_SYSTEM] == "postgres" assert '- op="db": description="connect"' in render_span_tree(event) From 6b7f07c6196fa0963df2a4db805262c10e93243d Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 26 Apr 2023 14:30:51 +0200 Subject: [PATCH 06/17] it dict everywhere --- tests/integrations/django/test_basic.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index a7fb9dfcdd..b108277157 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -451,8 +451,9 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t (event,) = events for span in event["spans"]: - if span.op == "db": - assert span["data"][SPANDATA.DB_SYSTEM] == "postgres" + if span.get("op") == "db": + data = span.get("data") + assert data.get(SPANDATA.DB_SYSTEM) == "postgres" assert '- op="db": description="connect"' in render_span_tree(event) From 7aff2c0e982baf49ec666c4782cc4baf25163ca9 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 26 Apr 2023 15:39:34 +0200 Subject: [PATCH 07/17] use postgresql --- 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 b108277157..201854d552 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -453,7 +453,7 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t for span in event["spans"]: if span.get("op") == "db": data = span.get("data") - assert data.get(SPANDATA.DB_SYSTEM) == "postgres" + assert data.get(SPANDATA.DB_SYSTEM) == "postgresql" assert '- op="db": description="connect"' in render_span_tree(event) From aae7e1e5b72f771212a9f6ac6f23000ed512e77e Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 13:36:33 +0200 Subject: [PATCH 08/17] Pinned werkzeug version for linters env --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index 7a7b314fb2..bd15a8bea7 100644 --- a/tox.ini +++ b/tox.ini @@ -166,6 +166,7 @@ deps = py3.8-common: hypothesis linters: -r linter-requirements.txt + linters: werkzeug<2.3.0 # Common {py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-common: pytest-asyncio From bf193125e38454cbb97333781893b8cac96c7c47 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 13:51:22 +0200 Subject: [PATCH 09/17] Pinning urllib version for boto3 tests --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index bd15a8bea7..4ab88ee6a9 100644 --- a/tox.ini +++ b/tox.ini @@ -197,6 +197,7 @@ deps = beam-master: git+https://github.com/apache/beam#egg=apache-beam&subdirectory=sdks/python # Boto3 + {py3.7,py3.8}-boto3: urllib3<1.26 boto3-v1.9: boto3>=1.9,<1.10 boto3-v1.10: boto3>=1.10,<1.11 boto3-v1.11: boto3>=1.11,<1.12 From 917d08421b6c1fa6b8c16fd51e16dfe30296bf50 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 14:19:04 +0200 Subject: [PATCH 10/17] trying something --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 4ab88ee6a9..25f68759e5 100644 --- a/tox.ini +++ b/tox.ini @@ -159,6 +159,7 @@ deps = # if you change test-requirements.txt and your change is not being reflected # in what's installed by tox (when running tox locally), try running tox # with the -r flag + urrlib3<2.0.0 -r test-requirements.txt py3.4-common: colorama==0.4.1 @@ -197,7 +198,6 @@ deps = beam-master: git+https://github.com/apache/beam#egg=apache-beam&subdirectory=sdks/python # Boto3 - {py3.7,py3.8}-boto3: urllib3<1.26 boto3-v1.9: boto3>=1.9,<1.10 boto3-v1.10: boto3>=1.10,<1.11 boto3-v1.11: boto3>=1.11,<1.12 From c5f2bb5a17c17b8429734fd6cf488eac4a6da8b5 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 14:21:13 +0200 Subject: [PATCH 11/17] . --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 25f68759e5..3d4c87bf86 100644 --- a/tox.ini +++ b/tox.ini @@ -159,7 +159,7 @@ deps = # if you change test-requirements.txt and your change is not being reflected # in what's installed by tox (when running tox locally), try running tox # with the -r flag - urrlib3<2.0.0 + urllib3<2.0.0 -r test-requirements.txt py3.4-common: colorama==0.4.1 From a68def16a522952a46be34f92f20f5ea2fe9ac64 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 14:33:01 +0200 Subject: [PATCH 12/17] . --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 3d4c87bf86..19c463905a 100644 --- a/tox.ini +++ b/tox.ini @@ -159,7 +159,6 @@ deps = # if you change test-requirements.txt and your change is not being reflected # in what's installed by tox (when running tox locally), try running tox # with the -r flag - urllib3<2.0.0 -r test-requirements.txt py3.4-common: colorama==0.4.1 @@ -198,6 +197,7 @@ deps = beam-master: git+https://github.com/apache/beam#egg=apache-beam&subdirectory=sdks/python # Boto3 + boto3: urllib3<2.0.0 boto3-v1.9: boto3>=1.9,<1.10 boto3-v1.10: boto3>=1.10,<1.11 boto3-v1.11: boto3>=1.11,<1.12 From 4fd3899faedf39fda0c776beebfb6ca60592fd64 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 15:12:07 +0200 Subject: [PATCH 13/17] Trying to fix urllib version for tests --- test-requirements.txt | 3 +++ tox.ini | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index a70bd769d1..5b952ac213 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -12,3 +12,6 @@ executing asttokens responses ipdb +urllib3<2.0.0,>=1.25.7; python_version<="3.4" +urllib3<2.0.0,>=1.26.9; python_version=="3.5" +urllib3<2.0.0,>=1.26.11; python_version>="3.6" diff --git a/tox.ini b/tox.ini index 19c463905a..bd15a8bea7 100644 --- a/tox.ini +++ b/tox.ini @@ -197,7 +197,6 @@ deps = beam-master: git+https://github.com/apache/beam#egg=apache-beam&subdirectory=sdks/python # Boto3 - boto3: urllib3<2.0.0 boto3-v1.9: boto3>=1.9,<1.10 boto3-v1.10: boto3>=1.10,<1.11 boto3-v1.11: boto3>=1.11,<1.12 From 536d131c1bd0aa4110ec07ca2ab0924994a1c1c7 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 15:35:48 +0200 Subject: [PATCH 14/17] Again, dependencies --- test-requirements.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test-requirements.txt b/test-requirements.txt index 5b952ac213..6410de3927 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -12,6 +12,5 @@ executing asttokens responses ipdb -urllib3<2.0.0,>=1.25.7; python_version<="3.4" -urllib3<2.0.0,>=1.26.9; python_version=="3.5" -urllib3<2.0.0,>=1.26.11; python_version>="3.6" +# The tests do not run in urllib3 and the minimum version is taken from setup.py +urllib3<2.0.0,>=1.25.7 From 6c35730984afb2311efe311e6e0cfbef790e694a Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 15:41:54 +0200 Subject: [PATCH 15/17] Again, dependencies --- tox.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tox.ini b/tox.ini index bd15a8bea7..9c393ce986 100644 --- a/tox.ini +++ b/tox.ini @@ -504,6 +504,8 @@ basepython = linters: python3.11 commands = + {py3.7,py3.8}-boto3: pip install urllib<2.0.0 + ; https://github.com/pytest-dev/pytest/issues/5532 {py3.5,py3.6,py3.7,py3.8,py3.9}-flask-v{0.11,0.12}: pip install pytest<5 {py3.6,py3.7,py3.8,py3.9}-flask-v{0.11}: pip install Werkzeug<2 From ff61d37a3a4885fbaf2d892244f6bfb9a9f1ff0d Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 15:42:06 +0200 Subject: [PATCH 16/17] Again, dependencies --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 9c393ce986..7632af225f 100644 --- a/tox.ini +++ b/tox.ini @@ -504,7 +504,7 @@ basepython = linters: python3.11 commands = - {py3.7,py3.8}-boto3: pip install urllib<2.0.0 + {py3.7,py3.8}-boto3: pip install urllib3<2.0.0 ; https://github.com/pytest-dev/pytest/issues/5532 {py3.5,py3.6,py3.7,py3.8,py3.9}-flask-v{0.11,0.12}: pip install pytest<5 From 12ed192e36119b8a415a3141a66b1a6be82d644f Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 27 Apr 2023 15:50:06 +0200 Subject: [PATCH 17/17] Cleanup --- test-requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/test-requirements.txt b/test-requirements.txt index 6410de3927..a70bd769d1 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -12,5 +12,3 @@ executing asttokens responses ipdb -# The tests do not run in urllib3 and the minimum version is taken from setup.py -urllib3<2.0.0,>=1.25.7