Skip to content

Commit

Permalink
feat(metrics): Shift flushing by up to a rollup window (#2396)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Sep 28, 2023
1 parent 6908aad commit f35adf3
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion sentry_sdk/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import io
import re
import threading
import random
import time
import zlib
from functools import wraps, partial
Expand Down Expand Up @@ -303,6 +304,14 @@ def __init__(
self._flush_event = Event()
self._force_flush = False

# The aggregator shifts it's flushing by up to an entire rollup window to
# avoid multiple clients trampling on end of a 10 second window as all the
# buckets are anchored to multiples of ROLLUP seconds. We randomize this
# number once per aggregator boot to achieve some level of offsetting
# across a fleet of deployed SDKs. Relay itself will also apply independent
# jittering.
self._flush_shift = random.random() * self.ROLLUP_IN_SECONDS

self._flusher = None # type: Optional[Thread]
self._flusher_pid = None # type: Optional[int]
self._ensure_thread()
Expand Down Expand Up @@ -339,7 +348,7 @@ def _flushable_buckets(self):
# type: (...) -> (Iterable[Tuple[int, Dict[BucketKey, Metric]]])
with self._lock:
force_flush = self._force_flush
cutoff = time.time() - self.ROLLUP_IN_SECONDS
cutoff = time.time() - self.ROLLUP_IN_SECONDS - self._flush_shift
flushable_buckets = () # type: Iterable[Tuple[int, Dict[BucketKey, Metric]]]
weight_to_remove = 0

Expand Down

0 comments on commit f35adf3

Please sign in to comment.