From 139a036dadacf18b46977e0de4316e3318412940 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 30 Jun 2024 08:15:55 +0200 Subject: [PATCH] fix: handle errors gracefully in user count updates Return False instead of 0 when fetching registered users fails, and skip updating the gauge if an error occurs. Improves error handling, avoiding misleading user count updates. --- prometheus_piped.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prometheus_piped.py b/prometheus_piped.py index 14a78ad..56fe0c4 100755 --- a/prometheus_piped.py +++ b/prometheus_piped.py @@ -28,7 +28,7 @@ def fetch_registered_users(domain): return int(match.group(1)) except Exception as e: print(f"Error fetching registered users from {domain}: {e}") - return 0 + return False def update_registered_users(domains, update_interval): @@ -36,7 +36,8 @@ def update_registered_users(domains, update_interval): for domain in domains: # Fetch the number of registered users and update the gauge registered_users = fetch_registered_users(domain) - registered_users_gauge.labels(domain=domain).set(registered_users) + if registered_users: + registered_users_gauge.labels(domain=domain).set(registered_users) # Sleep for a while before fetching the data again time.sleep(update_interval)