Add FastAPI service, Dockerfile and compose

This commit is contained in:
rezayaghobi-dev
2026-10-03 19:08:48 +03:30
commit a1973b97ff
10 changed files with 119 additions and 0 deletions

44
webapp/app/main.py Normal file
View File

@@ -0,0 +1,44 @@
import time
from fastapi import FastAPI, HTTPException, Request, Response
from prometheus_client import CONTENT_TYPE_LATEST, Counter, Histogram, generate_latest
app = FastAPI(title="DevOps Test Service")
REQUESTS = Counter(
"http_requests_total", "Total HTTP requests", ["method", "path", "status"]
)
LATENCY = Histogram(
"http_request_duration_seconds", "Request latency in seconds", ["path"]
)
@app.middleware("http")
async def track_metrics(request: Request, call_next):
start = time.perf_counter()
response = await call_next(request)
path = request.url.path
REQUESTS.labels(request.method, path, response.status_code).inc()
LATENCY.labels(path).observe(time.perf_counter() - start)
return response
@app.get("/")
def index():
return {"message": "Hello from the DevOps test service"}
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/error")
def error():
# Used to generate 5xx responses and trigger the high-error-rate alert
raise HTTPException(status_code=500, detail="simulated failure")
@app.get("/metrics")
def metrics():
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)