Skip to content

Commit d09babd

Browse files
committed
Removed timer code, need to think about something better
1 parent 65b1791 commit d09babd

File tree

3 files changed

+6
-73
lines changed

3 files changed

+6
-73
lines changed

sentry_sdk/integrations/wsgi.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22
from functools import partial
3-
from threading import Timer
43

54
import sentry_sdk
65
from sentry_sdk._werkzeug import get_host, _get_headers
@@ -124,18 +123,11 @@ def __call__(self, environ, start_response):
124123
origin=self.span_origin,
125124
)
126125

127-
timer = None
128126
if transaction is not None:
129127
sentry_sdk.start_transaction(
130128
transaction,
131129
custom_sampling_context={"wsgi_environ": environ},
132130
).__enter__()
133-
timer = Timer(
134-
MAX_TRANSACTION_DURATION_SECONDS,
135-
_finish_long_running_transaction,
136-
args=(current_scope, scope),
137-
)
138-
timer.start()
139131

140132
try:
141133
response = self.app(
@@ -149,7 +141,7 @@ def __call__(self, environ, start_response):
149141
except BaseException:
150142
exc_info = sys.exc_info()
151143
_capture_exception(exc_info)
152-
finish_running_transaction(current_scope, exc_info, timer)
144+
finish_running_transaction(current_scope, exc_info)
153145
reraise(*exc_info)
154146

155147
finally:
@@ -159,7 +151,6 @@ def __call__(self, environ, start_response):
159151
response=response,
160152
current_scope=current_scope,
161153
isolation_scope=scope,
162-
timer=timer,
163154
)
164155

165156

@@ -255,20 +246,18 @@ class _ScopedResponse:
255246
- WSGI servers streaming responses interleaved from the same thread
256247
"""
257248

258-
__slots__ = ("_response", "_current_scope", "_isolation_scope", "_timer")
249+
__slots__ = ("_response", "_current_scope", "_isolation_scope")
259250

260251
def __init__(
261252
self,
262253
response, # type: Iterator[bytes]
263254
current_scope, # type: sentry_sdk.scope.Scope
264255
isolation_scope, # type: sentry_sdk.scope.Scope
265-
timer=None, # type: Optional[Timer]
266256
):
267257
# type: (...) -> None
268258
self._response = response
269259
self._current_scope = current_scope
270260
self._isolation_scope = isolation_scope
271-
self._timer = timer
272261

273262
def __iter__(self):
274263
# type: () -> Iterator[bytes]
@@ -290,14 +279,14 @@ def __iter__(self):
290279
finally:
291280
with use_isolation_scope(self._isolation_scope):
292281
with use_scope(self._current_scope):
293-
finish_running_transaction(timer=self._timer)
282+
finish_running_transaction()
294283

295284
def close(self):
296285
# type: () -> None
297286
with use_isolation_scope(self._isolation_scope):
298287
with use_scope(self._current_scope):
299288
try:
300-
finish_running_transaction(timer=self._timer)
289+
finish_running_transaction()
301290
self._response.close() # type: ignore
302291
except AttributeError:
303292
pass
@@ -346,18 +335,3 @@ def event_processor(event, hint):
346335
return event
347336

348337
return event_processor
349-
350-
351-
def _finish_long_running_transaction(current_scope, isolation_scope):
352-
# type: (sentry_sdk.scope.Scope, sentry_sdk.scope.Scope) -> None
353-
"""
354-
Make sure we don't keep transactions open for too long.
355-
Triggered after MAX_TRANSACTION_DURATION_SECONDS have passed.
356-
"""
357-
try:
358-
with use_isolation_scope(isolation_scope):
359-
with use_scope(current_scope):
360-
finish_running_transaction()
361-
except AttributeError:
362-
# transaction is not there anymore
363-
pass

sentry_sdk/tracing_utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
from types import FrameType
3838

3939
from sentry_sdk._types import ExcInfo
40-
from threading import Timer
4140

4241

4342
SENTRY_TRACE_REGEX = re.compile(
@@ -744,11 +743,8 @@ def get_current_span(scope=None):
744743
from sentry_sdk.tracing import Span
745744

746745

747-
def finish_running_transaction(scope=None, exc_info=None, timer=None):
748-
# type: (Optional[sentry_sdk.Scope], Optional[ExcInfo], Optional[Timer]) -> None
749-
if timer is not None:
750-
timer.cancel()
751-
746+
def finish_running_transaction(scope=None, exc_info=None):
747+
# type: (Optional[sentry_sdk.Scope], Optional[ExcInfo]) -> None
752748
current_scope = scope or sentry_sdk.get_current_scope()
753749
if current_scope.transaction is not None and hasattr(
754750
current_scope.transaction, "_context_manager_state"

tests/integrations/wsgi/test_wsgi.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -537,40 +537,3 @@ def long_running_app(environ, start_response):
537537
assert (
538538
transaction_duration <= new_max_duration * 1.02
539539
) # we allow 2% margin for processing the request
540-
541-
542-
def test_long_running_transaction_timer_canceled(sentry_init, capture_events):
543-
# we allow transactions to be 0.5 seconds as a maximum
544-
new_max_duration = 0.5
545-
546-
with mock.patch.object(
547-
sentry_sdk.integrations.wsgi,
548-
"MAX_TRANSACTION_DURATION_SECONDS",
549-
new_max_duration,
550-
):
551-
with mock.patch(
552-
"sentry_sdk.integrations.wsgi._finish_long_running_transaction"
553-
) as mock_finish:
554-
555-
def generate_content():
556-
# This response will take 0.3 seconds to generate
557-
for _ in range(3):
558-
time.sleep(0.1)
559-
yield "ok"
560-
561-
def long_running_app(environ, start_response):
562-
start_response("200 OK", [])
563-
return generate_content()
564-
565-
sentry_init(send_default_pii=True, traces_sample_rate=1.0)
566-
app = SentryWsgiMiddleware(long_running_app)
567-
568-
events = capture_events()
569-
570-
client = Client(app)
571-
response = client.get("/")
572-
_ = response.get_data()
573-
574-
(transaction,) = events
575-
576-
mock_finish.assert_not_called()

0 commit comments

Comments
 (0)