-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhello-world.py
73 lines (64 loc) · 2.8 KB
/
hello-world.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
#!flask/bin/python
import os
import time
from threading import Thread
import flask
import prometheus_client
import psutil
from flask import Flask
from prometheus_client import generate_latest
from prometheus_flask_exporter import PrometheusMetrics
app = Flask(__name__)
PrometheusMetrics(app)
UPDATE_PERIOD = 5
SYSTEM_USAGE = prometheus_client.Gauge('system_usage',
'Hold current system resource usage',
['resource_type'])
PROCESS_USAGE = prometheus_client.Gauge('process_usage',
'Hold current process resource usage',
['resource_type'])
DEVICE_COUNTER = prometheus_client.Counter('devices_created', 'Simulates a counter to count created devices')
# Hello world endpoint
@app.route('/')
def hello():
DEVICE_COUNTER.inc()
return 'Hello world!'
# Verify the status of the microservice
@app.route('/health')
def health():
response = flask.Response('{ "status" : "UP" }')
response.headers["Content-Type"] = "application/json"
return response
@app.route('/prometheus')
def metrics():
response = flask.Response(generate_latest())
response.headers["Content-Type"] = "text/plain"
return response
def system_metrics():
process = psutil.Process(os.getpid())
process.cpu_percent()
while True:
# print('Updating system metrics...')
SYSTEM_USAGE.labels('cpu_usage').set(psutil.cpu_percent())
SYSTEM_USAGE.labels('cpu_count').set(psutil.cpu_count())
SYSTEM_USAGE.labels('memory_total').set(psutil.virtual_memory()[0])
SYSTEM_USAGE.labels('memory_available').set(psutil.virtual_memory()[1])
SYSTEM_USAGE.labels('memory_usage').set(psutil.virtual_memory()[2])
SYSTEM_USAGE.labels('memory_used').set(psutil.virtual_memory()[3])
SYSTEM_USAGE.labels('memory_free').set(psutil.virtual_memory()[4])
PROCESS_USAGE.labels('cpu_usage').set(process.cpu_percent())
if (hasattr(process, "cpu_num")):
PROCESS_USAGE.labels('cpu_count').set(process.cpu_num())
PROCESS_USAGE.labels('thread_count').set(len(process.threads()))
PROCESS_USAGE.labels('memory_rss').set(process.memory_info().rss)
PROCESS_USAGE.labels('memory_vms').set(process.memory_info().vms)
PROCESS_USAGE.labels('memory_uss').set(process.memory_full_info().uss)
if (hasattr(process, "pss")):
PROCESS_USAGE.labels('memory_pss').set(process.memory_full_info().pss)
if (hasattr(process, "swap")):
PROCESS_USAGE.labels('memory_swap').set(process.memory_full_info().swap)
time.sleep(UPDATE_PERIOD)
if __name__ == '__main__':
daemon = Thread(target=system_metrics, daemon=True, name='Background')
daemon.start()
app.run(host='0.0.0.0', port=80)