8000 multiprocess: don't crash on missing gauge_live/sum files (#424) · prometheus/client_python@04c112a · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 04c112a

Browse files
xavfernandezbrian-brazil
authored andcommitted
multiprocess: don't crash on missing gauge_live/sum files (#424)
Fixes #425 Signed-off-by: Xavier Fernandez <xavier.fernandez@polyconseil.fr>
1 parent bc7319f commit 04c112a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

prometheus_client/multiprocess.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
from .samples import Sample
1313
from .utils import floatToGoString
1414

15+
try: # Python3
16+
FileNotFoundError
17+
except NameError: # Python >= 2.5
18+
FileNotFoundError = IOError
19+
1520
MP_METRIC_HELP = 'Multiprocess metric'
1621

1722

@@ -54,7 +59,16 @@ def _parse_key(key):
5459
for f in files:
5560
parts = os.path.basename(f).split('_')
5661
typ = parts[0]
57-
for key, value, pos in MmapedDict.read_all_values_from_file(f):
62+
try:
63+
file_values = MmapedDict.read_all_values_from_file(f)
64+
except FileNotFoundError:
65+
if typ == 'gauge' and parts[1] in ('liveall', 'livesum'):
66+
# Those files can disappear between the glob of collect
67+
# and now (via a mark_process_dead call) so don't fail if
68+
# the file is missing
69+
continue
70+
raise
71+
for key, value, pos in file_values:
5872
metric_name, name, labels, labels_key = _parse_key(key)
5973

6074
metric = metrics.get(metric_name)

tests/test_multiprocess.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ def add_label(key, value):
270270

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

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

274284
class TestMmapedDict(unittest.TestCase):
275285
def setUp(self):

0 commit comments

Comments
 (0)
0