From d8605385a7e816f4a16e00c8be9a9fe2de1c9d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artu=CC=84rs=20Meks=CC=8Cs?= Date: Tue, 16 Jul 2019 20:38:03 +0300 Subject: [PATCH 1/3] Customisable pid provider MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Artūrs Mekšs --- prometheus_client/pidprovider.py | 9 +++++++ prometheus_client/values.py | 5 ++-- tests/test_pidprovider.py | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 prometheus_client/pidprovider.py create mode 100644 tests/test_pidprovider.py diff --git a/prometheus_client/pidprovider.py b/prometheus_client/pidprovider.py new file mode 100644 index 00000000..b41bc3f1 --- /dev/null +++ b/prometheus_client/pidprovider.py @@ -0,0 +1,9 @@ +import os + + +class Pidprovider(object): + source = os.getpid + + @classmethod + def getpid(cls): + return cls.source() diff --git a/prometheus_client/values.py b/prometheus_client/values.py index 2831665a..5bcc1c58 100644 --- a/prometheus_client/values.py +++ b/prometheus_client/values.py @@ -4,7 +4,7 @@ from threading import Lock from .mmap_dict import mmap_key, MmapedDict - +from .pidprovider import Pidprovider class MutexValue(object): """A float protected by a mutex.""" @@ -27,8 +27,7 @@ def get(self): with self._lock: return self._value - -def MultiProcessValue(_pidFunc=os.getpid): +def MultiProcessValue(_pidFunc=Pidprovider.getpid): files = {} values = [] pid = {'value': _pidFunc()} diff --git a/tests/test_pidprovider.py b/tests/test_pidprovider.py new file mode 100644 index 00000000..bb341cc8 --- /dev/null +++ b/tests/test_pidprovider.py @@ -0,0 +1,45 @@ +import os +import shutil +import sys +import tempfile + +from prometheus_client import values +from prometheus_client.core import Counter +from prometheus_client.values import MultiProcessValue, Pidprovider + +if sys.version_info < (2, 7): + # We need the skip decorators from unittest2 on Python 2.6. + import unittest2 as unittest +else: + import unittest + + +class TestPidprovider(unittest.TestCase): + def setUp(self): + self.tempdir = tempfile.mkdtemp() + os.environ['prometheus_multiproc_dir'] = self.tempdir + self.originalValueClass = values.ValueClass + self.originalPidproviderSource = Pidprovider.source + values.ValueClass = MultiProcessValue() + + def tearDown(self): + del os.environ['prometheus_multiproc_dir'] + shutil.rmtree(self.tempdir) + values.ValueClass = self.originalValueClass + Pidprovider.source = self.originalPidproviderSource + + # can not inspect the files cache directly, as it's a closure, so we + # check for the actual files themselves + def _files(self): + fs = os.listdir(self.tempdir) + fs.sort() + return fs + + def test_with_default_pidprovider(self): + Counter('c1', 'c1', registry=None) + self.assertEqual(self._files(), ['counter_{}.db'.format(os.getpid())]) + + def test_with_user_defined_pidprovider(self): + Pidprovider.source = lambda: 1234 + Counter('c1', 'c1', registry=None) + self.assertEqual(self._files(), ['counter_1234.db'.format(os.getpid())]) From 3e0350a6bb855fceed96ad8913dd8abbd2ff8e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artu=CC=84rs=20Meks=CC=8Cs?= Date: Thu, 18 Jul 2019 14:16:23 +0300 Subject: [PATCH 2/3] Fix syntax to follow [PEP 8] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Artūrs Mekšs --- prometheus_client/mmap_dict.py | 2 +- prometheus_client/values.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/prometheus_client/mmap_dict.py b/prometheus_client/mmap_dict.py index 274b7389..95ee3c62 100644 --- a/prometheus_client/mmap_dict.py +++ b/prometheus_client/mmap_dict.py @@ -37,7 +37,7 @@ def _read_all_values(data, used=0): if encoded_len + pos > used: raise RuntimeError('Read beyond file size detected, file is corrupted.') pos += 4 - encoded_key = data[pos : pos + encoded_len] + encoded_key = data[pos: pos + encoded_len] padded_len = encoded_len + (8 - (encoded_len + 4) % 8) pos += padded_len value = _unpack_double(data, pos)[0] diff --git a/prometheus_client/values.py b/prometheus_client/values.py index 5bcc1c58..2d7c26cb 100644 --- a/prometheus_client/values.py +++ b/prometheus_client/values.py @@ -6,6 +6,7 @@ from .mmap_dict import mmap_key, MmapedDict from .pidprovider import Pidprovider + class MutexValue(object): """A float protected by a mutex.""" @@ -27,6 +28,7 @@ def get(self): with self._lock: return self._value + def MultiProcessValue(_pidFunc=Pidprovider.getpid): files = {} values = [] From f18de677944742bc4ee175e2d2786936432fc78e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artu=CC=84rs=20Meks=CC=8Cs?= Date: Thu, 18 Jul 2019 17:55:59 +0300 Subject: [PATCH 3/3] Fixing tests for python 2x MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Artūrs Mekšs --- tests/test_pidprovider.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pidprovider.py b/tests/test_pidprovider.py index bb341cc8..4296691a 100644 --- a/tests/test_pidprovider.py +++ b/tests/test_pidprovider.py @@ -37,9 +37,9 @@ def _files(self): def test_with_default_pidprovider(self): Counter('c1', 'c1', registry=None) - self.assertEqual(self._files(), ['counter_{}.db'.format(os.getpid())]) + self.assertEqual(self._files(), ['counter_{0}.db'.format(os.getpid())]) def test_with_user_defined_pidprovider(self): - Pidprovider.source = lambda: 1234 + Pidprovider.source = staticmethod(lambda: 1234) Counter('c1', 'c1', registry=None) - self.assertEqual(self._files(), ['counter_1234.db'.format(os.getpid())]) + self.assertEqual(self._files(), ['counter_1234.db'])