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

詳解Python prometheus_client使用方式

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

背景說明

服務(wù)部署在阿里云的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結(jié)果
    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)計結(jié)果
    	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返回結(jié)果實例

Metrics返回結(jié)果截圖

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

相關(guān)文章

  • Python django中如何使用restful框架

    Python django中如何使用restful框架

    今天給大家?guī)淼氖顷P(guān)于Python框架的相關(guān)知識,文章圍繞著django中restful框架的使用展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 關(guān)于Python 3中print函數(shù)的換行詳解

    關(guān)于Python 3中print函數(shù)的換行詳解

    最近在學習python3,發(fā)現(xiàn)了一個問題想著總結(jié)出來,所以下面這篇文章主要給大家介紹了關(guān)于Python 3中print函數(shù)換行的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對需要的朋友們具有一定的參考學習價值,感興趣的朋友們下面隨著小編來一起學習學習吧。
    2017-08-08
  • python實現(xiàn)基于SVM手寫數(shù)字識別功能

    python實現(xiàn)基于SVM手寫數(shù)字識別功能

    這篇文章主要為大家詳細介紹了python實現(xiàn)基于SVM手寫數(shù)字識別功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python Opencv中基礎(chǔ)的知識點

    Python Opencv中基礎(chǔ)的知識點

    這篇文章主要介紹了Python Opencv中基礎(chǔ)的知識點,主要包括創(chuàng)建窗口、保存圖片、采集視頻、鼠標控制的代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • 五個Jupyter?Notebook實用魔法命令分享

    五個Jupyter?Notebook實用魔法命令分享

    Jupyter?Notebook是一個開源的交互式編程環(huán)境,用于創(chuàng)建和共享包含實時代碼、文本等,本文主要來和大家分享一些有趣的Jupyter?Notebook魔法命令,需要的可以參考一下
    2023-07-07
  • Python2和3字符編碼的區(qū)別知識點整理

    Python2和3字符編碼的區(qū)別知識點整理

    在本篇文章中小編給各位分享的是關(guān)于Python2和3字符編碼的區(qū)別知識點,有需要的朋友們可以學習下。
    2019-08-08
  • Python 多進程、多線程效率對比

    Python 多進程、多線程效率對比

    這篇文章主要介紹了Python 多進程、多線程的效率對比,幫助大家選擇適合的技術(shù),感興趣的朋友可以了解下
    2020-11-11
  • python破解WiFi教程代碼,Python蹭網(wǎng)原理講解

    python破解WiFi教程代碼,Python蹭網(wǎng)原理講解

    用Python生成一個簡單的密碼本,一般是有數(shù)字、字母和符號組成,這里用到的思路主要是窮舉法。通過使用pywifi?模塊,根據(jù)密碼本暴力破解WiFi。本文只是從技術(shù)的角度來闡述學習Pywifi庫!并不建議大家做任何破壞性的操作和任何不當?shù)男袨椋?/div> 2023-01-01
  • scrapy處理python爬蟲調(diào)度詳解

    scrapy處理python爬蟲調(diào)度詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于scrapy處理python爬蟲調(diào)度的相關(guān)內(nèi)容,有興趣的朋友們學習下。
    2020-11-11
  • Python腳本導出為exe程序的方法

    Python腳本導出為exe程序的方法

    這篇文章主要介紹了如何把Python腳本導出為exe程序的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03

最新評論