From f7a13962f0379a4e0da025a6f15b059c4e13ebed Mon Sep 17 00:00:00 2001 From: hogweed1 Date: Mon, 12 May 2025 21:25:08 +1000 Subject: [PATCH] init gauges once init gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges onceinit gauges once --- handlers/metrics.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/handlers/metrics.py b/handlers/metrics.py index f4450d6..5951d8b 100644 --- a/handlers/metrics.py +++ b/handlers/metrics.py @@ -2,7 +2,6 @@ import json import time from prometheus_client import Gauge, Info - class user_info_struct: user_id: str user_fullname: str @@ -12,14 +11,16 @@ class user_info_struct: chat_id: int - +initialized = False def metric_wrap(chatID:int, userName: str, length: int, attemptsCount: int): """Pass here chat ID, username, pipisa length and attempts count to wrap them into metric.""" - pipisa_length_metric = Gauge('user_length_total', 'Pipisa length', labelnames=['ChatID', 'Username']) - user_attempt_metric = Gauge('user_attempts_total', 'Attempts count', labelnames=['ChatID', 'Username']) - user_last_attempt_metric = Gauge('user_last_attempt_time', 'Last attempt time', labelnames=['ChatID', 'Username']) + if not initialized: + pipisa_length_metric = Gauge('user_length_total', 'Pipisa length', labelnames=['ChatID', 'Username']) + user_attempt_metric = Gauge('user_attempts_total', 'Attempts count', labelnames=['ChatID', 'Username']) + user_last_attempt_metric = Gauge('user_last_attempt_time', 'Last attempt time', labelnames=['ChatID', 'Username']) + initialized = True pipisa_length_metric.labels(chatID, userName).set(length) user_attempt_metric.labels(chatID, userName).set(attemptsCount)