Skip to content

Commit

Permalink
fix(metrics): Only start thread on demand (#2727)
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrivana authored Feb 13, 2024
1 parent 2772dde commit 26b6853
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 6 additions & 2 deletions sentry_sdk/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ def __init__(

self._flusher = None # type: Optional[Union[threading.Thread, ThreadPool]]
self._flusher_pid = None # type: Optional[int]
self._ensure_thread()

def _ensure_thread(self):
# type: (...) -> bool
Expand All @@ -460,6 +459,11 @@ def _ensure_thread(self):
return True

with self._lock:
# Recheck to make sure another thread didn't get here and start the
# the flusher in the meantime
if self._flusher_pid == pid:
return True

self._flusher_pid = pid

if not is_gevent():
Expand All @@ -484,9 +488,9 @@ def _flush_loop(self):
# type: (...) -> None
_in_metrics.set(True)
while self._running or self._force_flush:
self._flush()
if self._running:
self._flush_event.wait(self.FLUSHER_SLEEP_TIME)
self._flush()

def _flush(self):
# type: (...) -> None
Expand Down
1 change: 0 additions & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ def test_timing_basic(sentry_init, capture_envelopes, maybe_monkeypatched_thread
metrics.timing("timing", 3.0, tags={"a": "b"}, timestamp=ts)
Hub.current.flush()

(envelope,) = envelopes
(envelope,) = envelopes
statsd_item, meta_item = envelope.items

Expand Down

0 comments on commit 26b6853

Please sign in to comment.