Escape special characters in Prometheus metric labels
Add escape function to sanitize backslashes, double quotes, and newlines in label values per Prometheus text format spec. Prevents corrupted metrics output from model names or paths containing these characters. Resolves #10 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3163,9 +3163,11 @@ class SystemHealthMonitor:
|
||||
hostname = health_report.get('hostname', socket.gethostname())
|
||||
metrics = []
|
||||
|
||||
# Helper to format labels
|
||||
# Helper to format labels with proper Prometheus escaping
|
||||
def labels(**kwargs) -> str:
|
||||
pairs = [f'{k}="{v}"' for k, v in kwargs.items() if v is not None]
|
||||
def escape(value):
|
||||
return str(value).replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
|
||||
pairs = [f'{k}="{escape(v)}"' for k, v in kwargs.items() if v is not None]
|
||||
return '{' + ','.join(pairs) + '}' if pairs else ''
|
||||
|
||||
# === System Info ===
|
||||
|
||||
Reference in New Issue
Block a user