process_collector.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import os
  2. from typing import Callable, Iterable, Optional, Union
  3. from .metrics_core import CounterMetricFamily, GaugeMetricFamily, Metric
  4. from .registry import Collector, CollectorRegistry, REGISTRY
  5. try:
  6. import resource
  7. _PAGESIZE = resource.getpagesize()
  8. except ImportError:
  9. # Not Unix
  10. _PAGESIZE = 4096
  11. class ProcessCollector(Collector):
  12. """Collector for Standard Exports such as cpu and memory."""
  13. def __init__(self,
  14. namespace: str = '',
  15. pid: Callable[[], Union[int, str]] = lambda: 'self',
  16. proc: str = '/proc',
  17. registry: Optional[CollectorRegistry] = REGISTRY):
  18. self._namespace = namespace
  19. self._pid = pid
  20. self._proc = proc
  21. if namespace:
  22. self._prefix = namespace + '_process_'
  23. else:
  24. self._prefix = 'process_'
  25. self._ticks = 100.0
  26. try:
  27. self._ticks = os.sysconf('SC_CLK_TCK')
  28. except (ValueError, TypeError, AttributeError, OSError):
  29. pass
  30. self._pagesize = _PAGESIZE
  31. # This is used to test if we can access /proc.
  32. self._btime = 0
  33. try:
  34. self._btime = self._boot_time()
  35. except OSError:
  36. pass
  37. if registry:
  38. registry.register(self)
  39. def _boot_time(self):
  40. with open(os.path.join(self._proc, 'stat'), 'rb') as stat:
  41. for line in stat:
  42. if line.startswith(b'btime '):
  43. return float(line.split()[1])
  44. def collect(self) -> Iterable[Metric]:
  45. if not self._btime:
  46. return []
  47. pid = os.path.join(self._proc, str(self._pid()).strip())
  48. result = []
  49. try:
  50. with open(os.path.join(pid, 'stat'), 'rb') as stat:
  51. parts = (stat.read().split(b')')[-1].split())
  52. vmem = GaugeMetricFamily(self._prefix + 'virtual_memory_bytes',
  53. 'Virtual memory size in bytes.', value=float(parts[20]))
  54. rss = GaugeMetricFamily(self._prefix + 'resident_memory_bytes', 'Resident memory size in bytes.',
  55. value=float(parts[21]) * self._pagesize)
  56. start_time_secs = float(parts[19]) / self._ticks
  57. start_time = GaugeMetricFamily(self._prefix + 'start_time_seconds',
  58. 'Start time of the process since unix epoch in seconds.',
  59. value=start_time_secs + self._btime)
  60. utime = float(parts[11]) / self._ticks
  61. stime = float(parts[12]) / self._ticks
  62. cpu = CounterMetricFamily(self._prefix + 'cpu_seconds_total',
  63. 'Total user and system CPU time spent in seconds.',
  64. value=utime + stime)
  65. result.extend([vmem, rss, start_time, cpu])
  66. except OSError:
  67. pass
  68. try:
  69. with open(os.path.join(pid, 'limits'), 'rb') as limits:
  70. for line in limits:
  71. if line.startswith(b'Max open file'):
  72. max_fds = GaugeMetricFamily(self._prefix + 'max_fds',
  73. 'Maximum number of open file descriptors.',
  74. value=float(line.split()[3]))
  75. break
  76. open_fds = GaugeMetricFamily(self._prefix + 'open_fds',
  77. 'Number of open file descriptors.',
  78. len(os.listdir(os.path.join(pid, 'fd'))))
  79. result.extend([open_fds, max_fds])
  80. except OSError:
  81. pass
  82. return result
  83. PROCESS_COLLECTOR = ProcessCollector()
  84. """Default ProcessCollector in default Registry REGISTRY."""