Repository navigation
feat: Add per-metric windows to lock free metrics batcher #8327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: webb/sliding-window-metrics-batcher
Are you sure you want to change the base?
Changes from all commits
0cb3e73
c800bc7
84db651
21fd75e
343bef6
ff10f51
7e49836
14893a6
183d1a1
5181903
c626961
33aa0d7
4afc3ba
3f07315
4fdf682
7ed73d5
c7cc2dd
3119a60
44d2d61
eb22208
eaf3ab6
3f45ee5
cb36693
4ce601c
ffc6b96
c89e37d
7967d7f
f38f3a9
5e08286
525cb85
e5ae8c9
008f662
cbcd999
7442556
b617d12
c304ece
2caf187
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| import bisect | ||
| import itertools | ||
| import os | ||
| import random | ||
| import threading | ||
| from collections import deque | ||
| import time | ||
| from collections import defaultdict, deque | ||
| from datetime import datetime, timezone | ||
| from typing import TYPE_CHECKING | ||
|
|
||
|
|
@@ -17,9 +21,42 @@ | |
| from sentry_sdk._types import Metric | ||
|
|
||
|
|
||
| class _Reservoir: | ||
| __slots__ = ("kept", "seen") | ||
|
|
||
| def __init__(self) -> None: | ||
| self.kept: "list[Metric]" = [] | ||
| self.seen = 0 | ||
|
|
||
|
|
||
| class _ThreadState: | ||
| """The metrics collected by one thread in a given window.""" | ||
|
|
||
| __slots__ = ("reservoirs",) | ||
|
|
||
| def __init__(self) -> None: | ||
| self.reservoirs: "defaultdict[str, _Reservoir]" = defaultdict(_Reservoir) | ||
|
|
||
|
|
||
| class _Window: | ||
| """ | ||
| One time window in which multiple threads concurrently capture metrics. | ||
| Only the thread with the given identifier should change its local state. | ||
| """ | ||
|
|
||
| __slots__ = ("start", "threads", "retire_claims", "number_kept") | ||
|
|
||
| def __init__(self) -> None: | ||
| self.start = time.monotonic() | ||
| self.threads: "defaultdict[int, _ThreadState]" = defaultdict(_ThreadState) | ||
| self.retire_claims: "list[object]" = [] | ||
| self.number_kept = 0 | ||
|
|
||
|
|
||
| class LockFreeMetricsBatcher(Batcher["Metric"]): | ||
| MAX_BEFORE_FLUSH = 1000 | ||
| MAX_BEFORE_DROP = 10_000 | ||
| MAX_UNSAMPLED_BEFORE_FLUSH = 1000 | ||
| MAX_UNSAMPLED_BEFORE_DROP = 10_000 | ||
| MAX_KEPT_PER_WINDOW = 10_000 | ||
| FLUSH_WAIT_TIME = 5.0 | ||
|
|
||
| TYPE = "trace_metric" | ||
|
|
@@ -30,15 +67,26 @@ def __init__( | |
| self, | ||
| capture_func: "Callable[[Envelope], None]", | ||
| record_lost_func: "Callable[..., None]", | ||
| window_sampling: "dict[str, float]", | ||
| ) -> None: | ||
| super().__init__(capture_func, record_lost_func) | ||
|
|
||
| self._limits = { | ||
| name: int(rate * self.FLUSH_WAIT_TIME) | ||
| for name, rate in window_sampling.items() | ||
| } | ||
|
|
||
| self._queue: "deque[Metric]" = deque() | ||
| self._window = _Window() | ||
| self._retired: "deque[_Window]" = deque() | ||
| # Holds the claim of the one thread that gets to start the flusher. | ||
| self._flusher_claim: "dict[str, object]" = {} | ||
|
|
||
| def _reset_thread_state(self) -> None: | ||
| super()._reset_thread_state() | ||
| self._queue = deque() | ||
| self._window = _Window() | ||
| self._retired = deque() | ||
| self._flusher_claim = {} | ||
|
|
||
| def _ensure_thread(self) -> bool: | ||
|
|
@@ -75,16 +123,59 @@ def add(self, item: "Metric") -> None: | |
| if not self._ensure_thread(): | ||
| return None | ||
|
|
||
| queue = self._queue | ||
| if len(queue) >= self.MAX_BEFORE_DROP: | ||
| self._record_lost(item) | ||
| return None | ||
| window = self._window | ||
| if time.monotonic() - window.start >= self.FLUSH_WAIT_TIME: | ||
| self._retire_window(window) | ||
| window = self._window | ||
|
|
||
| queue.append(item) | ||
| if len(queue) < self.MAX_BEFORE_FLUSH or self._flush_event.is_set(): | ||
| limit = self._limits.get(item["name"]) | ||
| if limit is None: | ||
| if len(self._queue) >= self.MAX_UNSAMPLED_BEFORE_DROP: | ||
| self._record_lost(item) | ||
| return None | ||
|
|
||
| self._queue.append(item) | ||
| if ( | ||
| len(self._queue) < self.MAX_UNSAMPLED_BEFORE_FLUSH | ||
| or self._flush_event.is_set() | ||
| ): | ||
| return | ||
|
|
||
| self._flush_event.set() | ||
| return | ||
|
|
||
| self._flush_event.set() | ||
| self._add_to_window(item, window, limit) | ||
|
|
||
| def _add_to_window(self, item: "Metric", window: "_Window", limit: int) -> None: | ||
| state = window.threads[threading.get_ident()] | ||
| reservoir = state.reservoirs[item["name"]] | ||
| reservoir.seen += 1 | ||
| if ( | ||
| len(reservoir.kept) < limit | ||
| and window.number_kept < self.MAX_KEPT_PER_WINDOW | ||
| ): | ||
| window.number_kept += 1 | ||
|
alexander-alderman-webb marked this conversation as resolved.
Resolved
|
||
| reservoir.kept.append(item) | ||
| return | ||
|
|
||
| index = random.randrange(reservoir.seen) | ||
| if index >= len(reservoir.kept): | ||
| return | ||
|
|
||
| reservoir.kept[index] = item | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| def _retire_window(self, window: "_Window") -> None: | ||
| claim = object() | ||
| window.retire_claims.append(claim) | ||
| if window.retire_claims[0] is not claim: | ||
| return | ||
|
|
||
| self._window = _Window() | ||
| self._retired.append(window) | ||
|
|
||
| def flush(self) -> None: | ||
| self._retire_window(self._window) | ||
| super().flush() | ||
|
|
||
| def _flush(self) -> "Optional[Envelope]": | ||
| queue = self._queue | ||
|
|
@@ -96,6 +187,32 @@ def _flush(self) -> "Optional[Envelope]": | |
| except IndexError: | ||
| break | ||
|
|
||
| sampled_out = 0 | ||
|
|
||
| now = time.monotonic() | ||
| window = self._window | ||
| if now - window.start >= self.FLUSH_WAIT_TIME: | ||
| self._retire_window(window) | ||
|
|
||
| for window in self._retired.copy(): | ||
| if ( | ||
| threading.current_thread() is self._flusher | ||
| and now - window.start < self.FLUSH_WAIT_TIME + 0.05 | ||
| ): | ||
| break | ||
| try: | ||
| window = self._retired.popleft() | ||
| except IndexError: | ||
| break | ||
| sampled_out += self._harvest(window, items) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if we should decouple the harvesting from the flush somehow, or at least introduce some small delay in between. A recently retired window might probably still have a thread writing to it, if the write was initiated before it was retired, so we might be losing some metrics there.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added another 0.05 seconds delay so there's at least a guaranteed buffer since I couldn't come up with a good synchronization mechanism to strictly not lose any metrics. |
||
|
|
||
| if sampled_out: | ||
| self._record_lost_func( | ||
| reason="sample_rate", | ||
| data_category=self.CLIENT_REPORT_TYPE, | ||
| quantity=sampled_out, | ||
| ) | ||
|
|
||
| if not items: | ||
| return None | ||
|
|
||
|
|
@@ -106,6 +223,38 @@ def _flush(self) -> "Optional[Envelope]": | |
| self._capture_func(envelope) | ||
| return envelope | ||
|
|
||
| def _harvest(self, window: "_Window", items: "list[Metric]") -> int: | ||
| by_name: "dict[str, list[_Reservoir]]" = {} | ||
| for state in window.threads.copy().values(): | ||
| for name, reservoir in state.reservoirs.copy().items(): | ||
| by_name.setdefault(name, []).append(reservoir) | ||
|
|
||
| sampled_out = 0 | ||
| for name, reservoirs in by_name.items(): | ||
| limit = self._limits[name] | ||
| bounds = list( | ||
| itertools.accumulate(reservoir.seen for reservoir in reservoirs) | ||
| ) | ||
| slots = [0] * len(reservoirs) | ||
| for position in random.sample(range(bounds[-1]), min(limit, bounds[-1])): | ||
| slots[bisect.bisect_right(bounds, position)] += 1 | ||
|
|
||
| kept: "list[Metric]" = [] | ||
| for reservoir, count in zip(reservoirs, slots): | ||
| thread_kept = reservoir.kept.copy() | ||
| kept.extend(random.sample(thread_kept, min(count, len(thread_kept)))) | ||
| if not kept: | ||
| sampled_out += bounds[-1] | ||
| continue | ||
|
|
||
| items.extend(kept) | ||
| rate = len(kept) / bounds[-1] | ||
| for item in kept: | ||
| item["attributes"]["sentry.client_sample_rate"] = rate | ||
| sampled_out += bounds[-1] - len(kept) | ||
|
|
||
| return sampled_out | ||
|
|
||
| @staticmethod | ||
| def _to_transport_format(item: "Metric") -> "Any": | ||
| res = { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.