From d4fb40507f8ff5cd776a7f222522f9e9197e88da Mon Sep 17 00:00:00 2001 From: Noah Kantrowitz Date: Thu, 20 Sep 2018 20:28:24 -0700 Subject: [PATCH 1/3] If only reading the mmap file, open in read-only mode. Signed-off-by: Noah Kantrowitz --- prometheus_client/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus_client/core.py b/prometheus_client/core.py index cb1e7c5b..045bb8f5 100644 --- a/prometheus_client/core.py +++ b/prometheus_client/core.py @@ -505,7 +505,7 @@ class _MmapedDict(object): Not thread safe. """ def __init__(self, filename, read_mode=False): - self._f = open(filename, 'a+b') + self._f = open(filename, 'rb' if read_mode else 'a+b') if os.fstat(self._f.fileno()).st_size == 0: self._f.truncate(_INITIAL_MMAP_SIZE) self._capacity = os.fstat(self._f.fileno()).st_size From 067a6006df131d7c2cade522b416852f038605ff Mon Sep 17 00:00:00 2001 From: Noah Kantrowitz Date: Thu, 20 Sep 2018 20:35:49 -0700 Subject: [PATCH 2/3] Fix bug in WSGI app code. Signed-off-by: Noah Kantrowitz --- prometheus_client/exposition.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 1ebeba29..29494d08 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -37,7 +37,7 @@ def make_wsgi_app(registry=core.REGISTRY): def prometheus_app(environ, start_response): params = parse_qs(environ.get('QUERY_STRING', '')) r = registry - encoder, content_type = choose_encoder(environ.get['HTTP_ACCEPT']) + encoder, content_type = choose_encoder(environ.get('HTTP_ACCEPT')) if 'name[]' in params: r = r.restricted_registry(params['name[]']) output = encoder(r) From 474b9a17c9ba42ded65e5ef901874e2eba736866 Mon Sep 17 00:00:00 2001 From: Noah Kantrowitz Date: Thu, 20 Sep 2018 20:48:51 -0700 Subject: [PATCH 3/3] Also set the mmap mode to match the read_mode flag. Signed-off-by: Noah Kantrowitz --- prometheus_client/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus_client/core.py b/prometheus_client/core.py index 045bb8f5..b05bf645 100644 --- a/prometheus_client/core.py +++ b/prometheus_client/core.py @@ -509,7 +509,7 @@ def __init__(self, filename, read_mode=False): if os.fstat(self._f.fileno()).st_size == 0: self._f.truncate(_INITIAL_MMAP_SIZE) self._capacity = os.fstat(self._f.fileno()).st_size - self._m = mmap.mmap(self._f.fileno(), self._capacity) + self._m = mmap.mmap(self._f.fileno(), self._capacity, access=mmap.ACCESS_READ if read_mode else mmap.ACCESS_WRITE) self._positions = {} self._used = _unpack_integer(self._m, 0)[0]