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

詳解Python prometheus_client使用方式

 更新時間:2022年02月09日 10:19:47   作者:JoJo93  
本文主要介紹了Python prometheus_client使用方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

背景說明

服務部署在阿里云的K8s上,配置了基于Prometheus的Grafana監(jiān)控。原本用的是自定義的Metrics接口統(tǒng)計,上報一些字段,后面發(fā)現(xiàn)Prometheus自帶的監(jiān)控非常全面好用,適合直接抓取統(tǒng)計,所以做了一些改變。

Python prometheus-client 安裝

pip install prometheus-client

Python封裝

# encoding: utf-8
from prometheus_client import Counter, Gauge, Summary
from prometheus_client.core import CollectorRegistry
from prometheus_client.exposition import choose_encoder


class Monitor:
    def __init__(self):
    # 注冊收集器&最大耗時map
    self.collector_registry = CollectorRegistry(auto_describe=False)
    self.request_time_max_map = {}

    # 接口調(diào)用summary統(tǒng)計
    self.http_request_summary = Summary(name="http_server_requests_seconds",
                                   documentation="Num of request time summary",
                                   labelnames=("method", "code", "uri"),
                                   registry=self.collector_registry)
    # 接口最大耗時統(tǒng)計
    self.http_request_max_cost = Gauge(name="http_server_requests_seconds_max",
                                  documentation="Number of request max cost",
                                  labelnames=("method", "code", "uri"),
                                  registry=self.collector_registry)

    # 請求失敗次數(shù)統(tǒng)計
    self.http_request_fail_count = Counter(name="http_server_requests_error",
                                      documentation="Times of request fail in total",
                                      labelnames=("method", "code", "uri"),
                                      registry=self.collector_registry)

    # 模型預測耗時統(tǒng)計
    self.http_request_predict_cost = Counter(name="http_server_requests_seconds_predict",
                                        documentation="Seconds of prediction cost in total",
                                        labelnames=("method", "code", "uri"),
                                        registry=self.collector_registry)
    # 圖片下載耗時統(tǒng)計
    self.http_request_download_cost = Counter(name="http_server_requests_seconds_download",
                                         documentation="Seconds of download cost in total",
                                         labelnames=("method", "code", "uri"),
                                         registry=self.collector_registry)

    # 獲取/metrics結果
    def get_prometheus_metrics_info(self, handler):
        encoder, content_type = choose_encoder(handler.request.headers.get('accept'))
        handler.set_header("Content-Type", content_type)
        handler.write(encoder(self.collector_registry))
        self.reset_request_time_max_map()

    # summary統(tǒng)計
    def set_prometheus_request_summary(self, handler):
        self.http_request_summary.labels(handler.request.method, handler.get_status(), handler.request.path).observe(handler.request.request_time())
        self.set_prometheus_request_max_cost(handler)

    # 自定義summary統(tǒng)計
    def set_prometheus_request_summary_customize(self, method, status, path, cost_time):
        self.http_request_summary.labels(method, status, path).observe(cost_time)
        self.set_prometheus_request_max_cost_customize(method, status, path, cost_time)

    # 失敗統(tǒng)計
    def set_prometheus_request_fail_count(self, handler, amount=1.0):
        self.http_request_fail_count.labels(handler.request.method, handler.get_status(), handler.request.path).inc(amount)

    # 自定義失敗統(tǒng)計
    def set_prometheus_request_fail_count_customize(self, method, status, path, amount=1.0):
        self.http_request_fail_count.labels(method, status, path).inc(amount)

    # 最大耗時統(tǒng)計
    def set_prometheus_request_max_cost(self, handler):
        requset_cost = handler.request.request_time()
        if self.check_request_time_max_map(handler.request.path, requset_cost):
            self.http_request_max_cost.labels(handler.request.method, handler.get_status(), handler.request.path).set(requset_cost)
            self.request_time_max_map[handler.request.path] = requset_cost

    # 自定義最大耗時統(tǒng)計
    def set_prometheus_request_max_cost_customize(self, method, status, path, cost_time):
        if self.check_request_time_max_map(path, cost_time):
            self.http_request_max_cost.labels(method, status, path).set(cost_time)
            self.request_time_max_map[path] = cost_time

    # 預測耗時統(tǒng)計
    def set_prometheus_request_predict_cost(self, handler, amount=1.0):
        self.http_request_predict_cost.labels(handler.request.method, handler.get_status(), handler.request.path).inc(amount)

    # 自定義預測耗時統(tǒng)計
    def set_prometheus_request_predict_cost_customize(self, method, status, path, cost_time):
        self.http_request_predict_cost.labels(method, status, path).inc(cost_time)

    # 下載耗時統(tǒng)計
    def set_prometheus_request_download_cost(self, handler, amount=1.0):
        self.http_request_download_cost.labels(handler.request.method, handler.get_status(), handler.request.path).inc(amount)

    # 自定義下載耗時統(tǒng)計
    def set_prometheus_request_download_cost_customize(self, method, status, path, cost_time):
        self.http_request_download_cost.labels(method, status, path).inc(cost_time)

    # 校驗是否賦值最大耗時map
    def check_request_time_max_map(self, uri, cost):
        if uri not in self.request_time_max_map:
            return True
        if self.request_time_max_map[uri] < cost:
            return True
        return False

    # 重置最大耗時map
    def reset_request_time_max_map(self):
        for key in self.request_time_max_map:
            self.request_time_max_map[key] = 0.0

調(diào)用

import tornado
import tornado.ioloop
import tornado.web
import tornado.gen
from datetime import datetime
from tools.monitor import Monitor

