網(wǎng)站首頁 編程語言 正文
背景說明
服務(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) # 模型預(yù)測耗時統(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 # 預(yù)測耗時統(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) # 自定義預(yù)測耗時統(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é)果實例
原文鏈接:https://blog.csdn.net/mywmy/article/details/103109561
相關(guān)推薦
- 2023-09-18 【解決】npm ERR A complete log of this run can be foun
- 2022-07-22 yaml文件的加載使用
- 2022-04-21 Docker - Error: Error response from daemon: No com
- 2022-05-25 Spring Security 中的權(quán)限注解很神奇嗎?
- 2024-03-19 Shell腳本中的條件測試命令簡介
- 2022-06-27 Abp集成HangFire開源.NET任務(wù)調(diào)度框架_實用技巧
- 2022-08-17 C語言超全面覆蓋操作符知識點_C 語言
- 2022-07-09 docker 中進程的信號
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支