_reactor_metrics.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import select
  15. import time
  16. from typing import Any, Iterable, List, Tuple
  17. from prometheus_client import Histogram, Metric
  18. from prometheus_client.core import REGISTRY, GaugeMetricFamily
  19. from twisted.internet import reactor
  20. from synapse.metrics._types import Collector
  21. #
  22. # Twisted reactor metrics
  23. #
  24. tick_time = Histogram(
  25. "python_twisted_reactor_tick_time",
  26. "Tick time of the Twisted reactor (sec)",
  27. buckets=[0.001, 0.002, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2, 5],
  28. )
  29. class EpollWrapper:
  30. """a wrapper for an epoll object which records the time between polls"""
  31. def __init__(self, poller: "select.epoll"): # type: ignore[name-defined]
  32. self.last_polled = time.time()
  33. self._poller = poller
  34. def poll(self, *args, **kwargs) -> List[Tuple[int, int]]: # type: ignore[no-untyped-def]
  35. # record the time since poll() was last called. This gives a good proxy for
  36. # how long it takes to run everything in the reactor - ie, how long anything
  37. # waiting for the next tick will have to wait.
  38. tick_time.observe(time.time() - self.last_polled)
  39. ret = self._poller.poll(*args, **kwargs)
  40. self.last_polled = time.time()
  41. return ret
  42. def __getattr__(self, item: str) -> Any:
  43. return getattr(self._poller, item)
  44. class ReactorLastSeenMetric(Collector):
  45. def __init__(self, epoll_wrapper: EpollWrapper):
  46. self._epoll_wrapper = epoll_wrapper
  47. def collect(self) -> Iterable[Metric]:
  48. cm = GaugeMetricFamily(
  49. "python_twisted_reactor_last_seen",
  50. "Seconds since the Twisted reactor was last seen",
  51. )
  52. cm.add_metric([], time.time() - self._epoll_wrapper.last_polled)
  53. yield cm
  54. try:
  55. # if the reactor has a `_poller` attribute, which is an `epoll` object
  56. # (ie, it's an EPollReactor), we wrap the `epoll` with a thing that will
  57. # measure the time between ticks
  58. from select import epoll # type: ignore[attr-defined]
  59. poller = reactor._poller # type: ignore[attr-defined]
  60. except (AttributeError, ImportError):
  61. pass
  62. else:
  63. if isinstance(poller, epoll):
  64. poller = EpollWrapper(poller)
  65. reactor._poller = poller # type: ignore[attr-defined]
  66. REGISTRY.register(ReactorLastSeenMetric(poller))