Skip to content

Commit 96236b5

Browse files
committed
Remove propagate_scopes
1 parent 571ff3b commit 96236b5

File tree

2 files changed

+55
-98
lines changed

2 files changed

+55
-98
lines changed

sentry_sdk/integrations/threading.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
class ThreadingIntegration(Integration):
2828
identifier = "threading"
2929

30-
def __init__(self, propagate_scope=True):
31-
# type: (bool) -> None
32-
self.propagate_scope = propagate_scope
33-
3430
@staticmethod
3531
def setup_once():
3632
# type: () -> None
@@ -52,31 +48,27 @@ def sentry_start(self, *a, **kw):
5248
if integration is None:
5349
return old_start(self, *a, **kw)
5450

55-
if integration.propagate_scope:
56-
if (
57-
sys.version_info < (3, 9)
58-
and channels_version is not None
59-
and channels_version < "4.0.0"
60-
and django_version is not None
61-
and django_version >= (3, 0)
62-
and django_version < (4, 0)
63-
):
64-
warnings.warn(
65-
"There is a known issue with Django channels 2.x and 3.x when using Python 3.8 or older. "
66-
"(Async support is emulated using threads and some Sentry data may be leaked between those threads.) "
67-
"Please either upgrade to Django channels 4.0+, use Django's async features "
68-
"available in Django 3.1+ instead of Django channels, or upgrade to Python 3.9+.",
69-
stacklevel=2,
70-
)
71-
isolation_scope = sentry_sdk.get_isolation_scope()
72-
current_scope = sentry_sdk.get_current_scope()
73-
74-
else:
75-
isolation_scope = sentry_sdk.get_isolation_scope().fork()
76-
current_scope = sentry_sdk.get_current_scope().fork()
51+
if (
52+
sys.version_info < (3, 9)
53+
and channels_version is not None
54+
and channels_version < "4.0.0"
55+
and django_version is not None
56+
and django_version >= (3, 0)
57+
and django_version < (4, 0)
58+
):
59+
warnings.warn(
60+
"There is a known issue with Django channels 2.x and 3.x when using Python 3.8 or older. "
61+
"(Async support is emulated using threads and some Sentry data may be leaked between those threads.) "
62+
"Please either upgrade to Django channels 4.0+, use Django's async features "
63+
"available in Django 3.1+ instead of Django channels, or upgrade to Python 3.9+.",
64+
stacklevel=2,
65+
)
66+
isolation_scope = sentry_sdk.get_isolation_scope()
67+
current_scope = sentry_sdk.get_current_scope()
68+
7769
else:
78-
isolation_scope = None
79-
current_scope = None
70+
isolation_scope = sentry_sdk.get_isolation_scope().fork()
71+
current_scope = sentry_sdk.get_current_scope().fork()
8072

8173
# Patching instance methods in `start()` creates a reference cycle if
8274
# done in a naive way. See
@@ -114,9 +106,6 @@ def _run_old_run_func():
114106
with sentry_sdk.use_isolation_scope(isolation_scope_to_use):
115107
with sentry_sdk.use_scope(current_scope_to_use):
116108
return _run_old_run_func()
117-
else:
118-
with sentry_sdk.isolation_scope():
119-
return _run_old_run_func()
120109

121110
return run # type: ignore
122111

tests/integrations/threading/test_threading.py

Lines changed: 35 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ def crash():
3636
assert not events
3737

3838

39-
@pytest.mark.parametrize("propagate_scope", (True, False))
40-
def test_propagates_scope(sentry_init, capture_events, propagate_scope):
39+
def test_propagates_scope(sentry_init, capture_events):
4140
sentry_init(
4241
default_integrations=False,
43-
integrations=[ThreadingIntegration(propagate_scope=propagate_scope)],
42+
integrations=[ThreadingIntegration()],
4443
)
4544
events = capture_events()
4645

@@ -66,17 +65,13 @@ def stage2():
6665
assert exception["mechanism"]["type"] == "threading"
6766
assert not exception["mechanism"]["handled"]
6867

69-
if propagate_scope:
70-
assert event["tags"]["stage1"] == "true"
71-
else:
72-
assert "stage1" not in event.get("tags", {})
68+
assert event["tags"]["stage1"] == "true"
7369

7470

75-
@pytest.mark.parametrize("propagate_scope", (True, False))
76-
def test_propagates_threadpool_scope(sentry_init, capture_events, propagate_scope):
71+
def test_propagates_threadpool_scope(sentry_init, capture_events):
7772
sentry_init(
7873
traces_sample_rate=1.0,
79-
integrations=[ThreadingIntegration(propagate_scope=propagate_scope)],
74+
integrations=[ThreadingIntegration()],
8075
)
8176
events = capture_events()
8277

@@ -92,16 +87,12 @@ def double(number):
9287

9388
sentry_sdk.flush()
9489

