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/server.py b/sf_notifier/server.py
index 658061d..a42c26a 100644
--- a/sf_notifier/server.py
+++ b/sf_notifier/server.py
@@ -18,6 +18,8 @@
from flask import Flask, Response, jsonify, request
+from prometheus_client import make_wsgi_app
+
from sf_notifier.helpers import alert_fields_and_action
from sf_notifier.salesforce.client import SalesforceClient
@@ -25,19 +27,19 @@
from simple_settings import settings
+from werkzeug.wsgi import DispatcherMiddleware
+
dictConfig(settings.LOGGING)
-app = Flask('__name__')
+app = Flask(__name__)
+app_dispatch = DispatcherMiddleware(app, {
+ '/metrics': make_wsgi_app()
+})
+
sf_cli = SalesforceClient(settings.SF_CONFIG)
-@app.route('/health', methods=['GET'])
-def health():
- app.logger.info('Health: OK!')
- return 'OK!'
-
-
@app.route('/hook', methods=['POST'])
def webhook_receiver():
@@ -66,8 +68,9 @@
if fields:
try:
cases.append(getattr(sf_cli, action)(*fields))
- except SalesforceMalformedRequest:
- msg = 'Salesforce request failure.'
+ except SalesforceMalformedRequest as err:
+ msg = 'Salesforce request failure: {}.'.format(err)
+ sf_cli.metrics['sf_error_count'].inc()
app.logger.error(msg)
return Response(json.dumps({'error': msg}),
status=500,