2026-05-26 07:43:58 -04:00
|
|
|
"""
|
|
|
|
|
Tests for ontology monitoring metrics.
|
|
|
|
|
"""
|
|
|
|
|
|
2026-05-26 13:12:03 +01:00
|
|
|
from trustgraph.query.ontology.monitoring import (
|
|
|
|
|
PerformanceMonitor,
|
|
|
|
|
_extract_metric_label,
|
2026-05-26 07:43:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extract_metric_label_reads_unquoted_label_value():
|
|
|
|
|
metric_name = "cache_requests_total{cache_type=entity,component=ontology}"
|
|
|
|
|
|
|
|
|
|
assert _extract_metric_label(metric_name, "cache_type") == "entity"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extract_metric_label_reads_quoted_label_value():
|
|
|
|
|
metric_name = 'cache_requests_total{cache_type="entity",component="ontology"}'
|
|
|
|
|
|
|
|
|
|
assert _extract_metric_label(metric_name, "cache_type") == "entity"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extract_metric_label_returns_none_when_label_missing():
|
|
|
|
|
metric_name = "cache_requests_total{component=ontology}"
|
|
|
|
|
|
|
|
|
|
assert _extract_metric_label(metric_name, "cache_type") is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_performance_report_ignores_counters_without_cache_type_label():
|
|
|
|
|
monitor = PerformanceMonitor({"enabled": False})
|
|
|
|
|
monitor.metrics_collector.increment(
|
|
|
|
|
"cache_requests_total",
|
|
|
|
|
labels={"component": "ontology"},
|
|
|
|
|
)
|
|
|
|
|
monitor.metrics_collector.increment(
|
|
|
|
|
"cache_type=not_a_label",
|
|
|
|
|
labels={"component": "ontology"},
|
|
|
|
|
)
|
|
|
|
|
monitor.metrics_collector.increment(
|
|
|
|
|
"cache_requests_total",
|
|
|
|
|
labels={"cache_type": "entity"},
|
|
|
|
|
)
|
|
|
|
|
monitor.metrics_collector.increment(
|
|
|
|
|
"cache_hits_total",
|
|
|
|
|
labels={"cache_type": "entity"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
report = monitor.get_performance_report()
|
|
|
|
|
|
|
|
|
|
assert report["cache_performance"] == {
|
|
|
|
|
"entity": {
|
|
|
|
|
"hit_rate": 1.0,
|
|
|
|
|
"total_requests": 1.0,
|
|
|
|
|
"total_hits": 1.0,
|
|
|
|
|
}
|
|
|
|
|
}
|