Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation and added code warning for remove and clear in multi-process mode #1003

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/content/multiprocess/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This comes with a number of limitations:
- The pushgateway cannot be used
- Gauges cannot use the `pid` label
- Exemplars are not supported
- Remove and Clear of labels are currently not supported in multiprocess mode.

There's several steps to getting this working:

Expand Down
10 changes: 10 additions & 0 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Any, Callable, Dict, Iterable, List, Literal, Optional, Sequence, Tuple,
Type, TypeVar, Union,
)
import warnings

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

def remove(self, *labelvalues: Any) -> None:
if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ:
warnings.warn(
"Removal of labels has not been implemented in multi-process mode yet.",
UserWarning)

if not self._labelnames:
raise ValueError('No label names were set when constructing %s' % self)

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

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

Expand Down
16 changes: 16 additions & 0 deletions tests/test_multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,22 @@ def test_missing_gauge_file_during_merge(self):
os.path.join(self.tempdir, 'gauge_livesum_9999999.db'),
]))

def test_remove_clear_warning(self):
os.environ['PROMETHEUS_MULTIPROC_DIR'] = self.tempdir
with warnings.catch_warnings(record=True) as w:
values.ValueClass = get_value_class()
registry = CollectorRegistry()
collector = MultiProcessCollector(registry)
counter = Counter('c', 'help', labelnames=['label'], registry=None)
counter.labels('label').inc()
counter.remove('label')
counter.clear()
assert os.environ['PROMETHEUS_MULTIPROC_DIR'] == self.tempdir
assert issubclass(w[0].category, UserWarning)
assert "Removal of labels has not been implemented" in str(w[0].message)
assert issubclass(w[-1].category, UserWarning)
assert "Clearing labels has not been implemented" in str(w[-1].message)


class TestMmapedDict(unittest.TestCase):
def setUp(self):
Expand Down