-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (69 loc) · 2.49 KB
/
Copy pathapp.py
File metadata and controls
89 lines (69 loc) · 2.49 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
from flask import Flask, jsonify,request
import psutil,time
from prometheus_client import Histogram,Counter, generate_latest
from prometheus_client import CollectorRegistry
app = Flask(__name__)
# CreateRegistry
registry = CollectorRegistry()
# Create a histogram to record the response times of requests.
REQUEST_LATENCY = Histogram(
'http_request_duration_seconds',
'HTTP Request Latency',
['method', 'endpoint'],
registry=registry
)
####record response status
http_status_counter = Counter(
'http_response_status_total',
'Total number of HTTP responses by status code',
['method', 'endpoint', 'status_code'],
registry=registry
)
@app.before_request
def before_request():
# Record the start time at the beginning of the request
# Initialize the status code before each request
request.start_time = time.time()
@app.after_request
def after_request(response):
# Record the status code
http_status_counter.labels(method=request.method, endpoint=request.path, status_code=response.status_code).inc()
# Calculate the duration of the request
latency = time.time() - request.start_time
# Record the response time in the histogram
REQUEST_LATENCY.labels(method=request.method, endpoint=request.path).observe(latency)
# Return the response
return response
@app.route('/metrics')
def metrics():
# Expose the metrics
return generate_latest(registry)
@app.route('/system_info', methods=['GET'])
def system_info():
# Get CPU usage
cpu_usage = psutil.cpu_percent(interval=1)
# Get disk information
memory_info = psutil.virtual_memory()
# Construct return data
disk_info = psutil.disk_usage('/')
# Start Flask application
info = {
'cpu_usage_percent': cpu_usage,
'memory': {
'total': memory_info.total,
'available': memory_info.available,
'used': memory_info.used,
'percentage': memory_info.percent
},
'disk': {
'total': disk_info.total,
'used': disk_info.used,
'free': disk_info.free,
'percentage': disk_info.percent
}
}
print(info)
return jsonify(info)
if __name__ == '__main__':
# enable Flask flask
app.run(host='0.0.0.0',port=8001, debug=True)