Skip to content

Commit e0a9b5f

Browse files
committed
test(tracing): TODO
1 parent 531f8f7 commit e0a9b5f

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ def maybe_create_breadcrumbs_from_span(scope, span):
170170
)
171171

172172

173+
def _get_frame_module_abs_path(frame):
174+
# type: (FrameType) -> str
175+
try:
176+
return frame.f_code.co_filename
177+
except Exception:
178+
return ""
179+
180+
173181
def add_query_source(span):
174182
# type: (sentry_sdk.tracing.Span) -> None
175183
"""
@@ -200,10 +208,7 @@ def add_query_source(span):
200208
# Find the correct frame
201209
frame = sys._getframe() # type: Union[FrameType, None]
202210
while frame is not None:
203-
try:
204-
abs_path = frame.f_code.co_filename
205-
except Exception:
206-
abs_path = ""
211+
abs_path = _get_frame_module_abs_path(frame)
207212

208213
try:
209214
namespace = frame.f_globals.get("__name__") # type: Optional[str]
@@ -250,10 +255,7 @@ def add_query_source(span):
250255
if namespace is not None:
251256
span.set_data(SPANDATA.CODE_NAMESPACE, namespace)
252257

253-
try:
254-
filepath = frame.f_code.co_filename
255-
except Exception:
256-
filepath = None
258+
filepath = _get_frame_module_abs_path(frame)
257259
if filepath is not None:
258260
if namespace is not None:
259261
in_app_path = filename_for_module(namespace, filepath)

tests/integrations/django/test_db_query_data.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import contextlib
12
import os
3+
import sys
24

35
import pytest
46
from datetime import datetime
7+
from pathlib import Path
58
from unittest import mock
69

710
from django import VERSION as DJANGO_VERSION
@@ -17,7 +20,8 @@
1720
from sentry_sdk import start_transaction
1821
from sentry_sdk.consts import SPANDATA
1922
from sentry_sdk.integrations.django import DjangoIntegration
20-
from sentry_sdk.tracing_utils import record_sql_queries
23+
from sentry_sdk.tracing_utils import _get_frame_module_abs_path, record_sql_queries
24+
from sentry_sdk.utils import _module_in_list
2125

2226
from tests.conftest import unpack_werkzeug_response
2327
from tests.integrations.django.utils import pytest_mark_django_db_decorator
@@ -283,7 +287,10 @@ def test_query_source_with_in_app_exclude(sentry_init, client, capture_events):
283287

284288
@pytest.mark.forked
285289
@pytest_mark_django_db_decorator(transaction=True)
286-
def test_query_source_with_in_app_include(sentry_init, client, capture_events):
290+
@pytest.mark.parametrize("django_outside_of_project_root", [False, True])
291+
def test_query_source_with_in_app_include(
292+
sentry_init, client, capture_events, django_outside_of_project_root
293+
):
287294
sentry_init(
288295
integrations=[DjangoIntegration()],
289296
send_default_pii=True,
@@ -301,8 +308,35 @@ def test_query_source_with_in_app_include(sentry_init, client, capture_events):
301308

302309
events = capture_events()
303310

304-
_, status, _ = unpack_werkzeug_response(client.get(reverse("postgres_select_orm")))
305-
assert status == "200 OK"
311+
# Simulate Django installation outside of the project root
312+
original_get_frame_module_abs_path = _get_frame_module_abs_path
313+
314+
def patched_get_frame_module_abs_path_function(frame):
315+
result = original_get_frame_module_abs_path(frame)
316+
namespace = frame.f_globals.get("__name__")
317+
print(f"{namespace=}")
318+
print(f"{_module_in_list(namespace, ["django"])=}")
319+
if _module_in_list(namespace, ["django"]):
320+
print(f"{frame.f_code.co_filename=}")
321+
result = str(
322+
Path("/outside-of-project-root") / frame.f_code.co_filename[1:]
323+
)
324+
return result
325+
326+
patched_get_frame_module_abs_path = (
327+
mock.patch(
328+
"sentry_sdk.tracing_utils._get_frame_module_abs_path",
329+
patched_get_frame_module_abs_path_function,
330+
)
331+
if django_outside_of_project_root
332+
else contextlib.suppress()
333+
)
334+
335+
with patched_get_frame_module_abs_path:
336+
_, status, _ = unpack_werkzeug_response(
337+
client.get(reverse("postgres_select_orm"))
338+
)
339+
assert status == "200 OK"
306340

307341
(event,) = events
308342
for span in event["spans"]:

0 commit comments

Comments
 (0)