自定义指标与自研 pushgateway

想用 pushgateway 推送业务指标、或自己写 /metrics 接口、或了解平台告警推送代理接口时读这篇

01-架构原理 / 监控体系
pushgateway自定义指标custom metrics报警代理webhookprometheus_clientflaskaiohttpCounterGauge/metrics消息推送wechatusername_group企业微信

自定义指标与自研 pushgateway

官方 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 字符串 推送类型,目前支持 wechatusername_group
sender 字符串 推送者(微信推送时如 TME_DataInfra;企业微信群推送时为企业微信机器人 key)
username 字符串 接收用户(逗号分隔多用户)。微信推送时为 username;企业微信群推送时为空
message 字符串 推送字符串。如果有 message 字段,则仅推送 message 字段;否则除上面之外的所有字段会 JSON 序列化为 message 推送

与代码一致(pushgateway/server.py):

  • sender_type=wechatpush_wechat():微信公众号告警指向个人推送,通过 Sender(发送者) + Rcptto(接收者列表) 调用内网微信接口。
  • sender_type=username_grouppush_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 中。

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