這篇文章將為大家詳細講解有關使用Python怎么對Prometheus進行監(jiān)控,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
在凌云等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供網(wǎng)站制作、成都網(wǎng)站建設 網(wǎng)站設計制作定制網(wǎng)站,公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,品牌網(wǎng)站制作,成都全網(wǎng)營銷,外貿網(wǎng)站建設,凌云網(wǎng)站建設費用合理。使用Python和Flask編寫Prometheus監(jiān)控
Installation
pip install flask pip install prometheus_client
Metrics
Prometheus提供4種類型Metrics:Counter
, Gauge
, Summary
和Histogram
Counter
Counter可以增長,并且在程序重啟的時候會被重設為0,常被用于任務個數(shù),總處理時間,錯誤個數(shù)等只增不減的指標。
import prometheus_client from prometheus_client import Counter from prometheus_client.core import CollectorRegistry from flask import Response, Flask app = 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 counter request_count 3.0
Gauge
Gauge與Counter類似,唯一不同的是Gauge數(shù)值可以減少,常被用于溫度、利用率等指標。
import random import prometheus_client from prometheus_client import Gauge from flask import Response, Flask app = 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 gauge random_value 3.0
Summary/Histogram
Summary/Histogram概念比較復雜,一般exporter很難用到,暫且不說。
LABELS
使用labels來區(qū)分metric的特征
from prometheus_client import Counter c = 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, Gauge from prometheus_client.core import CollectorRegistry REGISTRY = 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)
import prometheus_client from prometheus_client import Counter,Gauge from prometheus_client.core import CollectorRegistry from aiohttp import web import aiohttp import asyncio import uvloop import random,logging,time,datetime asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) routes = web.RouteTableDef() # metrics包含 requests_total = Counter("request_count", "Total request cout of the host") # 數(shù)值只增 random_value = Gauge("random_value", "Random value of the request") # 數(shù)值可大可小 @routes.get('/metrics') async def metrics(request): requests_total.inc() # 計數(shù)器自增 # requests_total.inc(2) data = prometheus_client.generate_latest(requests_total) return web.Response(body = data,content_type="text/plain") # 將計數(shù)器的值返回 @routes.get("/metrics2") async def metrics2(request): random_value.set(random.randint(0, 10)) # 設置值任意值,但是一定要為 整數(shù)或者浮點數(shù) return web.Response(body = prometheus_client.generate_latest(random_value),content_type="text/plain") # 將值返回 @routes.get('/') async def hello(request): return web.Response(text="Hello, world") # 使用labels來區(qū)分metric的特征 c = Counter('requests_total', 'HTTP requests total', ['method', 'clientip']) # 添加lable的key, c.labels('get', '127.0.0.1').inc() #為不同的label進行統(tǒng)計 c.labels('post', '192.168.0.1').inc(3) #為不同的label進行統(tǒng)計 c.labels(method="get", clientip="192.168.0.1").inc() #為不同的label進行統(tǒng)計 g = Gauge('my_inprogress_requests', 'Description of gauge',['mylabelname']) g.labels(mylabelname='str').set(3.6) #value自己定義,但是一定要為 整數(shù)或者浮點數(shù) if __name__ == '__main__': logging.info('server start:%s'% datetime.datetime.now()) app = web.Application(client_max_size=int(2)*1024**2) # 創(chuàng)建app,設置大接收圖片大小為2M app.add_routes(routes) # 添加路由映射 web.run_app(app,host='0.0.0.0',port=2222) # 啟動app logging.info('server close:%s'% datetime.datetime.now())
關于使用Python怎么對Prometheus進行監(jiān)控就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。