global g_monitor

class ClassifierHandler(tornado.web.RequestHandler):
    def post(self):
        # TODO Something you need
        # work....
        # 統(tǒng)計Summary,包括請求次數(shù)和每次耗時
        g_monitor.set_prometheus_request_summary(self)
        self.write("OK")


class PingHandler(tornado.web.RequestHandler):
    def head(self):
        print('INFO', datetime.now(), "/ping Head.")
        g_monitor.set_prometheus_request_summary(self)
        self.write("OK")

    def get(self):
        print('INFO', datetime.now(), "/ping Get.")
        g_monitor.set_prometheus_request_summary(self)
        self.write("OK")


class MetricsHandler(tornado.web.RequestHandler):
    def get(self):
        print('INFO', datetime.now(), "/metrics Get.")
		g_monitor.set_prometheus_request_summary(self)
		# 通過Metrics接口返回統(tǒng)計結果
    	g_monitor.get_prometheus_metrics_info(self)
    

def make_app():
    return tornado.web.Application([
        (r"/ping?", PingHandler),
        (r"/metrics?", MetricsHandler),
        (r"/work?", ClassifierHandler)
    ])

if __name__ == "__main__":
    g_monitor = Monitor()
	
	app = make_app()
    app.listen(port)
    tornado.ioloop.IOLoop.current().start()

Metrics返回結果實例

Metrics返回結果截圖

 到此這篇關于詳解Python prometheus_client使用方式的文章就介紹到這了,更多相關Python prometheus_client內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python+Sklearn實現(xiàn)異常檢測

    Python+Sklearn實現(xiàn)異常檢測

    這篇文章主要為大家詳細介紹了Python如何利用Sklearn實現(xiàn)異常檢測,文中的示例代碼講解詳細,對我們學習Python有一定的幫助,感興趣的可以跟隨小編一起學習一下
    2022-12-12
  • py中的目錄與文件判別代碼

    py中的目錄與文件判別代碼

    python中的判別目錄和文件的腳本
    2008-07-07
  • Python連接達夢數(shù)據(jù)庫的實現(xiàn)示例

    Python連接達夢數(shù)據(jù)庫的實現(xiàn)示例

    本文主要介紹了Python連接達夢數(shù)據(jù)庫的實現(xiàn)示例,dmPython是DM提供的依據(jù)Python DB API version 2.0中API使用規(guī)定而開發(fā)的數(shù)據(jù)庫訪問接口,使Python應用程序能夠?qū)M數(shù)據(jù)庫進行訪問
    2023-12-12
  • pycharm 使用心得(三)Hello world!

    pycharm 使用心得(三)Hello world!

    作為PyCharm編輯器的起步,我們理所當然的先寫一個Hello word,并運行它。(此文獻給對IDE不熟悉的初學者)
    2014-06-06
  • python實現(xiàn)數(shù)通設備端口監(jiān)控示例

    python實現(xiàn)數(shù)通設備端口監(jiān)控示例

    這篇文章主要介紹了python實現(xiàn)數(shù)通設備端口監(jiān)控示例,需要的朋友可以參考下
    2014-04-04
  • Python基于當前時間批量創(chuàng)建文件

    Python基于當前時間批量創(chuàng)建文件

    這篇文章主要介紹了Python基于當前時間批量創(chuàng)建文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • 關于使用OpenCsv導入大數(shù)據(jù)量報錯的問題

    關于使用OpenCsv導入大數(shù)據(jù)量報錯的問題

    這篇文章主要介紹了使用OpenCsv導入大數(shù)據(jù)量報錯的問題 ,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • python并發(fā)編程多進程之守護進程原理解析

    python并發(fā)編程多進程之守護進程原理解析

    這篇文章主要介紹了python并發(fā)編程多進程之守護進程原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • 解讀NumPy數(shù)組與Python列表的比較

    解讀NumPy數(shù)組與Python列表的比較

    在Python中處理數(shù)值數(shù)據(jù)時,可以選擇使用Python列表或NumPy數(shù)組,Python列表靈活,可存儲不同類型元素,但在大數(shù)據(jù)處理上可能較慢,NumPy數(shù)組固定類型,內(nèi)存連續(xù)存儲,執(zhí)行數(shù)組操作如加法、乘法等更高效,尤其在大數(shù)據(jù)集處理上具有明顯的性能和內(nèi)存使用優(yōu)勢
    2024-10-10
  • Python語言中的數(shù)據(jù)類型-序列

    Python語言中的數(shù)據(jù)類型-序列

    這篇文章主要介紹了Python語言中的數(shù)據(jù)類型-序列,前面我們提到了Python數(shù)據(jù)類型中的內(nèi)置數(shù)值類型與字符串類型。今天學習一下Python的序列數(shù)據(jù)類型,要知道的是在Python中沒有數(shù)組這一數(shù)據(jù)結構,需要的朋友可以參考一下
    2022-02-02

最新評論

宁陕县| 大丰市| 长岛县| 富顺县| 乌审旗| 施甸县| 漳州市| 松滋市| 威宁| 本溪市| 南昌县| 冕宁县| 桑日县| 保德县| 延寿县| 青神县| 东阿县| 卢氏县| 秦安县| 曲松县| 夏津县| 揭阳市| 天等县| 盐边县| 资溪县| 清丰县| 莱州市| 永和县| 府谷县| 灯塔市| 偏关县| 略阳县| 高安市| 霸州市| 玛纳斯县| 建平县| 河东区| 萨迦县| 荆门市| 洪洞县| 大竹县|