Repository navigation
feat(metrics): Add experimental window sampling for trace metrics #8320
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: master
Are you sure you want to change the base?
Changes from all commits
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,12 +1,20 @@ | ||
| import math | ||
| import random | ||
| import time | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from sentry_sdk._batcher import Batcher | ||
| from sentry_sdk.utils import serialize_attribute | ||
| from sentry_sdk.utils import capture_internal_exceptions, logger, serialize_attribute | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any | ||
| from typing import Any, Callable, Dict, List, Optional, Tuple, Union | ||
|
|
||
| from sentry_sdk._types import Metric | ||
| from sentry_sdk._types import Metric, MetricWindowSamplingOptions | ||
| from sentry_sdk.envelope import Envelope | ||
|
|
||
| DEFAULT_SAMPLE_PER_SECOND = 2 | ||
| # Matches FLUSH_WAIT_TIME, so each window goes out in one flush | ||
| WINDOW_SECONDS = 5.0 | ||
|
|
||
|
|
||
| class MetricsBatcher(Batcher["Metric"]): | ||
|
|
@@ -18,6 +26,151 @@ class MetricsBatcher(Batcher["Metric"]): | |
| CLIENT_REPORT_TYPE = "trace_metric" | ||
| CONTENT_TYPE = "application/vnd.sentry.items.trace-metric+json" | ||
|
|
||
| def __init__( | ||
| self, | ||
| capture_func: "Callable[[Envelope], None]", | ||
| record_lost_func: "Callable[..., None]", | ||
| window_sampling: "Union[bool, MetricWindowSamplingOptions, None]" = None, | ||
| ) -> None: | ||
| self._default_limit: "Optional[int]" = None | ||
| self._name_limits: "Dict[str, int]" = {} | ||
| if window_sampling: | ||
| options = window_sampling if isinstance(window_sampling, dict) else {} | ||
| per_name = options.get("per_name") | ||
| if isinstance(per_name, dict): | ||
| self._name_limits = { | ||
| name: self._parse_limit(rate) for name, rate in per_name.items() | ||
| } | ||
| if "sample_per_second" in options or not self._name_limits: | ||
| self._default_limit = self._parse_limit( | ||
| options.get("sample_per_second", DEFAULT_SAMPLE_PER_SECOND) | ||
| ) | ||
| self._sampling = bool(self._name_limits) or self._default_limit is not None | ||
|
|
||
| self._reset_sampling_state() | ||
| super().__init__(capture_func, record_lost_func) | ||
|
|
||
| def _reset_thread_state(self) -> None: | ||
| super()._reset_thread_state() | ||
| self._reset_sampling_state() | ||
|
|
||
| def _reset_sampling_state(self) -> None: | ||
| self._window_start = time.monotonic() | ||
| self._reservoirs: "Dict[str, List[Metric]]" = {} | ||
| self._seen: "Dict[str, int]" = {} | ||
| # Reservoirs count against MAX_BEFORE_DROP together with the buffer, so | ||
| # memory stays bounded no matter how many names there are | ||
| self._held = 0 | ||
| self._sampled_out: "List[Tuple[Metric, int]]" = [] | ||
|
|
||
| def _parse_limit(self, value: float) -> int: | ||
| try: | ||
| rate = float(value) | ||
| except (TypeError, ValueError): | ||
| rate = math.nan | ||
|
|
||
| if not (math.isfinite(rate) and rate > 0): | ||
| logger.warning( | ||
| "[Metrics] Given window sampling rate is invalid. It must be a positive number. Got %r, using %s instead.", | ||
| value, | ||
| DEFAULT_SAMPLE_PER_SECOND, | ||
| ) | ||
| rate = DEFAULT_SAMPLE_PER_SECOND | ||
|
|
||
| # Never go below 1, so we always keep at least one metric per name to | ||
| # extrapolate from | ||
| return min(max(1, int(rate * WINDOW_SECONDS)), self.MAX_BEFORE_DROP) | ||
|
|
||
| def _add_locked(self, item: "Metric") -> bool: | ||
| if not self._sampling: | ||
| return super()._add_locked(item) | ||
|
|
||
| name = item["name"] | ||
| limit = self._name_limits.get(name, self._default_limit) | ||
| if limit is None: | ||
| if len(self._buffer) + self._held >= self.MAX_BEFORE_DROP: | ||
| return False | ||
| return super()._add_locked(item) | ||
|
|
||
| if time.monotonic() - self._window_start >= WINDOW_SECONDS: | ||
| self._close_window() | ||
|
|
||
| reservoir = self._reservoirs.setdefault(name, []) | ||
| seen = self._seen.get(name, 0) + 1 | ||
| if len(reservoir) < limit: | ||
| # Replacing a slot in a full reservoir doesn't need more memory, only | ||
| # a new slot does | ||
| if len(self._buffer) + self._held >= self.MAX_BEFORE_DROP: | ||
| return False | ||
|
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. Empty reservoirs bypass memory boundMedium Severity A new metric name is inserted into Reviewed by Cursor Bugbot for commit c27cfa5. Configure here. |
||
| reservoir.append(item) | ||
| self._held += 1 | ||
| else: | ||
| index = random.randrange(seen) | ||
| if index < limit: | ||
| reservoir[index] = item | ||
| self._seen[name] = seen | ||
| return True | ||
|
|
||
| def _close_window(self) -> None: | ||
| for name, kept in self._reservoirs.items(): | ||
| seen = self._seen.get(name, 0) | ||
| if kept and seen > len(kept): | ||
| rate = len(kept) / seen | ||
| for item in kept: | ||
| attributes = item["attributes"] | ||
| client_rate = attributes.get("sentry.client_sample_rate") | ||
| if isinstance(client_rate, (int, float)) and 0 < client_rate <= 1: | ||
| attributes["sentry.client_sample_rate"] = rate * client_rate | ||
|
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. Needs a conventions update, |
||
| else: | ||
| attributes["sentry.client_sample_rate"] = rate | ||
| self._sampled_out.append((kept[0], seen - len(kept))) | ||
| self._buffer.extend(kept) | ||
|
|
||
| self._reservoirs = {} | ||
| self._seen = {} | ||
| self._held = 0 | ||
| self._window_start = time.monotonic() | ||
| if len(self._buffer) >= self.MAX_BEFORE_FLUSH: | ||
| self._flush_event.set() | ||
|
|
||
| def flush(self) -> None: | ||
| if self._sampling: | ||
| with self._lock: | ||
| self._close_window() | ||
| super().flush() | ||
|
|
||
| def _flush(self) -> "Optional[Envelope]": | ||
| if not self._sampling: | ||
| return super()._flush() | ||
|
|
||
| with self._lock: | ||
| # Early flushes leave the window open, so a name can't go over its | ||
| # rate | ||
| if time.monotonic() - self._window_start >= WINDOW_SECONDS: | ||
| self._close_window() | ||
| sampled_out, self._sampled_out = self._sampled_out, [] | ||
|
|
||
| envelope = super()._flush() | ||
|
|
||
| # Record outside of the lock, since this serializes a metric and every | ||
| # add() would wait on it | ||
| for item, count in sampled_out: | ||
| self._record_lost_func( | ||
| reason="sample_rate", | ||
| data_category=self.CLIENT_REPORT_TYPE, | ||
| quantity=count, | ||
| ) | ||
| # Serializing every sampled out metric would cost as much as we saved | ||
| # by sampling, so we estimate from one | ||
| with capture_internal_exceptions(): | ||
| self._record_lost_func( | ||
| reason="sample_rate", | ||
| data_category="trace_metric_byte", | ||
| quantity=count * len(self._to_envelope_item([item]).get_bytes()), | ||
| ) | ||
|
|
||
| return envelope | ||
|
|
||
| @staticmethod | ||
| def _to_transport_format(item: "Metric") -> "Any": | ||
| res = { | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Window close drops incoming metrics
Medium Severity
Closing a full window copies every reserved metric into
_bufferup toMAX_BEFORE_DROP, then the sameaddstill capacity-checks that buffer. The metric that closed the window, and any that arrive before the flusher drains, are discarded asqueue_overfloweven though that capacity is about to be freed.MAX_BEFORE_FLUSHis meant to leave headroom for this gap, but a window close can fill the buffer to the hard cap in one shot and wipe that headroom out.Additional Locations (1)
sentry_sdk/_metrics_batcher.py#L113-L134Reviewed by Cursor Bugbot for commit c27cfa5. Configure here.