參考:
Prometheus Python Client https://github.com/prometheus/client_python
https://www.zybuluo.com/rickyChen/note/831219
INSTALL
pip install prometheus_client
DEMO
from prometheus_client import start_http_server, Summary
import random
import time
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
"""A dummy function that takes some time."""
time.sleep(t)
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8000)
# Generate some requests.
while True:
process_request(random.random())
運行程序拂檩,然后訪問 http://localhost:8000/ 可以查看 metrics.
CONFIG Prometheus
在配置文件添加:
- job_name: python_test
static_configs:
- targets: ['localhost:8000']
可以看到已經(jīng)被監(jiān)控起來了
可以查詢看一下結(jié)果: