-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
63 lines (50 loc) · 1.78 KB
/
Copy pathapp.py
File metadata and controls
63 lines (50 loc) · 1.78 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
"""
cloud-devops-lab — health and metrics service
Exposes three endpoints:
/health — liveness check (is the process running?)
/ready — readiness check (is the service ready to serve traffic?)
/metrics — Prometheus scrape endpoint (request count, uptime)
This is a sanitized lab service. It does not contain trading logic,
exchange credentials, employer code, or production data.
"""
import time
from fastapi import FastAPI, Response
from prometheus_client import (
Counter,
Gauge,
generate_latest,
CONTENT_TYPE_LATEST,
)
app = FastAPI(title="cloud-devops-lab health service")
# Track when the application started
START_TIME = time.time()
# Prometheus metrics
REQUEST_COUNT = Counter(
"app_requests_total",
"Total number of requests received",
["method", "endpoint", "status"],
)
UPTIME_GAUGE = Gauge(
"app_uptime_seconds",
"Number of seconds the application has been running",
)
@app.get("/health")
def health():
"""Liveness check — confirms the process is running."""
REQUEST_COUNT.labels(method="GET", endpoint="/health", status="200").inc()
return {"status": "healthy", "service": "cloud-devops-lab"}
@app.get("/ready")
def ready():
"""Readiness check — confirms the service is ready to serve traffic."""
REQUEST_COUNT.labels(method="GET", endpoint="/ready", status="200").inc()
return {"status": "ready", "service": "cloud-devops-lab"}
@app.get("/metrics")
def metrics():
"""Prometheus scrape endpoint — exposes request count and uptime."""
# Update uptime gauge on every scrape
UPTIME_GAUGE.set(time.time() - START_TIME)
REQUEST_COUNT.labels(method="GET", endpoint="/metrics", status="200").inc()
return Response(
content=generate_latest(),
media_type=CONTENT_TYPE_LATEST,
)