8000 Update documentation and code warning for remove and clear in multi-p… · HelloBroBro/client_python@998d8af · GitHub
[go: up one dir, main page]

Skip to content

Commit 998d8af

Browse files
authored
Update documentation and code warning for remove and clear in multi-process mode (prometheus#1003)
Signed-off-by: Yuanchen Wang <yctomwang123@gmail.com>
1 parent 147c9d1 commit 998d8af

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

docs/content/multiprocess/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This comes with a number of limitations:
1818
- The pushgateway cannot be used
1919
- Gauges cannot use the `pid` label
2020
- Exemplars are not supported
21+
- Remove and Clear of labels are currently not supported in multiprocess mode.
2122

2223
There's several steps to getting this working:
2324

prometheus_client/metrics.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Any, Callable, Dict, Iterable, List, Literal, Optional, Sequence, Tuple,
77
Type, TypeVar, Union,
88
)
9+
import warnings
910

1011
from . import values # retain this import style for testability
1112
from .context_managers import ExceptionCounter, InprogressTracker, Timer
@@ -210,6 +211,11 @@ def labels(self: T, *labelvalues: Any, **labelkwargs: Any) -> T:
210211
return self._metrics[labelvalues]
211212

212213
def remove(self, *labelvalues: Any) -> None:
214+
if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ:
215+
warnings.warn(
216+
"Removal of labels has not been implemented in multi-process mode yet.",
217+
UserWarning)
218+
213219
if not self._labelnames:
214220
raise ValueError('No label names were set when constructing %s' % self)
215221

@@ -222,6 +228,10 @@ def remove(self, *labelvalues: Any) -> None:
222228

223229
def clear(self) -> None:
224230
"""Remove all labelsets from the metric"""
231+
if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ:
232+
warnings.warn(
233+
"Clearing labels has not been implemented in multi-process mode yet",
234+
UserWarning)
225235
with self._lock:
226236
self._metrics = {}
227237

tests/test_multiprocess.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,22 @@ def test_missing_gauge_file_during_merge(self):
381381
os.path.join(self.tempdir, 'gauge_livesum_9999999.db'),
382382
]))
383383

384+
def test_remove_clear_warning(self):
385+
os.environ['PROMETHEUS_MULTIPROC_DIR'] = self.tempdir
386+
with warnings.catch_warnings(record=True) as w:
387+
values.ValueClass = get_value_class()
388+
registry = CollectorRegistry()
389+
collector = MultiProcessCollector(registry)
390+
counter = Counter('c', 'help', labelnames=['label'], registry=None)
391+
counter.labels('label').inc()
392+
counter.remove('label')
393+
counter.clear()
394+
assert os.environ['PROMETHEUS_MULTIPROC_DIR'] == self.tempdir
395+
assert issubclass(w[0].category, UserWarning)
396+
assert "Removal of labels has not been implemented" in str(w[0].message)
397+
assert issubclass(w[-1].category, UserWarning)
398+
assert "Clearing labels has not been implemented" in str(w[-1].message)
399+
384400

385401
class TestMmapedDict(unittest.TestCase):
386402
def setUp(self):

0 commit comments

Comments
 (0)
0