Skip to content

Commit

Permalink
multiprocess: don't crash on missing gauge_live/sum files (#424)
Browse files Browse the repository at this point in the history
Fixes #425

Signed-off-by: Xavier Fernandez <[email protected]>
  • Loading branch information
xavfernandez authored and brian-brazil committed Jun 18, 2019
1 parent bc7319f commit 04c112a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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

0 comments on commit 04c112a

Please sign in to comment.