自定义指标与自研 pushgateway
官方 pushgateway 及其弊端
官方的 pushgateway 是这样的:

在使用之前,有必要了解一下官方 pushgateway 的一些弊端:
- 将多个节点数据汇总到 pushgateway,如果 pushgateway 挂了,受影响比多个 target 大。
- Prometheus 拉取状态 up 只针对 pushgateway,无法做到对每个节点有效。
- pushgateway 可以持久化推送给它的所有监控数据。当 Prometheus 把数据拉取以后,这些数据仍然留在 pushgateway,不会被自动删除,下一次拉取又会被拉取到 Prometheus 中。
- pushgateway 在接收到同一个 metric 的后一次推送时,会把前一次的内容删除掉。
- pushgateway 客户端在推送数据到 pushgateway 服务器端以后,必须将不同 label 下的数据清理或者置为 0,不然会有叠加效果。
自研 pushgateway 与报警推送代理
由于 Alertmanager 在内网中消息推送有些故障,以及官方 pushgateway 的上述弊端,平台开源了一个 pushgateway,将 metric 数据收集和消息推送集成在 pushgateway 中。
与代码一致:自研 pushgateway 源码位于
install/kubernetes/prometheus/pushgateway/(server.py基于 aiohttp 实现),K8s Service 名为cloud-pushgateway-service,部署在monitoring命名空间,镜像ccr.ccs.tencentyun.com/cube-studio/prometheus:pushgateway(见pushgateway/deploy.yaml/service.yaml)。
报警推送代理接口
POST 访问接口 /{client}/webhook({client} 为来源标识,源码支持 grafana / alertmanager / rancher / superset 等,会按来源裁剪/格式化消息体)。
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
sender_type |
字符串 | 推送类型,目前支持 wechat 和 username_group |
sender |
字符串 | 推送者(微信推送时如 TME_DataInfra;企业微信群推送时为企业微信机器人 key) |
username |
字符串 | 接收用户(逗号分隔多用户)。微信推送时为 username;企业微信群推送时为空 |
message |
字符串 | 推送字符串。如果有 message 字段,则仅推送 message 字段;否则除上面之外的所有字段会 JSON 序列化为 message 推送 |
与代码一致(
pushgateway/server.py):
sender_type=wechat→push_wechat():微信公众号告警指向个人推送,通过Sender(发送者) +Rcptto(接收者列表) 调用内网微信接口。sender_type=username_group→push_username_group():企业微信群推送,sender为企业微信群聊机器人的 key,调用企业微信群机器人 webhook。- 接口实参不区分大小写匹配
username/sender_type/sender,可放在 query 或 JSON body 中。
说明:与本仓库
pushgateway/server.py参数语义一致——接收用户字段在源码中读取的是username键并赋给内部Receiver变量(见install/kubernetes/prometheus/pushgateway/server.py:119-128)。
指标推送(非告警)接口为 POST
/metrics,推送数据格式(metric 的 label/data、exist_update_type/not_exist_update_type等处理策略)详见install/kubernetes/prometheus/pushgateway/README.md。
监控业务自定义数据(Python 自定义 metrics)
如果通用监控无法满足需求,可以自己编写 metrics 接口。下面以 Python 为例(flask 版本和 aiohttp 版本)。
说明:原文提到的
python-metric目录在本商业版仓库中未找到(属于开源 CubeStudio 仓库的示例)。本仓库中可参考的 aiohttp 实现是自研 pushgateway 的install/kubernetes/prometheus/pushgateway/server.py。下方为通用示例代码。
flask 版本
pip install flask
pip install prometheus_client
import prometheus_client
from prometheus_client import Counter
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask
####
@app.route("/metrics")
def ApiResponse():
muxStatus.set(muxCode)
manageStatus.set(manageCode)
return Response(prometheus_client.generate_latest(REGISTRY), mimetype="text/plain")
aiohttp 版本
pip install asyncio
pip install aiohttp
pip install prometheus_client
import prometheus_client
from prometheus_client import Counter, Gauge
from prometheus_client.core import CollectorRegistry
from aiohttp import web
import aiohttp
import asyncio
import random, logging, time, datetime
routes = web.RouteTableDef()
# metrics
@routes.get('/metrics')
async def metrics(request):
requests_total.inc()
# requests_total.inc(2)
data = prometheus_client.generate_latest(requests_total)
return web.Response(body=data, content_type="text/plain")
形成的 metrics 接口
# HELP face_search_metric face search metrics
# TYPE face_search_metric gauge
face_search_metric{method="POST",path="/vesionbook/v1.0/user/feature",project="vesionbook"} 11.0
face_search_metric{method="POST",path="/vesionbook/v1.0/user/search",project="vesionbook"} 11.0
形成 metrics 接口后,再通过 ServiceMonitor 将其采集到 Prometheus 中。