Skip to content

Commit e81db80

Browse files
Fix edge case in metrics collector (#3875)
* Fix edge case in metrics collector For some reason, the default/fallback value returned by `get_collection_stats` is an empty list, not an empty object. This can trigger an edge case if Prometheus metrics are scraped after a collection has been created, but the stats for the new collection haven’t been computed yet. To prevent unintended side effects, I haven’t changed the default/fallback value (as other parts of the code base might rely on it) and have instead explicitly handled empty values. * Use Airplane instead of Person entities in tests This shouldn’t make a difference, but unfortunately our test suite contains a bunch of tests that have side effects (e.g. they create entities that aren’t properly rolled back after the tests). In this particular case, other tests create Person entities, but not Airplane entities, so that’s a workaround for now.
1 parent 1b1543f commit e81db80

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

aleph/metrics/collectors.py

+5
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ def _entities(self):
283283
collection_stats = get_collection_stats(collection.id)
284284
schemata = collection_stats["schema"]["values"]
285285

286+
# For some reason, the default/fallback value returned by `get_collection_stats`
287+
# is a list, not an empty object, so we need to handle this explicitly.
288+
if not schemata:
289+
continue
290+
286291
for schema, count in schemata.items():
287292
stats[schema] += count
288293

aleph/tests/test_metrics.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -335,22 +335,29 @@ def test_entities(self):
335335
collection_1 = self.create_collection()
336336
entity_1 = self.create_entity(
337337
collection=collection_1,
338-
data={"schema": "Person", "properties": {}},
338+
data={"schema": "Airplane", "properties": {}},
339339
)
340340

341341
collection_2 = self.create_collection()
342342
entity_2 = self.create_entity(
343343
collection=collection_2,
344-
data={"schema": "Person", "properties": {}},
344+
data={"schema": "Airplane", "properties": {}},
345345
)
346346

347347
index_entity(entity_1)
348348
index_entity(entity_2)
349349

350-
# This is usually executed periodically by a worker
350+
# Ensure that metric collection succeeds even if the stats for newly collections
351+
# haven’t been computed yet
352+
reg.collect()
353+
generate_latest(reg)
354+
planes = reg.get_sample_value("aleph_entities", {"schema": "Airplane"})
355+
assert planes is None
356+
357+
# This is usually executed periodically by a worker or after collection contents are updated
351358
compute_collections()
352359

353360
reg.collect()
354361
generate_latest(reg)
355-
persons = reg.get_sample_value("aleph_entities", {"schema": "Person"})
356-
assert persons == 2
362+
planes = reg.get_sample_value("aleph_entities", {"schema": "Airplane"})
363+
assert planes == 2

0 commit comments

Comments
 (0)