国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

使用Python編寫Prometheus監(jiān)控的方法

2020-02-15 23:13:47
字體:
供稿:網(wǎng)友

要使用python編寫Prometheus監(jiān)控,需要你先開啟Prometheus集群。可以參考//www.jb51.net/article/148895.htm 安裝。在python中實現(xiàn)服務(wù)器端。在Prometheus中配置請求網(wǎng)址,Prometheus會定期向該網(wǎng)址發(fā)起申請獲取你想要返回的數(shù)據(jù)。

使用Python和Flask編寫Prometheus監(jiān)控

Installation

pip install flaskpip install prometheus_client

Metrics

Prometheus提供4種類型Metrics:Counter, Gauge, SummaryHistogram

Counter

Counter可以增長,并且在程序重啟的時候會被重設(shè)為0,常被用于任務(wù)個數(shù),總處理時間,錯誤個數(shù)等只增不減的指標。

import prometheus_clientfrom prometheus_client import Counterfrom prometheus_client.core import CollectorRegistryfrom flask import Response, Flaskapp = Flask(__name__)requests_total = Counter("request_count", "Total request cout of the host")@app.route("/metrics")def requests_count():  requests_total.inc()  # requests_total.inc(2)  return Response(prometheus_client.generate_latest(requests_total),          mimetype="text/plain")@app.route('/')def index():  requests_total.inc()  return "Hello World"if __name__ == "__main__":  app.run(host="0.0.0.0")

運行該腳本,訪問youhost:5000/metrics

# HELP request_count Total request cout of the host# TYPE request_count counterrequest_count 3.0

Gauge

Gauge與Counter類似,唯一不同的是Gauge數(shù)值可以減少,常被用于溫度、利用率等指標。

import randomimport prometheus_clientfrom prometheus_client import Gaugefrom flask import Response, Flaskapp = Flask(__name__)random_value = Gauge("random_value", "Random value of the request")@app.route("/metrics")def r_value():  random_value.set(random.randint(0, 10))  return Response(prometheus_client.generate_latest(random_value),          mimetype="text/plain")if __name__ == "__main__":  app.run(host="0.0.0.0")

運行該腳本,訪問youhost:5000/metrics

# HELP random_value Random value of the request# TYPE random_value gaugerandom_value 3.0

Summary/Histogram

Summary/Histogram概念比較復(fù)雜,一般exporter很難用到,暫且不說。

LABELS

使用labels來區(qū)分metric的特征

from prometheus_client import Counterc = Counter('requests_total', 'HTTP requests total', ['method', 'clientip'])c.labels('get', '127.0.0.1').inc()c.labels('post', '192.168.0.1').inc(3)c.labels(method="get", clientip="192.168.0.1").inc()

使用Python和asyncio編寫Prometheus監(jiān)控

from prometheus_client import Counter, Gaugefrom prometheus_client.core import CollectorRegistryREGISTRY = CollectorRegistry(auto_describe=False)requests_total = Counter("request_count", "Total request cout of the host", registry=REGISTRY)random_value = Gauge("random_value", "Random value of the request", registry=REGISTRY)            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 轮台县| 肇源县| 昌吉市| 辉县市| 湖南省| 佛坪县| 安龙县| 丹凤县| 和田市| 化州市| 文水县| 泰州市| 奉新县| 永春县| 璧山县| 静海县| 邻水| 乌海市| 洪洞县| 土默特右旗| 定远县| 武汉市| 札达县| 澄迈县| 常德市| 麻城市| 芜湖县| 洞头县| 宿松县| 乡宁县| 漳浦县| 中宁县| 稻城县| 龙口市| 饶阳县| 铜鼓县| 宁武县| 鄱阳县| 卓资县| 汉川市| 永新县|