mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-05-27 16:25:12 +02:00
fix: safely parse metric labels (#948)
This commit is contained in:
parent
6af12f416f
commit
c10f2694a0
2 changed files with 96 additions and 2 deletions
|
|
@ -4,6 +4,7 @@ Provides comprehensive monitoring of system performance, query patterns, and res
|
|||
"""
|
||||
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
import asyncio
|
||||
import inspect
|
||||
|
|
@ -276,6 +277,26 @@ class MetricsCollector:
|
|||
return f"{name}{{{label_str}}}"
|
||||
|
||||
|
||||
def _extract_metric_label(metric_name: str, label: str) -> Optional[str]:
|
||||
"""Extract a label value from an internal metric key."""
|
||||
labels_start = metric_name.find('{')
|
||||
labels_end = metric_name.find('}', labels_start + 1)
|
||||
|
||||
if labels_start == -1 or labels_end == -1:
|
||||
return None
|
||||
|
||||
labels = metric_name[labels_start + 1:labels_end]
|
||||
label_match = re.search(
|
||||
rf'(?:^|,){re.escape(label)}=(?:"([^"]*)"|([^,]*))',
|
||||
labels,
|
||||
)
|
||||
if not label_match:
|
||||
return None
|
||||
|
||||
quoted_value, unquoted_value = label_match.groups()
|
||||
return quoted_value if quoted_value is not None else unquoted_value
|
||||
|
||||
|
||||
class PerformanceMonitor:
|
||||
"""Monitors system performance and component health."""
|
||||
|
||||
|
|
@ -474,8 +495,8 @@ class PerformanceMonitor:
|
|||
# Cache performance
|
||||
cache_types = set()
|
||||
for metric_name in self.metrics_collector.counters.keys():
|
||||
if 'cache_type=' in metric_name:
|
||||
cache_type = metric_name.split('cache_type=')[1].split(',')[0].split('}')[0]
|
||||
cache_type = _extract_metric_label(metric_name, 'cache_type')
|
||||
if cache_type is not None:
|
||||
cache_types.add(cache_type)
|
||||
|
||||
for cache_type in cache_types:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue