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 7, 2024
1 parent ef165f8 commit aac3acf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from prometheus_client import Counter

NOTIFICATIONS_SENT = Counter('notifications_sent', 'Number of notifications sent',
['type', 'subscription_name', 'endpoint'])
NOTIFICATIONS_ERROR = Counter('notifications_error', 'Number of notifications that could not be sent due to error',
['type', 'subscription_id', 'endpoint', 'exception'])
6 changes: 6 additions & 0 deletions src/notify-email.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from notify_deps import get_logger, timestamp_convert, main
from notify_deps import NUVLA_ENDPOINT
from metrics import NOTIFICATIONS_SENT, NOTIFICATIONS_ERROR
from prometheus_client import start_http_server


log_local = get_logger('email')
Expand Down Expand Up @@ -185,13 +187,16 @@ def worker(workq: multiprocessing.Queue):
html = html_content(msg.value)
send(smtp_server, recipients, subject, html)
log_local.info(f'sent: {msg} to {recipients}')
NOTIFICATIONS_SENT.labels('email', r_name, ','.join(recipients)).inc()
except smtplib.SMTPException as ex:
log_local.error(f'Failed sending email due to SMTP error: {ex}')
log_local.warning('Reconnecting to SMTP server...')
smtp_server = get_smtp_server()
log_local.warning('Reconnecting to SMTP server... done.')
NOTIFICATIONS_ERROR.labels('email', r_name, ','.join(recipients), type(ex)).inc()
except Exception as ex:
log_local.error(f'Failed sending email: {ex}')
NOTIFICATIONS_ERROR.labels('email', r_name, ','.join(recipients), type(ex)).inc()


def email_template(template_file=EMAIL_TEMPLATE_DEFAULT_FILE):
Expand All @@ -207,4 +212,5 @@ def init_email_templates(default=EMAIL_TEMPLATE_DEFAULT_FILE,
if __name__ == "__main__":
init_email_templates()
set_smtp_params()
start_http_server(9128)
main(worker, KAFKA_TOPIC, KAFKA_GROUP_ID)
2 changes: 2 additions & 0 deletions src/notify-slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from notify_deps import get_logger, timestamp_convert, main
from notify_deps import NUVLA_ENDPOINT
from prometheus_client import start_http_server

KAFKA_TOPIC = os.environ.get('KAFKA_TOPIC') or 'NOTIFICATIONS_SLACK_S'
KAFKA_GROUP_ID = 'nuvla-notification-slack'
Expand Down Expand Up @@ -132,4 +133,5 @@ def worker(workq: multiprocessing.Queue):


if __name__ == "__main__":
start_http_server(9129)
main(worker, KAFKA_TOPIC, KAFKA_GROUP_ID)

0 comments on commit aac3acf

Please sign in to comment.