forked from zilliztech/akcio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moniter.py
77 lines (65 loc) · 3.52 KB
/
moniter.py
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
import os
import time
from starlette.middleware.base import BaseHTTPMiddleware
from prometheus_client import Counter, Histogram, Summary
# Define metrics
latencies = {}
latencies['all'] = []
requests_total = Counter('requests_total', 'Cumulative requests', ['name'])
requests_success_total = Counter('requests_success_total', 'Cumulative successful requests', ['name'])
requests_failed_total = Counter('requests_failed_total', 'Cumulative failed requests', ['name'])
endpoint_requests_total = Counter('endpoint_requests_total', 'Cumulative requests of each endpoint', ['name', 'endpoint'])
endpoint_requests_success_total = Counter('endpoint_requests_success_total', 'Cumulative successful requests of each endpoint', ['name', 'endpoint'])
endpoint_requests_failed_total = Counter('endpoint_requests_failed_total', 'Cumulative failed requests of each endpoint', ['name', 'endpoint'])
latency_seconds_histogram = Histogram('latency_seconds_histogram', 'Request process latency histogram', ['name'])
endpoint_latency_seconds_histogram = Histogram(
'endpoint_latency_seconds_histogram', 'Request process latency histogram of each endpoint', ['name', 'endpoint']
)
latency_seconds_summary = Summary('latency_seconds_summary', 'Request process latency summary', ['name'])
endpoint_latency_seconds_summary = Summary(
'endpoint_latency_seconds_summary', 'Request process latency summary of each endpoint', ['name', 'endpoint']
)
def enable_moniter(app, max_observation, name):
# Define middleware to collect metrics
class RequestMetricsMiddleware(BaseHTTPMiddleware):
"""
Middleware to process requests.
"""
async def dispatch(self, request, call_next):
path = request.scope.get('path')
is_req = path != '/metrics'
if not is_req:
try:
response = await call_next(request)
return response
except Exception as e:
raise e
begin = time.time()
requests_total.labels(name).inc()
endpoint_requests_total.labels(name, path).inc()
try:
response = await call_next(request)
if response.status_code / 100 < 4:
requests_success_total.labels(name).inc()
endpoint_requests_success_total.labels(name, path).inc()
end = time.time()
if path in latencies:
latencies[path].append(end - begin)
latencies[path] = latencies[path][-max_observation:]
else:
latencies[path] = [end - begin]
latencies['all'].append(end - begin)
latencies['all'] = latencies['all'][-max_observation:]
latency_seconds_histogram.labels(name).observe(end - begin)
endpoint_latency_seconds_histogram.labels(name, path).observe(end - begin)
latency_seconds_summary.labels(name).observe(end - begin)
endpoint_latency_seconds_summary.labels(name, path).observe(end - begin)
return response
else:
requests_failed_total.labels(name).inc()
endpoint_requests_failed_total.labels(name, path).inc()
except Exception as e:
requests_failed_total.labels(name).inc()
endpoint_requests_failed_total.labels(name, path).inc()
raise e
app.add_middleware(RequestMetricsMiddleware)