Skip to content

Commit f35adf3

Browse files
authored
feat(metrics): Shift flushing by up to a rollup window (#2396)
1 parent 6908aad commit f35adf3

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

sentry_sdk/metrics.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import io
33
import re
44
import threading
5+
import random
56
import time
67
import zlib
78
from functools import wraps, partial
@@ -303,6 +304,14 @@ def __init__(
303304
self._flush_event = Event()
304305
self._force_flush = False
305306

307+
# The aggregator shifts it's flushing by up to an entire rollup window to
308+
# avoid multiple clients trampling on end of a 10 second window as all the
309+
# buckets are anchored to multiples of ROLLUP seconds. We randomize this
310+
# number once per aggregator boot to achieve some level of offsetting
311+
# across a fleet of deployed SDKs. Relay itself will also apply independent
312+
# jittering.
313+
self._flush_shift = random.random() * self.ROLLUP_IN_SECONDS
314+
306315
self._flusher = None # type: Optional[Thread]
307316
self._flusher_pid = None # type: Optional[int]
308317
self._ensure_thread()
@@ -339,7 +348,7 @@ def _flushable_buckets(self):
339348
# type: (...) -> (Iterable[Tuple[int, Dict[BucketKey, Metric]]])
340349
with self._lock:
341350
force_flush = self._force_flush
342-
cutoff = time.time() - self.ROLLUP_IN_SECONDS
351+
cutoff = time.time() - self.ROLLUP_IN_SECONDS - self._flush_shift
343352
flushable_buckets = () # type: Iterable[Tuple[int, Dict[BucketKey, Metric]]]
344353
weight_to_remove = 0
345354

0 commit comments

Comments
 (0)