用python编写exporter监控接口访问数量+错误率+响应时间
1.编写exporter
import prometheus_client from prometheus_client import Gauge,start_http_server,Counter import pycurl import time import threading from io import BytesIO #创建client_python里提供的prometheus Counter数据类型 url_http_code = Counter("url_http_code", "request http_code of the host",['code','url']) url_http_request_time = Counter("url_http_request_time", "request http_request_time of the host",['le','url']) http_request_total = Counter("http_request_total", "request request total of the host",['url']) #curl url,返回状态码和总共耗时 -- 返回状态和响应时间 def test_website(url): buffer_curl = BytesIO() c = pycurl.Curl() c.setopt(pycurl.URL, url) # c.setopt(pycurl.WRITEDATA, buffer_curl) c.setopt(pycurl.CONNECTTIMEOUT, 3) c.setopt(pycurl.TIMEOUT, 3) try: c.perform() except pycurl.error: http_code = 500 http_total_time = 999 else: http_code = c.getinfo(pycurl.HTTP_CODE) http_total_time = c.getinfo(pycurl.TOTAL_TIME) return http_code, http_total_time #根据curl返回值,统计放到exporter显示的数据 -- 统计各个状态的总数 def count_metric(url): http_code, http_total_time = test_website(url) if http_code >= 100 and http_code < 200 : url_http_code.labels('1xx',url).inc() elif http_code >= 200 and http_code < 300 : url_http_code.labels('2xx',url).inc() elif http_code >= 300 and http_code < 400 : url_http_code.labels('3xx',url).inc() elif http_code >= 400 and http_code < 500 : url_http_code.labels('4xx',url).inc() else: url_http_code.labels('5xx',url).inc() if http_total_time < 1 : url_http_request_time.labels('1',url).inc() elif http_total_time < 2 : url_http_request_time.labels('2',url).inc() elif http_total_time < 3 : url_http_request_time.labels('3',url).inc() else : url_http_request_time.labels('+Inf',url).inc() http_request_total.labels(url).inc() #线程控制,每隔5s执行curl url def count_threads(url): while True: t = threading.Thread(target=count_metric,args=(url,)) t.setDaemon(True) t.start() time.sleep(5) #将每个需要监控的域名起一个进程 if __name__ == '__main__': start_http_server(9091) server_list = [ 'www.baidu.com', 'www.qq.com', 'blog.csdn.net', 'github.com', 'google.com' ] threads = [] for url in server_list: t = threading.Thread(target=count_threads,args=(url,)) threads.append(t) for thread in threads: thread.setDaemon(True) thread.start() thread.join()
坑:prometheus不会提醒metrics的名字,要主动复制进去:curl http://10.0.0.111:19091/metrics
复制:https://blog.csdn.net/specter11235/article/details/87927202
--------------------------------------------------------------------------------------
计算exporter的metrics的比率
#自定义exporter-counter ##状态码是500的个数 url_http_code_total{code="5xx",url="10.0.0.111:55555/a.txt"} #访问接口的个数 http_request_total{url="10.0.0.111:55555/a.txt"} ------------------------------------------------------------------ #错误率 delta(url_http_code_total{code="5xx",url="10.0.0.111:55555/a.txt"}[1m]) / on(url) group_left delta(http_request_total{url="10.0.0.111:55555/a.txt"}[1m]) #http code的每分钟增长率,如果出现5xx,就说明有问题了 irate(http_request_total[1m]) #显示期望时间的比例,比如只显示小于1秒,占总次数的比例 delta(url_http_request_time_total{le='1'}[1m]) / on(url) group_left delta(http_request_total[1m]) #复制 https://blog.csdn.net/specter11235/article/details/87927202
用一个例子来演示会更加清晰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
2019-09-19 kube-metric在kubernetes上的部署
2019-09-19 kubenetes 的svc从ClusterPort 改为NodePort
2019-09-19 prometheus+grafana监控Linux和kubernetes的例子
2018-09-19 Taints和Tolerations -- 污点- 容忍