From 89fe9d788fb68e217c29d3897f2db991fe13e05f Mon Sep 17 00:00:00 2001 From: Maaike Date: Fri, 30 Aug 2024 09:25:44 +0200 Subject: [PATCH 1/2] to deal with first label in metric --- wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py b/wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py index f1d7ebb9..118f23ec 100644 --- a/wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py +++ b/wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py @@ -29,6 +29,7 @@ import sys import json +import time from prometheus_client import start_http_server, Counter, Gauge @@ -166,6 +167,12 @@ def sub_mqtt_metrics(client, userdata, msg): wsi = 'none' if 'wigos_station_identifier' in m['properties']: wsi = m['properties']['wigos_station_identifier'] + # if label wsi is not present in notify_wsi_total, set it to 0 and sleep 5 seconds before incrementing + if wsi not in notify_wsi_total._metrics: + notify_wsi_total.labels(wsi).inc(0) + failure_wsi_total.labels(wsi).inc(0) + station_wsi.labels(wsi).set(1) + time.sleep(5) notify_wsi_total.labels(wsi).inc(1) failure_wsi_total.labels(wsi).inc(0) station_wsi.labels(wsi).set(1) From adad0831cd41fb2796d2955ce3318c3b0e6c439c Mon Sep 17 00:00:00 2001 From: Maaike Date: Fri, 30 Aug 2024 10:17:02 +0200 Subject: [PATCH 2/2] log and flake8 --- wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py b/wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py index 118f23ec..e57c2b5c 100644 --- a/wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py +++ b/wis2box-mqtt-metrics-collector/mqtt_metrics_collector.py @@ -167,8 +167,9 @@ def sub_mqtt_metrics(client, userdata, msg): wsi = 'none' if 'wigos_station_identifier' in m['properties']: wsi = m['properties']['wigos_station_identifier'] - # if label wsi is not present in notify_wsi_total, set it to 0 and sleep 5 seconds before incrementing + # if label wsi is not in notify_wsi_total, set to 0 and sleep 5s if wsi not in notify_wsi_total._metrics: + logger.info(f"new station: {wsi}, sleep 5s before incrementing") notify_wsi_total.labels(wsi).inc(0) failure_wsi_total.labels(wsi).inc(0) station_wsi.labels(wsi).set(1)