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: 1 addition & 1 deletion src/aioprometheus/collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def get_all(self) -> List[Tuple[LabelsType, NumericValueType]]:
itself.
"""
result = []
for k in self.values:
for k in list(self.values):
# Check if is a single value dict (custom empty key)
key = (
{}
Expand Down
26 changes: 26 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from threading import Thread

from aioprometheus.collectors import (
REGISTRY,
Expand Down Expand Up @@ -166,6 +167,31 @@ def country_fetcher(x):
sorted_result = sorted(c.get_all(), key=country_fetcher)
self.assertEqual(sorted_data, sorted_result)

def test_get_all_change_iter(self) -> None:
"""
test iterating over get_all when the underlying dict changes

For https://github.com/claws/aioprometheus/issues/85
"""
c = Collector(**self.default_data)

# Create a large collection of values to increase the chance of a race
for i in range(100000):
c.set_value({f"b-{i}": "a"}, i)

# Now create a thread that will add a huge amount more values
def set_more_values():
for j in range(10000):
c.set_value({f"a-{j}": "a"}, j)

t = Thread(daemon=True, target=set_more_values)
t.start()

# Hope that due to the large amount of labels involved, we will smack
# head-first into the race of iterating over the dict whilst adding more
# unique labels to it.
list(c.get_all())


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