diff --git a/prometheus_client/multiprocess.py b/prometheus_client/multiprocess.py index 6c953747..514d7784 100644 --- a/prometheus_client/multiprocess.py +++ b/prometheus_client/multiprocess.py @@ -156,6 +156,11 @@ 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', os.environ.get('prometheus_multiproc_dir')) + + if path is None: + # Avoid error when environment is not correctly set-up + return + for f in glob.glob(os.path.join(path, f'gauge_livesum_{pid}.db')): os.remove(f) for f in glob.glob(os.path.join(path, f'gauge_liveall_{pid}.db')): diff --git a/tests/test_multiprocess.py b/tests/test_multiprocess.py index 9ec0578f..29739997 100644 --- a/tests/test_multiprocess.py +++ b/tests/test_multiprocess.py @@ -60,7 +60,7 @@ def _value_class(self): return def tearDown(self): - del os.environ['PROMETHEUS_MULTIPROC_DIR'] + os.environ.pop('PROMETHEUS_MULTIPROC_DIR', None) shutil.rmtree(self.tempdir) values.ValueClass = MutexValue @@ -303,6 +303,11 @@ def test_missing_gauge_file_during_merge(self): os.path.join(self.tempdir, 'gauge_livesum_9999999.db'), ])) + def test_mark_dead_no_env(self): + del os.environ['PROMETHEUS_MULTIPROC_DIR'] + + mark_process_dead(123) + class TestMmapedDict(unittest.TestCase): def setUp(self):