64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
import logging
|
|
|
|
import requests
|
|
from flask import Flask, jsonify
|
|
from waitress import serve
|
|
|
|
from pyinfra.config import CONFIG
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
|
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
|