Skip to content

MultiProcessCollector.collect() crashing on missing files #425

@xavfernandez

Description

@xavfernandez

Hello,

When you've used a lot of pids and have a lot of metrics files lying around, there is a high probability that mark_process_dead

def mark_process_dead(pid, path=None):
"""Do bookkeeping for when one process dies in a multi-process setup."""
if path is None:
path = os.environ.get('prometheus_multiproc_dir')
for f in glob.glob(os.path.join(path, 'gauge_livesum_{0}.db'.format(pid))):
os.remove(f)
for f in glob.glob(os.path.join(path, 'gauge_liveall_{0}.db'.format(pid))):
os.remove(f)

will be called between the glob.glob call and the self.merge one:
files = glob.glob(os.path.join(self._path, '*.db'))
return self.merge(files, accumulate=True)

leading to:

$ prometheus_multiproc_dir=/tmp/prometheus_metrics python
Python 3.7.3 (default, May  8 2019, 05:31:59)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from prometheus_client import multiprocess
>>> from prometheus_client import generate_latest, CollectorRegistry
>>> registry = CollectorRegistry()
>>> multiprocess.MultiProcessCollector(registry)
<prometheus_client.multiprocess.MultiProcessCollector object at 0x7ffb0b1efa58>
>>> data = generate_latest(registry)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/grocker/app.venv/lib/python3.7/site-packages/prometheus_client/exposition.py", line 89, in generate_latest
   for metric in registry.collect():
 File "/home/grocker/app.venv/lib/python3.7/site-packages/prometheus_client/registry.py", line 75, in collect
   for metric in collector.collect():
 File "/home/grocker/app.venv/lib/python3.7/site-packages/prometheus_client/multiprocess.py", line 30, in collect
   return self.merge(files, accumulate=True)
 File "/home/grocker/app.venv/lib/python3.7/site-packages/prometheus_client/multiprocess.py", line 43, in merge
   d = MmapedDict(f, read_mode=True)
 File "/home/grocker/app.venv/lib/python3.7/site-packages/prometheus_client/mmap_dict.py", line 39, in __init__
   self._f = open(filename, 'rb' if read_mode else 'a+b')
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/prometheus_metrics/gauge_livesum_37367.db'

An easy fix could be #424.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions