_metrics_batcher.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from typing import TYPE_CHECKING
  2. from sentry_sdk._batcher import Batcher
  3. from sentry_sdk.utils import serialize_attribute
  4. if TYPE_CHECKING:
  5. from typing import Any
  6. from sentry_sdk._types import Metric
  7. class MetricsBatcher(Batcher["Metric"]):
  8. MAX_BEFORE_FLUSH = 1000
  9. MAX_BEFORE_DROP = 10_000
  10. FLUSH_WAIT_TIME = 5.0
  11. TYPE = "trace_metric"
  12. CONTENT_TYPE = "application/vnd.sentry.items.trace-metric+json"
  13. @staticmethod
  14. def _to_transport_format(item: "Metric") -> "Any":
  15. res = {
  16. "timestamp": item["timestamp"],
  17. "trace_id": item["trace_id"],
  18. "name": item["name"],
  19. "type": item["type"],
  20. "value": item["value"],
  21. "attributes": {
  22. k: serialize_attribute(v) for (k, v) in item["attributes"].items()
  23. },
  24. }
  25. if item.get("span_id") is not None:
  26. res["span_id"] = item["span_id"]
  27. if item.get("unit") is not None:
  28. res["unit"] = item["unit"]
  29. return res
  30. def _record_lost(self, item: "Metric") -> None:
  31. self._record_lost_func(
  32. reason="queue_overflow",
  33. data_category="trace_metric",
  34. quantity=1,
  35. )