|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 |
|
| 5 | +from django import VERSION as DJANGO_VERSION |
| 6 | +from django.db import connections |
| 7 | + |
5 | 8 | try:
|
6 | 9 | from django.urls import reverse
|
7 | 10 | except ImportError:
|
8 | 11 | from django.core.urlresolvers import reverse
|
9 | 12 |
|
10 |
| -from django.db import connections |
11 |
| - |
12 | 13 | from werkzeug.test import Client
|
13 | 14 |
|
14 |
| -from sentry_sdk._compat import PY2 |
15 | 15 | from sentry_sdk.consts import SPANDATA
|
16 | 16 | from sentry_sdk.integrations.django import DjangoIntegration
|
17 | 17 |
|
@@ -102,24 +102,124 @@ def test_query_source(sentry_init, client, capture_events):
|
102 | 102 | assert type(data.get(SPANDATA.CODE_LINENO)) == int
|
103 | 103 | assert data.get(SPANDATA.CODE_LINENO) > 0
|
104 | 104 |
|
105 |
| - if PY2: |
| 105 | + assert ( |
| 106 | + data.get(SPANDATA.CODE_NAMESPACE) |
| 107 | + == "tests.integrations.django.myapp.views" |
| 108 | + ) |
| 109 | + assert data.get(SPANDATA.CODE_FILEPATH).endswith( |
| 110 | + "tests/integrations/django/myapp/views.py" |
| 111 | + ) |
| 112 | + assert data.get(SPANDATA.CODE_FUNCTION) == "postgres_select_orm" |
| 113 | + |
| 114 | + break |
| 115 | + else: |
| 116 | + raise AssertionError("No db span found") |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.forked |
| 120 | +@pytest_mark_django_db_decorator(transaction=True) |
| 121 | +def test_query_source_with_in_app_exclude(sentry_init, client, capture_events): |
| 122 | + sentry_init( |
| 123 | + integrations=[DjangoIntegration()], |
| 124 | + send_default_pii=True, |
| 125 | + traces_sample_rate=1.0, |
| 126 | + enable_db_query_source=True, |
| 127 | + db_query_source_threshold_ms=0, |
| 128 | + in_app_exclude=["tests.integrations.django.myapp.views"], |
| 129 | + ) |
| 130 | + |
| 131 | + if "postgres" not in connections: |
| 132 | + pytest.skip("postgres tests disabled") |
| 133 | + |
| 134 | + # trigger Django to open a new connection by marking the existing one as None. |
| 135 | + connections["postgres"].connection = None |
| 136 | + |
| 137 | + events = capture_events() |
| 138 | + |
| 139 | + _, status, _ = unpack_werkzeug_response(client.get(reverse("postgres_select_orm"))) |
| 140 | + assert status == "200 OK" |
| 141 | + |
| 142 | + (event,) = events |
| 143 | + for span in event["spans"]: |
| 144 | + if span.get("op") == "db" and "auth_user" in span.get("description"): |
| 145 | + data = span.get("data", {}) |
| 146 | + |
| 147 | + assert SPANDATA.CODE_LINENO in data |
| 148 | + assert SPANDATA.CODE_NAMESPACE in data |
| 149 | + assert SPANDATA.CODE_FILEPATH in data |
| 150 | + assert SPANDATA.CODE_FUNCTION in data |
| 151 | + |
| 152 | + assert type(data.get(SPANDATA.CODE_LINENO)) == int |
| 153 | + assert data.get(SPANDATA.CODE_LINENO) > 0 |
| 154 | + |
| 155 | + if DJANGO_VERSION >= (1, 11): |
106 | 156 | assert (
|
107 | 157 | data.get(SPANDATA.CODE_NAMESPACE)
|
108 |
| - == "tests.integrations.django.test_db_query_data" |
| 158 | + == "tests.integrations.django.myapp.settings" |
109 | 159 | )
|
110 | 160 | assert data.get(SPANDATA.CODE_FILEPATH).endswith(
|
111 |
| - "tests/integrations/django/test_db_query_data.py" |
| 161 | + "tests/integrations/django/myapp/settings.py" |
112 | 162 | )
|
113 |
| - assert data.get(SPANDATA.CODE_FUNCTION) == "test_query_source" |
| 163 | + assert data.get(SPANDATA.CODE_FUNCTION) == "middleware" |
114 | 164 | else:
|
115 | 165 | assert (
|
116 | 166 | data.get(SPANDATA.CODE_NAMESPACE)
|
117 |
| - == "tests.integrations.django.myapp.views" |
| 167 | + == "tests.integrations.django.test_db_query_data" |
118 | 168 | )
|
119 | 169 | assert data.get(SPANDATA.CODE_FILEPATH).endswith(
|
120 |
| - "tests/integrations/django/myapp/views.py" |
| 170 | + "tests/integrations/django/test_db_query_data.py" |
121 | 171 | )
|
122 |
| - assert data.get(SPANDATA.CODE_FUNCTION) == "postgres_select_orm" |
| 172 | + assert ( |
| 173 | + data.get(SPANDATA.CODE_FUNCTION) |
| 174 | + == "test_query_source_with_in_app_exclude" |
| 175 | + ) |
| 176 | + |
| 177 | + break |
| 178 | + else: |
| 179 | + raise AssertionError("No db span found") |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.forked |
| 183 | +@pytest_mark_django_db_decorator(transaction=True) |
| 184 | +def test_query_source_with_in_app_include(sentry_init, client, capture_events): |
| 185 | + sentry_init( |
| 186 | + integrations=[DjangoIntegration()], |
| 187 | + send_default_pii=True, |
| 188 | + traces_sample_rate=1.0, |
| 189 | + enable_db_query_source=True, |
| 190 | + db_query_source_threshold_ms=0, |
| 191 | + in_app_include=["django"], |
| 192 | + ) |
| 193 | + |
| 194 | + if "postgres" not in connections: |
| 195 | + pytest.skip("postgres tests disabled") |
| 196 | + |
| 197 | + # trigger Django to open a new connection by marking the existing one as None. |
| 198 | + connections["postgres"].connection = None |
| 199 | + |
| 200 | + events = capture_events() |
| 201 | + |
| 202 | + _, status, _ = unpack_werkzeug_response(client.get(reverse("postgres_select_orm"))) |
| 203 | + assert status == "200 OK" |
| 204 | + |
| 205 | + (event,) = events |
| 206 | + for span in event["spans"]: |
| 207 | + if span.get("op") == "db" and "auth_user" in span.get("description"): |
| 208 | + data = span.get("data", {}) |
| 209 | + |
| 210 | + assert SPANDATA.CODE_LINENO in data |
| 211 | + assert SPANDATA.CODE_NAMESPACE in data |
| 212 | + assert SPANDATA.CODE_FILEPATH in data |
| 213 | + assert SPANDATA.CODE_FUNCTION in data |
| 214 | + |
| 215 | + assert type(data.get(SPANDATA.CODE_LINENO)) == int |
| 216 | + assert data.get(SPANDATA.CODE_LINENO) > 0 |
| 217 | + |
| 218 | + assert data.get(SPANDATA.CODE_NAMESPACE) == "django.db.models.sql.compiler" |
| 219 | + assert data.get(SPANDATA.CODE_FILEPATH).endswith( |
| 220 | + "django/db/models/sql/compiler.py" |
| 221 | + ) |
| 222 | + assert data.get(SPANDATA.CODE_FUNCTION) == "execute_sql" |
123 | 223 | break
|
124 | 224 | else:
|
125 | 225 | raise AssertionError("No db span found")
|
0 commit comments