Skip to content

Commit

Permalink
Fix histograms not being unpickled correctly
Browse files Browse the repository at this point in the history
_Histogram._setstate does not pass heap to Struct._setstate, which caused
it to be initialized to None. This results in exemplars breaking, since
they access heap. Fix this, and update the unpickling test to be more
functional.

Fixes: 78f0ce7 ("types: Rework state-setting functions")
Signed-off-by: Sean Anderson <[email protected]>
  • Loading branch information
Forty-Bot committed Nov 18, 2023
1 parent 3a68e8e commit a2f0255
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mpmetrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def __init__(self, mem, thresholds, heap, **kwargs):
self._created.value = time.time()

def _setstate(self, mem, heap):
Struct._setstate(self, mem)
Struct._setstate(self, mem, heap)
self.thresholds = tuple(threshold.value for threshold in self._thresholds)

def observe(self, amount, exemplar=None):
Expand Down
1 change: 1 addition & 0 deletions mpmetrics/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def __init__(self, mem, heap):
self._heap = heap

def _setstate(self, mem, heap):
assert heap is not None
super()._setstate(mem, heap)
self._heap = heap

Expand Down
3 changes: 2 additions & 1 deletion tests/metric_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ def test_time(registry, cls, name):
@pytest.mark.parametrize('cls', (Counter, Gauge, Summary, Histogram))
def test_pickle(registry, cls):
metric = cls('name', 'help', labelnames=('l'), registry=registry)
pickle.loads(pickle.dumps(metric.labels('x')))
metric.labels('x')
assert list(metric.collect()) == list(pickle.loads(pickle.dumps(metric)).collect())

class TestEnum:
@pytest.fixture
Expand Down

0 comments on commit a2f0255

Please sign in to comment.