diff --git a/prometheus_client/multiprocess.py b/prometheus_client/multiprocess.py index 2fa97168..a45d1ec7 100644 --- a/prometheus_client/multiprocess.py +++ b/prometheus_client/multiprocess.py @@ -12,6 +12,8 @@ class MultiProcessCollector(object): """Collector for files for multi-process mode.""" def __init__(self, registry, path=os.environ.get('prometheus_multiproc_dir')): + if not path or not os.path.isdir(path): + raise ValueError('env prometheus_multiproc_dir is not set or not a directory') self._path = path if registry: registry.register(self) diff --git a/tests/test_multiprocess.py b/tests/test_multiprocess.py index d122dbbf..8f282d18 100644 --- a/tests/test_multiprocess.py +++ b/tests/test_multiprocess.py @@ -158,3 +158,28 @@ def test_multi_expansion(self): def tearDown(self): os.unlink(self.tempfile) + +class TestUnsetEnv(unittest.TestCase): + def setUp(self): + self.registry = CollectorRegistry() + fp, self.tmpfl = tempfile.mkstemp() + os.close(fp) + + def test_unset_syncdir_env(self): + self.assertRaises( + ValueError, + MultiProcessCollector, + self.registry + ) + + def test_file_syncpath(self): + registry = CollectorRegistry() + self.assertRaises( + ValueError, + MultiProcessCollector, + registry, + self.tmpfl + ) + + def tearDown(self): + os.remove(self.tmpfl)