Skip to content
Open
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
2 changes: 2 additions & 0 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> N
self._raise_if_not_observable()
if amount < 0:
raise ValueError('Counters can only be incremented by non-negative amounts.')
if math.isnan(amount):
raise ValueError('Counters cannot be incremented by NaN.')
self._value.inc(amount)
if exemplar:
_validate_exemplar(exemplar)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def test_clear_without_labels_is_noop(self):
def test_negative_increment_raises(self):
self.assertRaises(ValueError, self.counter.inc, -1)

def test_nan_increment_raises(self):
self.counter.inc(2)
self.assertRaises(ValueError, self.counter.inc, float('nan'))
self.assertEqual(2, self.registry.get_sample_value('c_total'))

def test_function_decorator(self):
@self.counter.count_exceptions(ValueError)
def f(r):
Expand Down