最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

Installation

pip install flask
pip install prometheus_client

Metrics

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

Counter

Counter可以增長(zhǎng),并且在程序重啟的時(shí)候會(huì)被重設(shè)為0,常被用于任務(wù)個(gè)數(shù),總處理時(shí)間,錯(cuò)誤個(gè)數(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")

運(yùn)行該腳本,訪問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")

運(yùn)行該腳本,訪問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()   # 計(jì)數(shù)器自增
  # requests_total.inc(2)
  data = prometheus_client.generate_latest(requests_total)
  return web.Response(body = data,content_type="text/plain")  # 將計(jì)數(shù)器的值返回
@routes.get("/metrics2")
async def metrics2(request):
  random_value.set(random.randint(0, 10))  # 設(shè)置值任意值,但是一定要為 整數(shù)或者浮點(diǎn)數(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進(jìn)行統(tǒng)計(jì)
c.labels('post', '192.168.0.1').inc(3)   #為不同的label進(jìn)行統(tǒng)計(jì)
c.labels(method="get", clientip="192.168.0.1").inc()  #為不同的label進(jìn)行統(tǒng)計(jì)
g = Gauge('my_inprogress_requests', 'Description of gauge',['mylabelname'])
g.labels(mylabelname='str').set(3.6)  #value自己定義,但是一定要為 整數(shù)或者浮點(diǎn)數(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è)置最大接收?qǐng)D片大小為2M
  app.add_routes(routes)   # 添加路由映射
  web.run_app(app,host='0.0.0.0',port=2222)  # 啟動(dòng)app
  logging.info('server close:%s'% datetime.datetime.now())


總結(jié)

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

相關(guān)文章

  • Tensorflow中TFRecord生成與讀取的實(shí)現(xiàn)

    Tensorflow中TFRecord生成與讀取的實(shí)現(xiàn)

    TFRecord格式的文件存儲(chǔ)形式會(huì)很合理的幫我們存儲(chǔ)數(shù)據(jù),本文主要介紹了Tensorflow中TFRecord生成與讀取的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • python本地文件服務(wù)器實(shí)例教程

    python本地文件服務(wù)器實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于python本地文件服務(wù)器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Python?pyecharts?數(shù)據(jù)可視化模塊的配置方法

    Python?pyecharts?數(shù)據(jù)可視化模塊的配置方法

    Echarts 是一個(gè)由百度開源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設(shè)計(jì),得到了眾多開發(fā)者的認(rèn)可,這篇文章主要介紹了Python?pyecharts?數(shù)據(jù)可視化模塊,需要的朋友可以參考下
    2022-09-09
  • pycharm?console?打印中文為亂碼問題及解決

    pycharm?console?打印中文為亂碼問題及解決

    這篇文章主要介紹了pycharm?console?打印中文為亂碼問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5菜單和工具欄功能作用

    Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5菜單和工具欄功能作用

    本文詳細(xì)解讀通過 QtDesigner 創(chuàng)建主窗口、菜單欄和工具欄,并以菜單項(xiàng) “退出” 為例關(guān)聯(lián)系統(tǒng)定義的動(dòng)作處理方法。有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • Python3.8官網(wǎng)文檔之類的基礎(chǔ)語法閱讀

    Python3.8官網(wǎng)文檔之類的基礎(chǔ)語法閱讀

    類提供了一種組合數(shù)據(jù)和功能的方法,今天通過本文給大家分享Python3.8官網(wǎng)文檔之類的基礎(chǔ)語法閱讀知識(shí),感興趣的朋友跟隨小編一起看看吧
    2021-09-09
  • python 實(shí)現(xiàn)一個(gè)貼吧圖片爬蟲的示例

    python 實(shí)現(xiàn)一個(gè)貼吧圖片爬蟲的示例

    下面小編就為大家?guī)硪黄猵ython 實(shí)現(xiàn)一個(gè)貼吧圖片爬蟲的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • 屏蔽Django admin界面添加按鈕的操作

    屏蔽Django admin界面添加按鈕的操作

    這篇文章主要介紹了屏蔽Django admin界面添加按鈕的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python正則爬取某段子網(wǎng)站前20頁(yè)段子(request庫(kù))過程解析

    python正則爬取某段子網(wǎng)站前20頁(yè)段子(request庫(kù))過程解析

    這篇文章主要介紹了python正則爬取某段子網(wǎng)站前20頁(yè)段子(request庫(kù))過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python中相見恨晚的技巧(記得收藏)

    Python中相見恨晚的技巧(記得收藏)

    這篇文章主要介紹了一些Python中相見恨晚的使用技巧,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04

最新評(píng)論

大悟县| 慈溪市| 措美县| 元氏县| 昌图县| 榕江县| 沙田区| 陆河县| 深水埗区| 仪征市| 莱芜市| 潼南县| 南郑县| 杂多县| 德化县| 柘荣县| 缙云县| 新干县| 嘉荫县| 琼海市| 通许县| 曲靖市| 峨眉山市| 禄丰县| 清丰县| 鄱阳县| 兴城市| 乌恰县| 哈密市| 涞水县| 仁寿县| 博客| 思南县| 修水县| 酒泉市| 遂溪县| 土默特左旗| 屯门区| 寻乌县| 阿坝县| 安溪县|