8000 Multiprocess environment variable in upper case (#624) · ethervoid/client_python@f22d38f · GitHub
[go: up one dir, main page]

Skip to content

Commit f22d38f

Browse files
authored
Multiprocess environment variable in upper case (prometheus#624)
* Use upper case environment variable for multi process * add test cases for deprecation warning on variable capitalization * Remove the warning when both are used and rephrase the message to be more deprecation aware. Signed-off-by: Marc-Aurèle Brothier <m@brothier.org>
1 parent 1a3ba21 commit f22d38f

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

prometheus_client/multiprocess.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import glob
55
import json
66
import os
7+
import warnings
78

89
from .metrics_core import Metric
910
from .mmap_dict import MmapedDict
@@ -23,9 +24,13 @@ class MultiProcessCollector(object):
2324

2425
def __init__(self, registry, path=None):
2526
if path is None:
26-
path = os.environ.get('prometheus_multiproc_dir')
27+
# This deprecation warning can go away in a few releases when removing the compatibility
28+
if 'prometheus_multiproc_dir' in os.environ and 'PROMETHEUS_MULTIPROC_DIR' not in os.environ:
29+
os.environ['PROMETHEUS_MULTIPROC_DIR'] = os.environ['prometheus_multiproc_dir']
30+
warnings.warn("prometheus_multiproc_dir variable has been deprecated in favor of the upper case naming PROMETHEUS_MULTIPROC_DIR", DeprecationWarning)
31+
path = os.environ.get('PROMETHEUS_MULTIPROC_DIR')
2732
if not path or not os.path.isdir(path):
28-
raise ValueError('env prometheus_multiproc_dir is not set or not a directory')
33+
raise ValueError('env PROMETHEUS_MULTIPROC_DIR is not set or not a directory')
2934
self._path = path
3035
if registry:
3136
registry.register(self)
@@ -66,7 +71,7 @@ def _parse_key(key):
6671
# the file is missing
6772
continue
6873
raise
69-
for key, value, pos in file_values:
74+
for key, value, _ in file_values:
7075
metric_name, name, labels, labels_key = _parse_key(key)
7176

7277
metric = metrics.get(metric_name)
@@ -152,7 +157,7 @@ def collect(self):
152157
def mark_process_dead(pid, path=None):
153158
"""Do bookkeeping for when one process dies in a multi-process setup."""
154159
if path is None:
155-
path = os.environ.get('prometheus_multiproc_dir')
160+
path = os.environ.get('PROMETHEUS_MULTIPROC_DIR')
156161
for f in glob.glob(os.path.join(path, 'gauge_livesum_{0}.db'.format(pid))):
157162
os.remove(f)
158163
for f in glob.glob(os.path.join(path, 'gauge_liveall_{0}.db'.format(pid))):

prometheus_client/values.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
from threading import Lock
5+
import warnings
56

67
from .mmap_dict import mmap_key, MmapedDict
78

@@ -51,6 +52,10 @@ class MmapedValue(object):
5152

5253
def __init__(self, typ, metric_name, name, labelnames, labelvalues, multiprocess_mode='', **kwargs):
5354
self._params = typ, metric_name, name, labelnames, labelvalues, multiprocess_mode
55+
# This deprecation warning can go away in a few releases when removing the compatibility
56+
if 'prometheus_multiproc_dir' in os.environ and 'PROMETHEUS_MULTIPROC_DIR' not in os.environ:
57+
os.environ['PROMETHEUS_MULTIPROC_DIR'] = os.environ['prometheus_multiproc_dir']
58+
warnings.warn("prometheus_multiproc_dir variable has been deprecated in favor of the upper case naming PROMETHEUS_MULTIPROC_DIR", DeprecationWarning)
5459
with lock:
5560
self.__check_for_pid_change()
5661
self.__reset()
@@ -64,7 +69,7 @@ def __reset(self):
6469
file_prefix = typ
6570
if file_prefix not in files:
6671
filename = os.path.join(
67-
os.environ['prometheus_multiproc_dir'],
72+
os.environ.get('PROMETHEUS_MULTIPROC_DIR'),
6873
'{0}_{1}.db'.format(file_prefix, pid['value']))
6974

7075
files[file_prefix] = MmapedDict(filename)
@@ -108,7 +113,7 @@ def get_value_class():
108113
# This needs to be chosen before the first metric is constructed,
109114
# and as that may be in some arbitrary library the user/admin has
110115
# no control over we use an environment variable.
111-
if 'prometheus_multiproc_dir' in os.environ:
116+
if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ:
112117
return MultiProcessValue()
113118
else:
114119
return MutexValue

tests/test_multiprocess.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import sys
77
import tempfile
8+
import warnings
89

910
from prometheus_client import mmap_dict, values
1011
from prometheus_client.core import (
@@ -13,7 +14,9 @@
1314
from prometheus_client.multiprocess import (
1415
mark_process_dead, MultiProcessCollector,
1516
)
16-
from prometheus_client.values import MultiProcessValue, MutexValue
17+
from prometheus_client.values import (
18+
get_value_class, MultiProcessValue, MutexValue,
19+
)
1720

1821
if sys.version_info < (2, 7):
1922
# We need the skip decorators from unittest2 on Python 2.6.
@@ -22,20 +25,44 @@
2225
import unittest
2326

2427

25-
class TestMultiProcess(unittest.TestCase):
28+
class TestMultiProcessDeprecation(unittest.TestCase):
2629
def setUp(self):
2730
self.tempdir = tempfile.mkdtemp()
31+
32+
def tearDown(self):
33+
del os.environ['prometheus_multiproc_dir']
34+
del os.environ['PROMETHEUS_MULTIPROC_DIR']
35+
values.ValueClass = MutexValue
36+
shutil.rmtree(self.tempdir)
37+
38+
def test_deprecation_warning(self):
2839
os.environ['prometheus_multiproc_dir'] = self.tempdir
40+
with warnings.catch_warnings(record=True) as w:
41+
values.ValueClass = get_value_class()
42+
registry = CollectorRegistry()
43+
collector = MultiProcessCollector(registry)
44+
Counter('c', 'help', registry=None)
45+
46+
assert os.environ['PROMETHEUS_MULTIPROC_DIR'] == self.tempdir
47+
assert len(w) == 1
48+
assert issubclass(w[-1].category, DeprecationWarning)
49+
assert "PROMETHEUS_MULTIPROC_DIR" in str(w[-1].message)
50+
51+
52+
class TestMultiProcess(unittest.TestCase):
53+
def setUp(self):
54+
self.tempdir = tempfile.mkdtemp()
55+
os.environ['PROMETHEUS_MULTIPROC_DIR'] = self.tempdir
2956
values.ValueClass = MultiProcessValue(lambda: 123)
3057
self.registry = CollectorRegistry()
31-
self.collector = MultiProcessCollector(self.registry, self.tempdir)
58+
self.collector = MultiProcessCollector(self.registry)
3259

3360
@property
3461
def _value_class(self):
3562
return
3663

3764
def tearDown(self):
38-
del os.environ['prometheus_multiproc_dir']
65+
del os.environ['PROMETHEUS_MULTIPROC_DIR']
3966
shutil.rmtree(self.tempdir)
4067
values.ValueClass = MutexValue
4168

@@ -80,7 +107,7 @@ def test_gauge_all(self):
80107
self.assertEqual(0, self.registry.get_sample_value('g', {'pid': '456'}))
81108
g1.set(1)
82109
g2.set(2)
83-
mark_process_dead(123, os.environ['prometheus_multiproc_dir'])
110+
mark_process_dead(123)
84111
self.assertEqual(1, self.registry.get_sample_value('g', {'pid': '123'}))
85112
self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
86113

@@ -94,7 +121,7 @@ def test_gauge_liveall(self):
94121
g2.set(2)
95122
self.assertEqual(1, self.registry.get_sample_value('g', {'pid': '123'}))
96123
self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
97-
mark_process_dead(123, os.environ['prometheus_multiproc_dir'])
124+
mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])
98125
self.assertEqual(None, self.registry.get_sample_value('g', {'pid': '123'}))
99126
self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
100127

@@ -124,7 +151,7 @@ def test_gauge_livesum(self):
124151
g1.set(1)
125152
g2.set(2)
126153
self.assertEqual(3, self.registry.get_sample_value('g'))
127-
mark_process_dead(123, os.environ['prometheus_multiproc_dir'])
154+
mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])
128155
self.assertEqual(2, self.registry.get_sample_value('g'))
129156

130157
def test_namespace_subsystem(self):
@@ -151,7 +178,7 @@ def test_initialization_detects_pid_change(self):
151178
# can not inspect the files cache directly, as it's a closure, so we
152179
# check for the actual files themselves
153180
def files():
154-
fs = os.listdir(os.environ['prometheus_multiproc_dir'])
181+
fs = os.listdir(os.environ['PROMETHEUS_MULTIPROC_DIR'])
155182
fs.sort()
156183
return fs
157184

@@ -240,7 +267,7 @@ def add_label(key, value):
240267
pid = 1
241268
h.labels(**labels).observe(5)
242269

243-
path = os.path.join(os.environ['prometheus_multiproc_dir'], '*.db')
270+
path = os.path.join(os.environ['PROMETHEUS_MULTIPROC_DIR'], '*.db')
244271
files = glob.glob(path)
245272
metrics = dict(
246273
(m.name, m) for m in self.collector.merge(files, accumulate=False)

0 commit comments

Comments
 (0)
0