Collector for Standard Exports such as cpu and memory.
| 14 | |
| 15 | |
| 16 | class ProcessCollector(Collector): |
| 17 | """Collector for Standard Exports such as cpu and memory.""" |
| 18 | |
| 19 | def __init__(self, |
| 20 | namespace: str = '', |
| 21 | pid: Callable[[], Union[int, str]] = lambda: 'self', |
| 22 | proc: str = '/proc', |
| 23 | registry: Optional[CollectorRegistry] = REGISTRY): |
| 24 | self._namespace = namespace |
| 25 | self._pid = pid |
| 26 | self._proc = proc |
| 27 | if namespace: |
| 28 | self._prefix = namespace + '_process_' |
| 29 | else: |
| 30 | self._prefix = 'process_' |
| 31 | self._ticks = 100.0 |
| 32 | try: |
| 33 | self._ticks = os.sysconf('SC_CLK_TCK') |
| 34 | except (ValueError, TypeError, AttributeError, OSError): |
| 35 | pass |
| 36 | |
| 37 | self._pagesize = _PAGESIZE |
| 38 | |
| 39 | # This is used to test if we can access /proc. |
| 40 | self._btime = 0 |
| 41 | try: |
| 42 | self._btime = self._boot_time() |
| 43 | except OSError: |
| 44 | pass |
| 45 | if registry: |
| 46 | registry.register(self) |
| 47 | |
| 48 | def _boot_time(self): |
| 49 | with open(os.path.join(self._proc, 'stat'), 'rb') as stat: |
| 50 | for line in stat: |
| 51 | if line.startswith(b'btime '): |
| 52 | return float(line.split()[1]) |
| 53 | |
| 54 | def collect(self) -> Iterable[Metric]: |
| 55 | if not self._btime: |
| 56 | return [] |
| 57 | |
| 58 | pid = os.path.join(self._proc, str(self._pid()).strip()) |
| 59 | |
| 60 | result = [] |
| 61 | try: |
| 62 | with open(os.path.join(pid, 'stat'), 'rb') as stat: |
| 63 | parts = (stat.read().split(b')')[-1].split()) |
| 64 | |
| 65 | vmem = GaugeMetricFamily(self._prefix + 'virtual_memory_bytes', |
| 66 | 'Virtual memory size in bytes.', value=float(parts[20])) |
| 67 | rss = GaugeMetricFamily(self._prefix + 'resident_memory_bytes', 'Resident memory size in bytes.', |
| 68 | value=float(parts[21]) * self._pagesize) |
| 69 | start_time_secs = float(parts[19]) / self._ticks |
| 70 | start_time = GaugeMetricFamily(self._prefix + 'start_time_seconds', |
| 71 | 'Start time of the process since unix epoch in seconds.', |
| 72 | value=start_time_secs + self._btime) |
| 73 | utime = float(parts[11]) / self._ticks |
no outgoing calls