Ghaymah SRE exam submission - Mohamed Adel
هذا الالتزام موجود في:
65
q1-deploy-monitor/app.py
Normal file
65
q1-deploy-monitor/app.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""
|
||||
Simple demo API for the Ghaymah SRE exam (Q1).
|
||||
|
||||
Endpoints:
|
||||
GET / -> basic welcome message
|
||||
GET /health -> liveness/readiness probe used by Ghaymah + monitoring script
|
||||
GET /metrics -> lightweight JSON metrics used by the dashboard
|
||||
(uptime, request count, avg response time)
|
||||
"""
|
||||
|
||||
import time
|
||||
from flask import Flask, jsonify
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
START_TIME = time.time()
|
||||
REQUEST_COUNT = 0
|
||||
TOTAL_RESPONSE_TIME = 0.0
|
||||
|
||||
|
||||
@app.before_request
|
||||
def _start_timer():
|
||||
global REQUEST_COUNT
|
||||
REQUEST_COUNT += 1
|
||||
app.config["_req_start"] = time.time()
|
||||
|
||||
|
||||
@app.after_request
|
||||
def _record_timing(response):
|
||||
global TOTAL_RESPONSE_TIME
|
||||
elapsed = time.time() - app.config.get("_req_start", time.time())
|
||||
TOTAL_RESPONSE_TIME += elapsed
|
||||
response.headers["X-Response-Time-ms"] = f"{elapsed * 1000:.2f}"
|
||||
return response
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return jsonify(message="Ghaymah SRE exam demo API is running", status="ok")
|
||||
|
||||
|
||||
@app.route("/health")
|
||||
def health():
|
||||
"""Used by: Ghaymah container platform health checks, and the
|
||||
external monitoring script (health-check.sh)."""
|
||||
return jsonify(status="healthy", uptime_seconds=round(time.time() - START_TIME, 2)), 200
|
||||
|
||||
|
||||
@app.route("/metrics")
|
||||
def metrics():
|
||||
"""Used by the dashboard.html to render status / avg response time / request count."""
|
||||
avg_response_ms = (
|
||||
(TOTAL_RESPONSE_TIME / REQUEST_COUNT) * 1000 if REQUEST_COUNT else 0
|
||||
)
|
||||
return jsonify(
|
||||
status="healthy",
|
||||
uptime_seconds=round(time.time() - START_TIME, 2),
|
||||
request_count=REQUEST_COUNT,
|
||||
avg_response_time_ms=round(avg_response_ms, 2),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# For local testing only; production uses gunicorn (see Dockerfile CMD)
|
||||
app.run(host="0.0.0.0", port=8080)
|
||||
المرجع في مشكلة جديدة
حظر مستخدم