SRE Split Alert 是一个用于接收Prometheus Alertmanager 告警并根据状态(firing 或 resolved)进行分组和转发的服务, 接受Alertmanager的信息,分组,并推送到PrometheusAlert。原生Alertmanager group中既包括firing 也包括 resolved,参考此issue
- 接收 Prometheus Alertmanager告警请求。
- 根据告警的状态(
firing或resolved)对告警进行分组。 - 将分组后的告警转发到指定的目标地址。
Dockerfile: 用于构建和运行服务的 Docker 镜像。splitAlert.go: 服务的核心逻辑,包括告警处理和配置加载。config.yml: 服务的配置文件(示例见下文)。
服务需要一个 config.yml 文件,放置在 /opt/splitAlert/config.yml 路径下。以下是一个示例配置:
prometheusAlertUrl: "http://example.com/alert"
config:
exampleConfig:
key1: "value1"
key2: "value2"prometheusAlertUrl: 转发告警的目标地址。config: 配置映射,定义不同的配置名称及其对应的键值对,将其转换成额外的url param参数。
该配置文件完全兼容PrometheusAlert项目,例如
将Alertmanager的配置
- name: 'prod'
webhook_configs:
- url: http://prometheusalert.monitoring:8080/prometheusalert?type=yourtype&tpl=yourtpl&fsurl=https://open.xxx.cn/open-apis/bot/v2/hook/xxxx转换成
- name: 'prod'
webhook_configs:
- url: 'http://split-alert.monitoring:8080/alert?config=prod'
同时,将PrometheusAlert的url以及额外的参数写进config.yml文件即可:
prometheusAlertUrl: http://prometheusalert.monitoring:8080/prometheusalert
config:
prod:
fsurl: https://open.xxx.cn/open-apis/bot/v2/hook/xxxx
tpl: yourtpl
type: yourtype-
构建 Docker 镜像:
进入到
code目录中docker build -t sre-split-alert . -
运行容器:
docker run -d -p 8080:8080 -v /path/to/config.yml:/opt/splitAlert/config.yml sre-split-alert
- 将本地的
config.yml挂载到容器的/opt/splitAlert/config.yml。
- 将本地的
-
安装依赖:
确保已安装 Go 语言环境,然后运行以下命令:
go mod tidy
-
运行服务:
go run splitAlert.go
-
访问服务:
服务默认运行在
8080端口。
进入到deployment中,手动apply yaml文件到china-ops集群中
- 方法:
POST - 描述: 接收 Prometheus 告警并转发。
- 参数:
config(query 参数): 指定使用的配置名称。
- 请求体: Prometheus 告警的 JSON 数据。
- 方法:
GET - 描述: 手动重新加载配置文件。
服务会输出以下类型的日志:
📥: 表示接收到请求。✅: 表示操作成功。❌: 表示操作失败。🔁: 表示重新加载配置。
以下是一个使用 curl 测试服务的示例:
curl -X POST "http://localhost:8080/alert?config=exampleConfig" \
-H "Content-Type: application/json" \
-d '{
"status": "firing",
"alerts": [
{"status": "firing", "labels": {"alertname": "HighCPU"}},
{"status": "resolved", "labels": {"alertname": "DiskFull"}}
]
}'欢迎提交 Issue 或 Pull Request 来改进此项目。
此项目使用 MIT 许可证。