From 7383a0c67461e49699de62e4e4bab2184c0c9a7e Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Tue, 10 Feb 2026 12:57:37 -0500 Subject: [PATCH] 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 https://code.lotusguild.org/LotusGuild/hwmonDaemon/issues/10 Co-Authored-By: Claude Opus 4.6 --- hwmonDaemon.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hwmonDaemon.py b/hwmonDaemon.py index 6d35342..15f39dc 100644 --- a/hwmonDaemon.py +++ b/hwmonDaemon.py @@ -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 ===