欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年10月15日 16:15:59   作者:數(shù)據(jù)架構(gòu)師  
今天小編就為大家分享一篇關(guān)于使用Python編寫Prometheus監(jiān)控的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

要使用python編寫Prometheus監(jiān)控,需要你先開啟Prometheus集群??梢詤⒖?a href="http://www.dbjr.com.cn/article/148895.htm">//www.dbjr.com.cn/article/148895.htm 安裝。在python中實現(xiàn)服務(wù)器端。在Prometheus中配置請求網(wǎng)址,Prometheus會定期向該網(wǎng)址發(fā)起申請獲取你想要返回的數(shù)據(jù)。

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

Installation

pip install flask
pip install prometheus_client

Metrics

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

Counter

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

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ù)值可以減少,常被用于溫度、利用率等指標(biāo)。

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概念比較復(fù)雜,一般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ù)或者浮點數(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,設(shè)置最大接收圖片大小為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())


總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • python離線安裝外部依賴包的實現(xiàn)

    python離線安裝外部依賴包的實現(xiàn)

    今天小編就為大家分享一篇python離線安裝外部依賴包的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python使用matplotlib繪制隨機漫步圖

    Python使用matplotlib繪制隨機漫步圖

    這篇文章主要為大家詳細介紹了使用Python生成隨機漫步數(shù)據(jù),使用matplotlib繪制隨機漫步圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 簡單的Apache+FastCGI+Django配置指南

    簡單的Apache+FastCGI+Django配置指南

    這篇文章主要介紹了簡單的Apache+FastCGI+Django配置指南,這也是Python上最流行的web框架Django的最流行搭配環(huán)境:)需要的朋友可以參考下
    2015-07-07
  • DataFrame中去除指定列為空的行方法

    DataFrame中去除指定列為空的行方法

    下面小編就為大家分享一篇DataFrame中去除指定列為空的行方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python解析yaml文件過程詳解

    python解析yaml文件過程詳解

    這篇文章主要介紹了python解析yaml文件過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實例

    Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實例

    這篇文章主要介紹了Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實例,本文直接給出操作mysql代碼實例,包含創(chuàng)建表、插入數(shù)據(jù)、插入多條數(shù)據(jù)、查詢數(shù)據(jù)等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Python爬蟲使用代理IP的實現(xiàn)

    Python爬蟲使用代理IP的實現(xiàn)

    這篇文章主要介紹了Python爬蟲使用代理IP的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python實現(xiàn)報表自動化詳解

    python實現(xiàn)報表自動化詳解

    這篇文章主要介紹了python實現(xiàn)報表自動化詳解,涉及python讀,寫excel—xlwt常用功能,xlutils 常用功能,xlwt寫Excel時公式的應(yīng)用等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • python的構(gòu)建工具setup.py的方法使用示例

    python的構(gòu)建工具setup.py的方法使用示例

    本篇文章主要介紹了python的構(gòu)建工具setup.py的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • python dict 相同key 合并value的實例

    python dict 相同key 合并value的實例

    今天小編就為大家分享一篇python dict 相同key 合并value的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01

最新評論