Skip to content

Commit e3bb1c0

Browse files
committed
ref(tracing): Move should_be_included logic into function
The change is required for unit testing the logic.
1 parent 6814df9 commit e3bb1c0

File tree

2 files changed

+130
-15
lines changed

2 files changed

+130
-15
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525
_module_in_list,
2626
)
2727

28-
from typing import TYPE_CHECKING
28+
from typing import List, Optional, TYPE_CHECKING
2929

3030
if TYPE_CHECKING:
3131
from typing import Any
3232
from typing import Dict
3333
from typing import Generator
34-
from typing import Optional
3534
from typing import Union
3635

3736
from types import FrameType
@@ -180,6 +179,27 @@ def _get_frame_module_abs_path(frame):
180179
return None
181180

182181

182+
def _should_be_included(
183+
is_sentry_sdk_frame: bool,
184+
namespace: Optional[str],
185+
in_app_include: Optional[List[str]],
186+
in_app_exclude: Optional[List[str]],
187+
abs_path: Optional[str],
188+
project_root: Optional[str],
189+
) -> bool:
190+
# in_app_include takes precedence over in_app_exclude
191+
should_be_included = (
192+
not (
193+
_is_external_source(abs_path) or _module_in_list(namespace, in_app_exclude)
194+
)
195+
) or _module_in_list(namespace, in_app_include)
196+
return (
197+
_is_in_project_root(abs_path, project_root)
198+
and should_be_included
199+
and not is_sentry_sdk_frame
200+
)
201+
202+
183203
def add_query_source(span):
184204
# type: (sentry_sdk.tracing.Span) -> None
185205
"""
@@ -221,19 +241,15 @@ def add_query_source(span):
221241
"sentry_sdk."
222242
)
223243

224-
# in_app_include takes precedence over in_app_exclude
225-
should_be_included = (
226-
not (
227-
_is_external_source(abs_path)
228-
or _module_in_list(namespace, in_app_exclude)
229-
)
230-
) or _module_in_list(namespace, in_app_include)
231-
232-
if (
233-
_is_in_project_root(abs_path, project_root)
234-
and should_be_included
235-
and not is_sentry_sdk_frame
236-
):
244+
should_be_included = _should_be_included(
245+
is_sentry_sdk_frame=is_sentry_sdk_frame,
246+
namespace=namespace,
247+
in_app_include=in_app_include,
248+
in_app_exclude=in_app_exclude,
249+
abs_path=abs_path,
250+
project_root=project_root,
251+
)
252+
if should_be_included:
237253
break
238254

239255
frame = frame.f_back

tests/test_tracing_utils.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from dataclasses import asdict, dataclass
2+
from typing import Optional, List, Any
3+
4+
from sentry_sdk.tracing_utils import _should_be_included
5+
import pytest
6+
7+
8+
def id_function(val: Any) -> str:
9+
if isinstance(val, ShouldBeIncludedTestCase):
10+
return val.id
11+
12+
13+
@dataclass(frozen=True)
14+
class ShouldBeIncludedTestCase:
15+
id: str
16+
is_sentry_sdk_frame: bool
17+
namespace: Optional[str] = None
18+
in_app_include: Optional[List[str]] = None
19+
in_app_exclude: Optional[List[str]] = None
20+
abs_path: Optional[str] = None
21+
project_root: Optional[str] = None
22+
23+
24+
@pytest.mark.parametrize(
25+
"test_case, expected",
26+
[
27+
(
28+
ShouldBeIncludedTestCase(
29+
id="Frame from Sentry SDK",
30+
is_sentry_sdk_frame=True,
31+
),
32+
False,
33+
),
34+
(
35+
ShouldBeIncludedTestCase(
36+
id="Frame from Django installed in virtualenv inside project root",
37+
is_sentry_sdk_frame=False,
38+
abs_path="/home/username/some_project/.venv/lib/python3.12/site-packages/django/db/models/sql/compiler",
39+
project_root="/home/username/some_project",
40+
namespace="django.db.models.sql.compiler",
41+
in_app_include=["django"],
42+
),
43+
True,
44+
),
45+
(
46+
ShouldBeIncludedTestCase(
47+
id="Frame from project",
48+
is_sentry_sdk_frame=False,
49+
abs_path="/home/username/some_project/some_project/__init__.py",
50+
project_root="/home/username/some_project",
51+
namespace="some_project",
52+
),
53+
True,
54+
),
55+
(
56+
ShouldBeIncludedTestCase(
57+
id="Frame from project module in `in_app_exclude`",
58+
is_sentry_sdk_frame=False,
59+
abs_path="/home/username/some_project/some_project/exclude_me/some_module.py",
60+
project_root="/home/username/some_project",
61+
namespace="some_project.exclude_me.some_module",
62+
in_app_exclude=["some_project.exclude_me"],
63+
),
64+
False,
65+
),
66+
(
67+
ShouldBeIncludedTestCase(
68+
id="Frame from system-wide installed Django",
69+
is_sentry_sdk_frame=False,
70+
abs_path="/usr/lib/python3.12/site-packages/django/db/models/sql/compiler",
71+
project_root="/home/username/some_project",
72+
namespace="django.db.models.sql.compiler",
73+
),
74+
False,
75+
),
76+
pytest.param(
77+
ShouldBeIncludedTestCase(
78+
id="Frame from system-wide installed Django with `django` in `in_app_include`",
79+
is_sentry_sdk_frame=False,
80+
abs_path="/usr/lib/python3.12/site-packages/django/db/models/sql/compiler",
81+
project_root="/home/username/some_project",
82+
namespace="django.db.models.sql.compiler",
83+
in_app_include=["django"],
84+
),
85+
True,
86+
marks=pytest.mark.xfail(
87+
reason="Bug, see https://github.com/getsentry/sentry-python/issues/3312"
88+
),
89+
),
90+
],
91+
ids=id_function,
92+
)
93+
def test_should_be_included(
94+
test_case: ShouldBeIncludedTestCase, expected: bool
95+
) -> None:
96+
"""Checking logic, see: https://github.com/getsentry/sentry-python/issues/3312"""
97+
kwargs = asdict(test_case)
98+
kwargs.pop("id")
99+
assert _should_be_included(**kwargs) == expected

0 commit comments

Comments
 (0)