Skip to content

Commit

Permalink
Added prometheus client
Browse files Browse the repository at this point in the history
-- Defined metrics
  • Loading branch information
Amit Bhanja committed Feb 13, 2024
1 parent ae4b745 commit 7c4eeec
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 55 deletions.
23 changes: 17 additions & 6 deletions src/metrics.py
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 3 additions & 24 deletions src/notify-email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
Expand Down Expand Up @@ -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)
26 changes: 3 additions & 23 deletions src/notify-slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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():
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/test_notify_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_notify_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7c4eeec

Please sign in to comment.