- further healthz improvements

This commit is contained in:
Josako
2025-09-07 14:45:47 +02:00
parent 362b2fe753
commit 575bfa259e
4 changed files with 35 additions and 28 deletions

View File

@@ -1,4 +1,4 @@
from flask import Blueprint, current_app, request
from flask import Blueprint, current_app, request, jsonify
from flask_healthz import HealthError
from sqlalchemy.exc import SQLAlchemyError
from celery.exceptions import TimeoutError as CeleryTimeoutError
@@ -8,7 +8,7 @@ import time
from common.extensions import db, metrics, minio_client
from common.utils.celery_utils import current_celery
healthz_bp = Blueprint('healthz', __name__, url_prefix='/_healthz')
healthz_bp = Blueprint('healthz', __name__, url_prefix='/healthz')
# Define Prometheus metrics
api_request_counter = Counter('api_request_count', 'API Request Count', ['method', 'endpoint'])
@@ -69,6 +69,22 @@ def check_minio():
return False
@healthz_bp.route('/ready')
def ready():
"""
Health check endpoint for readiness probe
"""
return jsonify({"status": "ok"})
@healthz_bp.route('/live')
def live():
"""
Health check endpoint for liveness probe
"""
return jsonify({"status": "ok"})
@healthz_bp.route('/metrics')
@metrics.do_not_track()
def prometheus_metrics():
@@ -76,7 +92,7 @@ def prometheus_metrics():
# Custom metrics example
@healthz_bp.before_app_request
@healthz_bp.before_request
def before_request():
request.start_time = time.time()
api_request_counter.labels(
@@ -84,17 +100,8 @@ def before_request():
).inc()
@healthz_bp.after_app_request
@healthz_bp.after_request
def after_request(response):
request_duration = time.time() - request.start_time
api_request_latency.observe(request_duration)
return response
def init_healtz(app):
app.config.update(
HEALTHZ={
"live": "healthz_views.liveness",
"ready": "healthz_views.readiness",
}
)