Add Prometheus scraping endpoint
Available on /metrics endpoint.
Added when app is run via uwsgi:
uwsgi --http 127.0.0.1:5000 \
--wsgi-file sf_notifier/server.py \
--callable app_dispatch
Change-Id: I3aa8f715e81f6032d6c14e1b764324ea05473e45
Related-bug: PROD-25099 (PROD:25099)
diff --git a/sf_notifier/salesforce/client.py b/sf_notifier/salesforce/client.py
index 319488d..79a6467 100644
--- a/sf_notifier/salesforce/client.py
+++ b/sf_notifier/salesforce/client.py
@@ -18,6 +18,8 @@
import os
import uuid
+from prometheus_client import Counter, Gauge
+
import requests
from simple_salesforce import Salesforce
@@ -66,6 +68,11 @@
class SalesforceClient(object):
def __init__(self, config):
+ self.metrics = {
+ 'sf_auth_ok': Gauge('sf_auth_ok', 'sf-notifier'),
+ 'sf_error_count': Counter('sf_error_count', 'sf-notifier'),
+ 'sf_request_count': Counter('sf_request_count', 'sf-notifier')
+ }
self.session = requests.Session()
self.config = self._validate_config(config)
self.environment = self.config.pop('environment_id')
@@ -103,8 +110,10 @@
self.sf = Salesforce(**kwargs)
except sf_exceptions.SalesforceAuthenticationFailed:
logger.error('Salesforce authentication failure.')
+ self.metrics['sf_auth_ok'].set(0)
return
logger.info('Salesforce authentication successful.')
+ self.metrics['sf_auth_ok'].set(1)
@staticmethod
def _get_alert_id(labels):
@@ -117,7 +126,7 @@
def _create_case(self, subject, body, labels, alert_id):
if alert_id in self._registered_alerts:
- logger.info('Duplicate case for alert: {}.'.format(alert_id))
+ logger.warning('Duplicate case for alert: {}.'.format(alert_id))
return 1, self._registered_alerts[alert_id]['Id']
severity = labels.get('severity', 'unknown').upper()
@@ -133,9 +142,11 @@
'Environment2__c': self.environment,
'Alert_ID__c': alert_id,
}
- logger.info('Try to create case: {}'.format(payload))
+ logger.info('Try to create case: {}.'.format(payload))
try:
+ self.metrics['sf_request_count'].inc()
case = self.sf.Case.create(payload)
+ logger.info('Created case: {}.'.format(case))
except sf_exceptions.SalesforceMalformedRequest as ex:
msg = ex.content[0]['message']
err_code = ex.content[0]['errorCode']
@@ -146,22 +157,16 @@
self._registered_alerts[alert_id] = {'Id': case_id}
return 1, case_id
else:
+ self.metrics['sf_error_count'].inc()
raise
self._registered_alerts[alert_id] = {'Id': case['id']}
return 0, case['id']
@sf_auth_retry
- def _get_case(self, case_id):
- return self.sf.Case.get(case_id)
-
- @sf_auth_retry
- def _update_case(self, case_id, data):
- return self.sf.Case.update(case_id, data)
-
- @sf_auth_retry
def _close_case(self, case_id):
logger.info('Try to close case: {}.'.format(case_id))
+ self.metrics['sf_request_count'].inc()
update = self.sf.Case.update(
case_id,
{'Status': 'Auto-solved', 'Alert_ID__c': uuid.uuid4().hex}