监控指标推送与报警代理(cloud-pushgateway)
cloud-pushgateway 是平台自研的指标网关服务(非官方 prometheus pushgateway),提供两类能力:
- 接收自定义指标的 POST 推送,按策略合并后以 Prometheus 文本格式对外暴露
/metrics供采集; - 作为报警推送代理,把外部系统(Grafana / Alertmanager / Rancher 等)的 webhook 报警转发到微信或企业微信群。
部署相关文件在 install/kubernetes/prometheus/pushgateway/:
- 镜像:
ccr.ccs.tencentyun.com/cube-studio/prometheus:pushgateway - 服务监听端口:
80(util/config.py中LOCAL_SERVER_PORT=80,容器containerPort: 80) - 默认 sender:环境变量
sender,默认TME_DataInfra;默认推送类型Sender_type默认wechat - 调度:通过节点亲和性
monitoring=true调度(与监控组件同机器,部署见 监控部署与看板)
1. Prometheus 指标推送
向 POST /metrics 推送数据,请求体为 JSON,外层含 type 与 metrics 两个字段(服务端读取 data['metrics'])。
post 数据格式:
type:'prometheus'
metrics:
{
'metric_name1':{
'lables':['method', 'clientip'],
'describe':'this is test',
'exist_not_update_type':'clear',
'exist_update_type':'update',
'not_exist_update_type':'add',
'pull_finish_deal_type':'clear',
'data':[
[['get','192.168.11.127'],4],
[['get','192.168.12.49'],3],
[['post','192.168.11.127'],5]
]
},
'metric_name2':{
}
}
说明:源 README 示例中此处写的是
push_deal_type,与服务端代码不符,已按代码改正为pull_finish_deal_type(见install/kubernetes/prometheus/pushgateway/util/prometheus_util.py:39,服务端读取的 key 为pull_finish_deal_type)。
各策略字段含义:
exist_not_update_type(已存在但本次未更新的数据)exist_update_type(已存在且本次更新的数据)not_exist_update_type(本次新出现的数据)pull_finish_deal_type(数据被拉取以后的处理行为)
可选取值:
# update 覆盖原有值
# clear 删除属性
# keep 保留原状态
# add 属性的 value 累加
# reset 属性的值设置为 0
说明:服务端对
exist_not_update_type实现了clear/reset;对exist_update_type实现了update/add/clear/reset;对not_exist_update_type实现了reset/add/update;对pull_finish_deal_type实现了clear/reset。keep(保留原状态)即不命中上述分支时的默认行为。详见install/kubernetes/prometheus/pushgateway/util/prometheus_util.py:72-156。
服务端(Python)内部存储结构(data 由数组转为按 label 元组索引的字典):
{
'metric_name1':{
'lables':['method', 'clientip'],
'describe':'this is test',
'exist_not_update_type':'clear',
'exist_update_type':'update',
'not_exist_update_type':'add',
'data':{
('get','192.168.11.127'):4,
('get','192.168.12.49'):3,
('post','192.168.11.127'):5
}
},
'metric_name2':{
}
}
Prometheus 通过 GET /metrics 拉取(返回 text/plain 文本格式),拉取后会按 pull_finish_deal_type 对数据做后处理。
2. 报警推送代理
POST 访问接口 /{client}/webhook,其中 {client} 为来源系统标识(如 grafana、alertmanager、rancher、superset 等,会做来源特定的消息格式化;其他值按通用方式处理)。
参数(可放在 query 或 JSON body 中):
sender_type:字符串。推送类型,目前支持wechat和username_group(企业微信群)。sender:字符串。推送者(微信场景为TME_DataInfra之类的发送方;企业微信群场景为群机器人 key)。username:字符串。接收用户(逗号分隔多用户)。微信推送时为接收人 username;企业微信群推送时为空。message:推送字符串。若带message字段则仅推送message;否则除上述参数外的所有字段会 JSON 序列化为 message 推送。
示例(向 juno 来源推送一条微信消息):
curl -H 'Content-type: application/json' -X POST \
-d '{"message":"'"${MY_POD_NAME}"' juno restart"}' \
'http://<pushgateway-ip>/juno/webhook?username=pengluan&sender_type=wechat'
推送实现说明(见 install/kubernetes/prometheus/pushgateway/server.py:40-80):
sender_type=wechat:调用微信公众号告警接口,按username(接收人)做个人推送,需要同时有 sender、receiver、message。sender_type=username_group:向企业微信群机器人 webhook 推送,sender为群机器人 key,message为文本内容(此时 username 为空)。
备注:除
/{client}/webhook外,服务端还提供POST /junopodstop/customwebhook(按username+message直接推送),以及GET /(健康检查,返回OK)。
相关文档
- 自定义指标与 pushgateway 的设计原理见 01-架构原理/监控体系/04-自定义指标与pushgateway。
- 监控整体部署、Grafana 看板见 监控部署与看板。