95-
if propagate_scope:
96-
assert len(events) == 1
97-
(event,) = events
98-
assert event["spans"][0]["trace_id"] == event["spans"][1]["trace_id"]
99-
assert event["spans"][1]["trace_id"] == event["spans"][2]["trace_id"]
100-
assert event["spans"][2]["trace_id"] == event["spans"][3]["trace_id"]
101-
assert event["spans"][3]["trace_id"] == event["spans"][0]["trace_id"]
102-
else:
103-
(event,) = events
104-
assert len(event["spans"]) == 0
90+
assert len(events) == 1
91+
(event,) = events
92+
assert event["spans"][0]["trace_id"] == event["spans"][1]["trace_id"]
93+
assert event["spans"][1]["trace_id"] == event["spans"][2]["trace_id"]
94+
assert event["spans"][2]["trace_id"] == event["spans"][3]["trace_id"]
95+
assert event["spans"][3]["trace_id"] == event["spans"][0]["trace_id"]
10596

10697

10798
def test_circular_references(sentry_init, request):
@@ -174,27 +165,19 @@ def target():
174165
assert t.run.__qualname__ == original_run.__qualname__
175166

176167

177-
@pytest.mark.parametrize(
178-
"propagate_scope",
179-
(True, False),
180-
ids=["propagate_scope=True", "propagate_scope=False"],
181-
)
182-
def test_scope_data_not_leaked_in_threads(sentry_init, propagate_scope):
168+
def test_scope_data_not_leaked_in_threads(sentry_init):
183169
sentry_init(
184-
integrations=[ThreadingIntegration(propagate_scope=propagate_scope)],
170+
integrations=[ThreadingIntegration()],
185171
)
186172

187173
sentry_sdk.set_tag("initial_tag", "initial_value")
188174
initial_iso_scope = sentry_sdk.get_isolation_scope()
189175

190176
def do_some_work():
191177
# check if we have the initial scope data propagated into the thread
192-
if propagate_scope:
193-
assert sentry_sdk.get_isolation_scope()._tags == {
194-
"initial_tag": "initial_value"
195-
}
196-
else:
197-
assert sentry_sdk.get_isolation_scope()._tags == {}
178+
assert sentry_sdk.get_isolation_scope()._tags == {
179+
"initial_tag": "initial_value"
180+
}
198181

199182
# change data in isolation scope in thread
200183
sentry_sdk.set_tag("thread_tag", "thread_value")
@@ -209,17 +192,14 @@ def do_some_work():
209192
}, "The isolation scope in the main thread should not be modified by the started thread."
210193

211194

212-
@pytest.mark.parametrize(
213-
"propagate_scope",
214-
(True, False),
215-
ids=["propagate_scope=True", "propagate_scope=False"],
216-
)
217195
def test_spans_from_multiple_threads(
218-
sentry_init, capture_events, render_span_tree, propagate_scope
196+
sentry_init,
197+
capture_events,
198+
render_span_tree,
219199
):
220200
sentry_init(
221201
traces_sample_rate=1.0,
222-
integrations=[ThreadingIntegration(propagate_scope=propagate_scope)],
202+
integrations=[ThreadingIntegration()],
223203
)
224204
events = capture_events()
225205

@@ -244,31 +224,19 @@ def do_some_work(number):
244224
t.join()
245225

246226
(event,) = events
247-
if propagate_scope:
248-
assert render_span_tree(event) == dedent(
249-
"""\
250-
- op="outer-trx": description=null
251-
- op="outer-submit-0": description="Thread: main"
252-
- op="inner-run-0": description="Thread: child-0"
253-
- op="outer-submit-1": description="Thread: main"
254-
- op="inner-run-1": description="Thread: child-1"
255-
- op="outer-submit-2": description="Thread: main"
256-
- op="inner-run-2": description="Thread: child-2"
257-
- op="outer-submit-3": description="Thread: main"
258-
- op="inner-run-3": description="Thread: child-3"
259-
- op="outer-submit-4": description="Thread: main"
260-
- op="inner-run-4": description="Thread: child-4"\
261-
"""
262-
)
263-
264-
elif not propagate_scope:
265-
assert render_span_tree(event) == dedent(
266-
"""\
267-
- op="outer-trx": description=null
268-
- op="outer-submit-0": description="Thread: main"
269-
- op="outer-submit-1": description="Thread: main"
270-
- op="outer-submit-2": description="Thread: main"
271-
- op="outer-submit-3": description="Thread: main"
272-
- op="outer-submit-4": description="Thread: main"\
227+
228+
assert render_span_tree(event) == dedent(
229+
"""\
230+
- op="outer-trx": description=null
231+
- op="outer-submit-0": description="Thread: main"
232+
- op="inner-run-0": description="Thread: child-0"
233+
- op="outer-submit-1": description="Thread: main"
234+
- op="inner-run-1": description="Thread: child-1"
235+
- op="outer-submit-2": description="Thread: main"
236+
- op="inner-run-2": description="Thread: child-2"
237+
- op="outer-submit-3": description="Thread: main"
238+
- op="inner-run-3": description="Thread: child-3"
239+
- op="outer-submit-4": description="Thread: main"
240+
- op="inner-run-4": description="Thread: child-4"\
273241
"""
274-
)
242+
)

0 commit comments

Comments
 (0)