Merge in RR/pyinfra from fixes to master
Squashed commit of the following:
commit e3eff12ccdea52e041cc7a14cda72d3e32aa2144
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 22 15:42:35 2022 +0100
black
commit 2bc520c849ea4e833cb60b2c97626da6636d3155
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 22 15:42:08 2022 +0100
adjust mock script
commit 429b6b8f3a3fc8aa35515395712057d1c7bec13e
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 22 15:41:42 2022 +0100
change scope for retry consume
commit 7488394e313270fe7ba356c40d810e7cb3c706ee
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 22 15:39:39 2022 +0100
add heartbeat to AMQP connection
commit 004c5fa805bfb982f55de533bc109fa21bacfbc8
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 22 15:38:15 2022 +0100
Adjust error handling for missing prometheus endpoint: error is logged not raised
65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
import logging
|
|
|
|
import requests
|
|
from flask import Flask, jsonify
|
|
from waitress import serve
|
|
|
|
from pyinfra.config import CONFIG
|
|
|
|
logger = logging.getLogger(__file__)
|
|
logger.setLevel(CONFIG.service.logging_level)
|
|
|
|
|
|
def run_probing_webserver(app, host=None, port=None, mode=None):
|
|
if not host:
|
|
host = CONFIG.probing_webserver.host
|
|
|
|
if not port:
|
|
port = CONFIG.probing_webserver.port
|
|
|
|
if not mode:
|
|
mode = CONFIG.probing_webserver.mode
|
|
|
|
if mode == "development":
|
|
app.run(host=host, port=port, debug=True)
|
|
|
|
elif mode == "production":
|
|
serve(app, host=host, port=port)
|
|
|
|
|
|
def set_up_probing_webserver():
|
|
# TODO: implement meaningful checks
|
|
app = Flask(__name__)
|
|
informed_about_missing_prometheus_endpoint = False
|
|
|
|
@app.route("/ready", methods=["GET"])
|
|
def ready():
|
|
resp = jsonify("OK")
|
|
resp.status_code = 200
|
|
return resp
|
|
|
|
@app.route("/health", methods=["GET"])
|
|
def healthy():
|
|
resp = jsonify("OK")
|
|
resp.status_code = 200
|
|
return resp
|
|
|
|
@app.route("/prometheus", methods=["GET"])
|
|
def get_metrics_from_analysis_endpoint():
|
|
nonlocal informed_about_missing_prometheus_endpoint
|
|
try:
|
|
resp = requests.get(f"{CONFIG.rabbitmq.callback.analysis_endpoint}/prometheus")
|
|
resp.raise_for_status()
|
|
except ConnectionError:
|
|
return ""
|
|
except requests.exceptions.HTTPError as err:
|
|
if resp.status_code == 404:
|
|
if not informed_about_missing_prometheus_endpoint:
|
|
logger.warning(f"Got no metrics from analysis prometheus endpoint: {err}")
|
|
informed_about_missing_prometheus_endpoint = True
|
|
else:
|
|
logging.warning(f"Caught {err}")
|
|
return resp.text
|
|
|
|
return app
|