监控指标推送与报警代理

需要把自定义业务指标推送给 Prometheus,或把报警转发到微信/企业微信群时阅读

运维管理 / 监控
pushgatewaycloud-pushgateway自定义指标custom metricsprometheus指标推送报警代理webhook企业微信wechatalertmanagergrafana webhook

监控指标推送与报警代理(cloud-pushgateway)

cloud-pushgateway 是平台自研的指标网关服务(非官方 prometheus pushgateway),提供两类能力:

  1. 接收自定义指标的 POST 推送,按策略合并后以 Prometheus 文本格式对外暴露 /metrics 供采集;
  2. 作为报警推送代理,把外部系统(Grafana / Alertmanager / Rancher 等)的 webhook 报警转发到微信或企业微信群。

部署相关文件在 install/kubernetes/prometheus/pushgateway/

  • 镜像:ccr.ccs.tencentyun.com/cube-studio/prometheus:pushgateway
  • 服务监听端口:80util/config.pyLOCAL_SERVER_PORT=80,容器 containerPort: 80
  • 默认 sender:环境变量 sender,默认 TME_DataInfra;默认推送类型 Sender_type 默认 wechat
  • 调度:通过节点亲和性 monitoring=true 调度(与监控组件同机器,部署见 监控部署与看板

1. Prometheus 指标推送

POST /metrics 推送数据,请求体为 JSON,外层含 typemetrics 两个字段(服务端读取 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/resetkeep(保留原状态)即不命中上述分支时的默认行为。详见 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} 为来源系统标识(如 grafanaalertmanagerranchersuperset 等,会做来源特定的消息格式化;其他值按通用方式处理)。

参数(可放在 query 或 JSON body 中):

  • sender_type:字符串。推送类型,目前支持 wechatusername_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)。

相关文档

最后更新 2026-06-30完整文档以官方仓库为准:GitHub Wiki