From 7c4eeec956199cc50844509aac8047883b76574b Mon Sep 17 00:00:00 2001 From: Amit Bhanja Date: Wed, 7 Feb 2024 11:16:51 +0100 Subject: [PATCH] Added prometheus client -- Defined metrics --- src/metrics.py | 23 +++++++++++++++++------ src/notify-email.py | 27 +++------------------------ src/notify-slack.py | 26 +++----------------------- tests/test_notify_email.py | 2 +- tests/test_notify_slack.py | 2 +- 5 files changed, 25 insertions(+), 55 deletions(-) diff --git a/src/metrics.py b/src/metrics.py index 1cacbfb..aa4c5f0 100644 --- a/src/metrics.py +++ b/src/metrics.py @@ -1,14 +1,25 @@ +import os +import shutil +from prometheus_client import multiprocess, CollectorRegistry from prometheus_client import Counter, Enum -NAMESPACE = 'kafka_notify' +path = os.environ.get('PROMETHEUS_MULTIPROC_DIR') +if os.path.exists(path): + shutil.rmtree(path) + os.mkdir(path) + +registry = CollectorRegistry() +multiprocess.MultiProcessCollector(registry) PROCESS_STATES = Enum('process_states', 'State of the process', states=['idle', 'processing', - 'error - recoverable', 'error - ' - 'need restart'] - , namespace=NAMESPACE) + 'error - recoverable', 'error - ' + 'need restart'] + , namespace='kafka_notify', registry=registry) NOTIFICATIONS_SENT = Counter('notifications_sent', 'Number of notifications sent', - ['type', 'name', 'endpoint'], namespace=NAMESPACE) + ['type', 'name', 'endpoint'], namespace='kafka_notify', + registry=registry) NOTIFICATIONS_ERROR = Counter('notifications_error', 'Number of notifications that could not be sent due to error', - ['type', 'name', 'endpoint', 'exception'], namespace=NAMESPACE) + ['type', 'name', 'endpoint', 'exception'], namespace='kafka_notify', + registry=registry) diff --git a/src/notify-email.py b/src/notify-email.py index 9fce4e8..5483f09 100755 --- a/src/notify-email.py +++ b/src/notify-email.py @@ -13,8 +13,8 @@ from notify_deps import get_logger, timestamp_convert, main from notify_deps import NUVLA_ENDPOINT, prometheus_exporter_port -from prometheus_client import start_http_server, multiprocess, CollectorRegistry -from prometheus_client import Counter, Enum +from prometheus_client import start_http_server +from metrics import PROCESS_STATES, NOTIFICATIONS_SENT, NOTIFICATIONS_ERROR, registry log_local = get_logger('email') @@ -33,27 +33,6 @@ IMG_ALERT_OK = 'ui/images/nuvla-alert-ok.png' IMG_ALERT_NOK = 'ui/images/nuvla-alert-nok.png' -path = os.environ.get('PROMETHEUS_MULTIPROC_DIR') -if os.path.exists(path): - shutil.rmtree(path) - os.mkdir(path) - -registry = CollectorRegistry() -multiprocess.MultiProcessCollector(registry) - -PROCESS_STATES = Enum('process_states', 'State of the process', states=['idle', 'processing', - 'error - recoverable', 'error - ' - 'need restart'] - , namespace='kafka_notify', registry=registry) - -NOTIFICATIONS_SENT = Counter('notifications_sent', 'Number of notifications sent', - ['type', 'name', 'endpoint'], namespace='kafka_notify', - registry=registry) -NOTIFICATIONS_ERROR = Counter('notifications_error', - 'Number of notifications that could not be sent due to error', - ['type', 'name', 'endpoint', 'exception'], namespace='kafka_notify', - registry=registry) - class SendFailedMaxAttempts(Exception): pass @@ -237,5 +216,5 @@ def init_email_templates(default=EMAIL_TEMPLATE_DEFAULT_FILE, if __name__ == "__main__": init_email_templates() set_smtp_params() - start_http_server(prometheus_exporter_port()) + start_http_server(prometheus_exporter_port(), registry=registry) main(worker, KAFKA_TOPIC, KAFKA_GROUP_ID) diff --git a/src/notify-slack.py b/src/notify-slack.py index a6edcc5..2e3d454 100755 --- a/src/notify-slack.py +++ b/src/notify-slack.py @@ -10,8 +10,8 @@ from notify_deps import get_logger, timestamp_convert, main from notify_deps import NUVLA_ENDPOINT, prometheus_exporter_port -from prometheus_client import start_http_server, multiprocess, CollectorRegistry -from prometheus_client import Counter, Enum +from prometheus_client import start_http_server +from metrics import PROCESS_STATES, NOTIFICATIONS_SENT, NOTIFICATIONS_ERROR, registry KAFKA_TOPIC = os.environ.get('KAFKA_TOPIC') or 'NOTIFICATIONS_SLACK_S' KAFKA_GROUP_ID = 'nuvla-notification-slack' @@ -24,26 +24,6 @@ COLOR_OK = "#2C9442" COLOR_NOK = "#B70B0B" -path = os.environ.get('PROMETHEUS_MULTIPROC_DIR') -if os.path.exists(path): - shutil.rmtree(path) - os.mkdir(path) - -registry = CollectorRegistry() -multiprocess.MultiProcessCollector(registry) - -PROCESS_STATES = Enum('process_states', 'State of the process', states=['idle', 'processing', - 'error - recoverable', 'error - ' - 'need restart'] - , namespace='kafka_notify', registry=registry) - -NOTIFICATIONS_SENT = Counter('notifications_sent', 'Number of notifications sent', - ['type', 'name', 'endpoint'], namespace='kafka_notify', - registry=registry) -NOTIFICATIONS_ERROR = Counter('notifications_error', - 'Number of notifications that could not be sent due to error', - ['type', 'name', 'endpoint', 'exception'], namespace='kafka_notify', - registry=registry) def now_timestamp(): @@ -161,5 +141,5 @@ def worker(workq: multiprocessing.Queue): if __name__ == "__main__": - start_http_server(prometheus_exporter_port()) + start_http_server(prometheus_exporter_port(), registry=registry) main(worker, KAFKA_TOPIC, KAFKA_GROUP_ID) diff --git a/tests/test_notify_email.py b/tests/test_notify_email.py index c88cd4f..7dba962 100644 --- a/tests/test_notify_email.py +++ b/tests/test_notify_email.py @@ -4,7 +4,7 @@ import shutil from prometheus_client import multiprocess -os.environ['PROMETHEUS_MULTIPROC_DIR'] = '/tmp/test' +os.environ['PROMETHEUS_MULTIPROC_DIR'] = '' os.path.exists = Mock(return_value=True) os.mkdir = Mock() shutil.rmtree = Mock() diff --git a/tests/test_notify_slack.py b/tests/test_notify_slack.py index 52803de..c37cb01 100644 --- a/tests/test_notify_slack.py +++ b/tests/test_notify_slack.py @@ -4,7 +4,7 @@ import shutil from prometheus_client import multiprocess -os.environ['PROMETHEUS_MULTIPROC_DIR'] = '/tmp/test' +os.environ['PROMETHEUS_MULTIPROC_DIR'] = '' os.path.exists = Mock(return_value=True) os.mkdir = Mock() shutil.rmtree = Mock()