utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import time
  2. from typing import TYPE_CHECKING, cast
  3. if TYPE_CHECKING:
  4. from typing import Any, Tuple
  5. from sentry_sdk._types import MonitorConfigScheduleUnit
  6. def _now_seconds_since_epoch() -> float:
  7. # We cannot use `time.perf_counter()` when dealing with the duration
  8. # of a Celery task, because the start of a Celery task and
  9. # the end are recorded in different processes.
  10. # Start happens in the Celery Beat process,
  11. # the end in a Celery Worker process.
  12. return time.time()
  13. def _get_humanized_interval(seconds: float) -> "Tuple[int, MonitorConfigScheduleUnit]":
  14. TIME_UNITS = ( # noqa: N806
  15. ("day", 60 * 60 * 24.0),
  16. ("hour", 60 * 60.0),
  17. ("minute", 60.0),
  18. )
  19. seconds = float(seconds)
  20. for unit, divider in TIME_UNITS:
  21. if seconds >= divider:
  22. interval = int(seconds / divider)
  23. return (interval, cast("MonitorConfigScheduleUnit", unit))
  24. return (int(seconds), "second")
  25. class NoOpMgr:
  26. def __enter__(self) -> None:
  27. return None
  28. def __exit__(self, exc_type: "Any", exc_value: "Any", traceback: "Any") -> None:
  29. return None