Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiprocess: don't crash on missing gauge_live/sum files #424

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion prometheus_client/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from .samples import Sample
from .utils import floatToGoString

try: # Python3
FileNotFoundError
except NameError: # Python >= 2.5
FileNotFoundError = IOError

MP_METRIC_HELP = 'Multiprocess metric'


Expand Down Expand Up @@ -54,7 +59,16 @@ def _parse_key(key):
for f in files:
parts = os.path.basename(f).split('_')
typ = parts[0]
for key, value, pos in MmapedDict.read_all_values_from_file(f):
try:
file_values = MmapedDict.read_all_values_from_file(f)
except FileNotFoundError:
if typ == 'gauge' and parts[1] in ('liveall', 'livesum'):
# Those files can disappear between the glob of collect
# and now (via a mark_process_dead call) so don't fail if
# the file is missing
continue
raise
for key, value, pos in file_values:
metric_name, name, labels, labels_key = _parse_key(key)

metric = metrics.get(metric_name)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ def add_label(key, value):

self.assertEqual(metrics['h'].samples, expected_histogram)

def test_missing_gauge_file_during_merge(self):
# These files don't exist, just like if mark_process_dead(9999999) had been
# called during self.collector.collect(), after the glob found it
# but before the merge actually happened.
# This should not raise and return no metrics
self.assertFalse(self.collector.merge([
os.path.join(self.tempdir, 'gauge_liveall_9999999.db'),
os.path.join(self.tempdir, 'gauge_livesum_9999999.db'),
]))


class TestMmapedDict(unittest.TestCase):
def setUp(self):
Expand Down