共计 2442 个字符,预计需要花费 7 分钟才能阅读完成。
背景形容
如图 想要取到 url get 申请的值,应用 prometheus blackbox 无奈获取,所以思考应用 flask 自定义 exporter 获取
-
install 库
pip install prometheus_client flask
from atexit import register
import mimetypes
import requests
from prometheus_client.core import CollectorRegistry
from prometheus_client import Gauge, Counter, Info, Enum, generate_latest, start_http_server
from flask import Response, Flask
meeting_useroom_url = 'http://10.31.140.24:9000/meeting_use_count/2'
json_meeting_count = requests.get(meeting_useroom_url)
dic_meeting_count = json_meeting_count.json()
meeting_totalroom_url = 'http://10.31.140.24:9000/meeting_rooms'
json_totalroom_count = requests.get(meeting_totalroom_url)
dic_totalroom_count = json_totalroom_count.json()
meeting_totalpersion_url = 'http://10.31.140.24:9000/meeting_persions'
json_totalpersion_count = requests.get(meeting_totalpersion_url)
dic_totalpersion_count = json_totalpersion_count.json()
# 获取源数据,数据源能够是任意接口、数据库、文件等
def get_qcloud_data():
data = {'meeting_count': int(dic_meeting_count),
'room_count': int(dic_totalroom_count),
'persion_count': int(dic_totalpersion_count),
'cbs': 2,
'clb': 3
}
return data
# 设置 metrics
# Prometheus 提供 4 种类型 Metrics:Counter, Gauge, Summary 和 Histogram
# Counter 累加器,只有 inc 办法, 定义方法指标名,形容,默认增长值是 1
# Gauge 可任意设置,比方 cpu、内存、磁盘等指标,定义方法,指标名,形容,标签
# Histogram 分桶统计,对每个桶的数据进行统计
# Summary 分位统计,对每个值进行统计
registry = CollectorRegistry(auto_describe=False)
product_meeting_count = Gauge('product_meeting_count', 'product_usage_meeting_count', ['product'], registry=registry)
product_room_count = Gauge('product_room_count', 'product_usage_room_count', ['product'], registry=registry)
product_persion_count = Gauge('product_persion_count', 'product_usage_persion_count', ['product'], registry=registry)
product_cbs = Gauge('product_cbs', 'product_usage_cbs', ['product'], registry=registry)
product_clb = Gauge('product_clb', 'product_usage_clb', ['product'], registry=registry)
app = Flask(__name__)
@app.route("/metrics")
def main():
data = get_qcloud_data()
for key, value in data.items():
if key == 'meeting_count':
product_meeting_count.labels(product=key).set(value)
elif key == 'room_count':
product_room_count.labels(product=key).set(value)
elif key == 'persion_count':
product_persion_count.labels(product=key).set(value)
elif key == 'cbs':
product_cbs.labels(product=key).set(value)
elif key == 'clb':
product_clb.labels(product=key).set(value)
return Response(generate_latest(registry), mimetype="text/plain")
@app.route('/')
def index():
print(dic_meeting_count, dic_totalpersion_count, dic_totalroom_count)
return "welecome to qcloud export"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=9117)
正文完
发表至: prometheus
2022-05-09