|
4 | 4 | import pytest |
5 | 5 | from datetime import datetime |
6 | 6 |
|
| 7 | +from sentry_sdk._compat import PY2 |
7 | 8 | from django import VERSION as DJANGO_VERSION |
8 | 9 | from django.db import connections |
9 | 10 |
|
@@ -168,6 +169,62 @@ def test_query_source(sentry_init, client, capture_events): |
168 | 169 | raise AssertionError("No db span found") |
169 | 170 |
|
170 | 171 |
|
| 172 | +@pytest.mark.forked |
| 173 | +@pytest_mark_django_db_decorator(transaction=True) |
| 174 | +def test_query_source_with_module_in_search_path(sentry_init, client, capture_events): |
| 175 | + """ |
| 176 | + Test that query source is relative to the path of the module it ran in |
| 177 | + """ |
| 178 | + client = Client(application) |
| 179 | + |
| 180 | + sentry_init( |
| 181 | + integrations=[DjangoIntegration()], |
| 182 | + send_default_pii=True, |
| 183 | + traces_sample_rate=1.0, |
| 184 | + enable_db_query_source=True, |
| 185 | + db_query_source_threshold_ms=0, |
| 186 | + ) |
| 187 | + |
| 188 | + if "postgres" not in connections: |
| 189 | + pytest.skip("postgres tests disabled") |
| 190 | + |
| 191 | + # trigger Django to open a new connection by marking the existing one as None. |
| 192 | + connections["postgres"].connection = None |
| 193 | + |
| 194 | + events = capture_events() |
| 195 | + |
| 196 | + _, status, _ = unpack_werkzeug_response( |
| 197 | + client.get(reverse("postgres_select_slow_from_supplement")) |
| 198 | + ) |
| 199 | + assert status == "200 OK" |
| 200 | + |
| 201 | + (event,) = events |
| 202 | + for span in event["spans"]: |
| 203 | + if span.get("op") == "db" and "auth_user" in span.get("description"): |
| 204 | + data = span.get("data", {}) |
| 205 | + |
| 206 | + assert SPANDATA.CODE_LINENO in data |
| 207 | + assert SPANDATA.CODE_NAMESPACE in data |
| 208 | + assert SPANDATA.CODE_FILEPATH in data |
| 209 | + assert SPANDATA.CODE_FUNCTION in data |
| 210 | + |
| 211 | + assert type(data.get(SPANDATA.CODE_LINENO)) == int |
| 212 | + assert data.get(SPANDATA.CODE_LINENO) > 0 |
| 213 | + |
| 214 | + if not PY2: |
| 215 | + assert data.get(SPANDATA.CODE_NAMESPACE) == "django_helpers.views" |
| 216 | + assert data.get(SPANDATA.CODE_FILEPATH) == "django_helpers/views.py" |
| 217 | + |
| 218 | + is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep |
| 219 | + assert is_relative_path |
| 220 | + |
| 221 | + assert data.get(SPANDATA.CODE_FUNCTION) == "postgres_select_orm" |
| 222 | + |
| 223 | + break |
| 224 | + else: |
| 225 | + raise AssertionError("No db span found") |
| 226 | + |
| 227 | + |
171 | 228 | @pytest.mark.forked |
172 | 229 | @pytest_mark_django_db_decorator(transaction=True) |
173 | 230 | def test_query_source_with_in_app_exclude(sentry_init, client, capture_events): |
|
0 commit comments