-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
290 lines (242 loc) · 8.05 KB
/
metrics.py
File metadata and controls
290 lines (242 loc) · 8.05 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
Metrics and Health Check Module
This module provides:
- /healthz endpoint for health checks
- /metrics endpoint for observability
- In-memory counters for request tracking
"""
import threading
from typing import Dict, Any
from datetime import datetime, timezone
from fastapi import APIRouter, HTTPException, status
from storage import get_storage
from cache import get_cache
from session_middleware import get_session_middleware
from middleware.rate_limiter import get_rate_limiter
from config import settings
from logging_config import get_logger
logger = get_logger(__name__)
router = APIRouter(tags=["Health & Metrics"])
class Metrics:
"""
Thread-safe metrics collector.
Tracks various application metrics including request counts,
cache performance, and error rates.
"""
def __init__(self):
self._lock = threading.RLock()
self._counters: Dict[str, int] = {
"total_requests": 0,
"successful_requests": 0,
"failed_requests": 0,
"weather_requests": 0,
"hourly_requests": 0,
"forecast_requests": 0,
"aqi_requests": 0,
"auth_requests": 0,
"cache_hits": 0,
"cache_misses": 0,
"rate_limit_exceeded": 0,
}
self._start_time = datetime.now(timezone.utc)
def increment(self, counter: str, value: int = 1) -> None:
"""Increment a counter."""
with self._lock:
if counter in self._counters:
self._counters[counter] += value
else:
self._counters[counter] = value
def get(self, counter: str) -> int:
"""Get a counter value."""
with self._lock:
return self._counters.get(counter, 0)
def get_all(self) -> Dict[str, int]:
"""Get all counters."""
with self._lock:
return self._counters.copy()
def reset(self) -> None:
"""Reset all counters."""
with self._lock:
for key in self._counters:
self._counters[key] = 0
def uptime_seconds(self) -> float:
"""Get application uptime in seconds."""
return (datetime.now(timezone.utc) - self._start_time).total_seconds()
# Global metrics instance
_metrics: Metrics = None
def get_metrics() -> Metrics:
"""Get the global metrics instance."""
global _metrics
if _metrics is None:
_metrics = Metrics()
return _metrics
def init_metrics() -> Metrics:
"""Initialize the global metrics instance."""
global _metrics
_metrics = Metrics()
return _metrics
# ==================== HEALTH CHECK ====================
@router.get("/healthz")
async def health_check():
"""
Health check endpoint.
Checks:
- Storage is writable
- Cache is working
- Session middleware is loaded
Returns:
Health status with component checks
"""
checks = {
"status": "healthy",
"timestamp": datetime.now(timezone.utc).isoformat(),
"checks": {}
}
all_healthy = True
# Check storage
try:
storage = get_storage()
storage_healthy = storage.is_writable()
checks["checks"]["storage"] = {
"status": "healthy" if storage_healthy else "unhealthy",
"writable": storage_healthy
}
if not storage_healthy:
all_healthy = False
except Exception as e:
checks["checks"]["storage"] = {
"status": "unhealthy",
"error": str(e)
}
all_healthy = False
# Check cache
try:
cache = get_cache()
# Test cache operations
test_key = "__health_check__"
cache.set(test_key, "test", ttl=10)
cache_value = cache.get(test_key)
cache.delete(test_key)
cache_healthy = cache_value == "test"
checks["checks"]["cache"] = {
"status": "healthy" if cache_healthy else "unhealthy",
"size": cache.size(),
"stats": cache.stats()
}
if not cache_healthy:
all_healthy = False
except Exception as e:
checks["checks"]["cache"] = {
"status": "unhealthy",
"error": str(e)
}
all_healthy = False
# Check session middleware
try:
session_middleware = get_session_middleware()
session_loaded = session_middleware is not None
checks["checks"]["session_middleware"] = {
"status": "healthy" if session_loaded else "not_loaded",
"loaded": session_loaded
}
except Exception as e:
checks["checks"]["session_middleware"] = {
"status": "error",
"error": str(e)
}
# Overall status
if not all_healthy:
checks["status"] = "unhealthy"
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=checks
)
return checks
# ==================== METRICS ====================
@router.get("/metrics")
async def get_metrics_endpoint():
"""
Metrics endpoint.
Exposes counters for:
- Total requests
- Cache hits/misses
- Active sessions
- Rate limit events
- Uptime
Returns:
Application metrics
"""
metrics = get_metrics()
# Get cache stats
try:
cache = get_cache()
cache_stats = cache.stats()
except Exception:
cache_stats = {}
# Get storage stats
try:
storage = get_storage()
storage_stats = storage.get_stats()
active_sessions = storage_stats.get("sessions_count", 0)
except Exception:
storage_stats = {}
active_sessions = 0
# Get rate limiter stats
try:
rate_limiter = get_rate_limiter()
rate_limiter_stats = rate_limiter.stats() if rate_limiter else {}
except Exception:
rate_limiter_stats = {}
return {
"timestamp": datetime.now(timezone.utc).isoformat(),
"uptime_seconds": round(metrics.uptime_seconds(), 2),
"counters": metrics.get_all(),
"cache": cache_stats,
"storage": storage_stats,
"rate_limiter": rate_limiter_stats,
"active_sessions": active_sessions,
"app_info": {
"name": settings.APP_NAME,
"version": settings.APP_VERSION,
"environment": "development" if settings.DEBUG else "production"
}
}
# ==================== PROMETHEUS FORMAT (Optional) ====================
@router.get("/metrics/prometheus")
async def get_prometheus_metrics():
"""
Metrics in Prometheus format.
Returns metrics in Prometheus text exposition format.
"""
metrics = get_metrics()
counters = metrics.get_all()
lines = [
"# HELP intelliweather_requests_total Total number of requests",
"# TYPE intelliweather_requests_total counter",
f"intelliweather_requests_total {counters.get('total_requests', 0)}",
"",
"# HELP intelliweather_cache_hits_total Total cache hits",
"# TYPE intelliweather_cache_hits_total counter",
f"intelliweather_cache_hits_total {counters.get('cache_hits', 0)}",
"",
"# HELP intelliweather_cache_misses_total Total cache misses",
"# TYPE intelliweather_cache_misses_total counter",
f"intelliweather_cache_misses_total {counters.get('cache_misses', 0)}",
"",
"# HELP intelliweather_uptime_seconds Application uptime in seconds",
"# TYPE intelliweather_uptime_seconds gauge",
f"intelliweather_uptime_seconds {round(metrics.uptime_seconds(), 2)}",
]
# Get active sessions
try:
storage = get_storage()
active_sessions = storage.count_active_sessions()
lines.extend([
"",
"# HELP intelliweather_active_sessions Number of active sessions",
"# TYPE intelliweather_active_sessions gauge",
f"intelliweather_active_sessions {active_sessions}",
])
except Exception:
pass
return "\n".join(lines) + "\n"