目前prometheus已经成为一个监控方案的标准了(这里有大量知名的软件已经附带了exporter),你有必要给你的应用服务加上metrics输出,以便可以利用prometheus收集你的应用服务的一些量度指标。
python应用服务可以使用:client_python
java应用服务可以使用:client_java
这里以python应用服务增加metrics输出为例子:
1. 安装client_python
pip3 install prometheus_client
2. 输出一个简单process_reques请求的metrics:
这里我们使用flask框架,所以先安装flask:
pip3 isntall flask
准备python 文件: myWebServer.py
"""
使用python内置WSGI server: wsgiref ,考虑性能问题你也可以使用其他WSGI server
WSGI server用了gevent, eventlet等 green thread技术,就可以支持更多并发。
"""
from prometheus_client import start_http_server, Summary, Counter
import random
import time
from flask import Flask, jsonify
from wsgiref.simple_server import make_server
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
Counter_TIME = Counter('My_Counter','My Counter Desc')
app = Flask(__name__)
# Decorate function with metric.
@app.route("/test1")
@REQUEST_TIME.time()
def process_request():
"""A dummy function that takes some time."""
time.sleep(random.random())
Counter_TIME.inc()
return jsonify({"return": "success OK!"})
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8000)
# Generate some requests.
httpd = make_server(
'127.0.0.1', # The host name.
8001, # A port number where to wait for the request.
app # Our application object name, in this case a function.
)
httpd.serve_forever()
3. 运行该python
python3.7 ./myWebServer.py
3.1 在浏览器刷新网址,让metrics的值变动:http://localhost:8001/test1
3.2 查看服务的metrics:http://localhost:8000/metrics
$ curl http://127.0.0.1:8000/metrics
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 411.0
python_gc_objects_collected_total{generation="1"} 10.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 69.0
python_gc_collections_total{generation="1"} 6.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="7",patchlevel="3",version="3.7.3"} 1.0
# HELP request_processing_seconds Time spent processing request
# TYPE request_processing_seconds summary
request_processing_seconds_count 1.0
request_processing_seconds_sum 0.7169587870000003
# TYPE request_processing_seconds_created gauge
request_processing_seconds_created 1.5612925420016e+09
# HELP My_Counter_total My Counter Desc
# TYPE My_Counter_total counter
My_Counter_total 1.0
# TYPE My_Counter_created gauge
My_Counter_created 1.561292542001632e+09
可以看到已经输出系统的一些指标了:
Prometheus的rate功能允许计算每秒的两个请求,以及此数据随时间的延迟。
此外,如果您使用的是Linux,则process指标会免费公开CPU,内存和有关该过程的其他信息!
目前提供四种metrics类型,分别是:Counter, Gauge, Summary and Histogram
详细参考:https://github.com/prometheus/client_python
指标类型: https://prometheus.io/docs/concepts/metric_types/
容器方式启动一个提供prometheus metrics例子:https://github.com/4220182/prometheus-metrics