-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth_server.py
More file actions
67 lines (52 loc) · 1.99 KB
/
Copy pathhealth_server.py
File metadata and controls
67 lines (52 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Optional ASGI app for liveness, readiness, and Prometheus text metrics (pip install [api]).
Run: uvicorn singapore_eda.health_server:app --host 0.0.0.0 --port 8080
"""
from __future__ import annotations
from typing import Any
from singapore_eda.gov_http import get_metrics
from singapore_eda.health import run_health
try:
from fastapi import FastAPI, Response
from fastapi.responses import JSONResponse, PlainTextResponse
except ImportError: # pragma: no cover - optional extra
FastAPI = None # type: ignore[assignment, misc]
app = None # type: ignore[assignment, misc]
else:
app = FastAPI(
title="singapore-eda-ops",
description="Liveness, readiness, and in-process HTTP metrics for open-data fetches",
version="0.1.0",
)
@app.get("/health", tags=["ops"])
def health() -> dict[str, Any]:
"""Liveness: process up."""
return {"status": "live"}
@app.get("/ready", tags=["ops"])
def ready() -> JSONResponse:
"""Readiness: deps + optional CKAN smoke check."""
h = run_health()
code = 200 if h.status in ("ok", "degraded") else 503
return JSONResponse(h.to_dict(), status_code=code)
@app.get("/ops", tags=["ops"])
def ops() -> JSONResponse:
h = run_health()
m = get_metrics().as_dict()
return JSONResponse({"health": h.to_dict(), "http_client": m})
@app.get("/metrics", tags=["ops"])
def metrics() -> Response:
body = get_metrics().prometheus_text()
return PlainTextResponse(
content=body, media_type="text/plain; version=0.0.4; charset=utf-8"
)
def main() -> None:
import sys
if FastAPI is None:
print(
"Install the api extra: pip install 'singapore-eda[api]' (or fastapi+uvicorn).",
file=sys.stderr,
)
raise SystemExit(1)
import uvicorn
uvicorn.run("singapore_eda.health_server:app", host="0.0.0.0", port=8080, reload=False)
if __name__ == "__main__":
main()