Prometheus-notifier告警推送
Prometheus-notifier告警推送
基于v3.5
工作原理
sequenceDiagram
participant Q as Queue
participant M as Manager
participant AM as Alertmanager
participant AL as Alertmanager API
Q->>M: nextBatch()
M->>M: 分批处理告警
M->>AM: sendAll()
AM->>AM: 应用特定于 AM 的 relabel
AM->>AL: HTTP POST /api/v2/alerts
AL-->>AM: Response
AM->>M: 返回发送结果
关键特性
- 告警队列管理
有界队列,防止内存无限增长
支持队列满时的丢弃策略
可配置的批处理大小 - Relabeling(重新标记)
发送前对告警应用 relabel 配置
支持每个 Alertmanager 集合的特定 relabel 规则 - 高可用性
支持多个 Alertmanager 实例
自动去重避免重复通知
故障转移机制 - 服务发现集成
与 Prometheus 服务发现机制集成
动态更新 Alertmanager 实例列表
这个 notifier 模块实现了 Prometheus 告警通知的核心功能,确保告警能够可靠地发送到 Alertmanager 集群
详细流程
告警控制器
1 | // Manager 是负责将警报通知分发到警报管理服务的结构体。 |
初始化
1 | notifierManager = notifier.NewManager(&cfg.notifier, logger.With("component", "notifier")) |
Run启动
- targetUpdateLoop 工作流程
sequenceDiagram
participant TUL as targetUpdateLoop
participant CH as tsets 通道
participant R as reload()
participant AM as Alertmanager 集合
loop 持续运行
TUL->>CH: 监听目标组更新
CH-->>TUL: 接收目标组数据
TUL->>R: 调用 reload 方法
R->>AM: 同步 Alertmanager 集合
end
- sendLoop 工作流程
sequenceDiagram
participant SL as sendLoop
participant MC as more 通道
participant SOB as sendOneBatch()
participant SA as sendAll()
participant DQ as drainQueue()
participant SR as stopRequested 通道
loop 持续运行
SL->>SR: 检查停止信号
SR-->>SL: 未停止
SL->>MC: 监听 more 通道
MC-->>SL: 有数据
SL->>SOB: 调用 sendOneBatch
SOB->>SA: 调用 sendAll 发送警报
SA-->>SOB: 发送完成
SOB->>SL: 检查队列是否还有数据
end
SR-->>SL: 收到停止信号
SL->>DQ: 调用 drainQueue 排空队列
- sendAll() 并发发送机制
graph LR
SA[sendAll] --> A[遍历 Alertmanager 集合]
A --> B{集合是否为空}
B -- 不为空 --> C[应用 Alert Relabel]
C --> D[转换为 OpenAPI 格式]
D --> E[JSON 序列化]
E --> F[遍历集合中的实例]
F --> G[启动并发 Goroutine]
G --> H[发送 HTTP 请求]
H --> I[更新指标]
I --> J[等待所有 Goroutine 完成]
J --> K[返回发送结果]
B -- 为空 --> A
- 队列管理机制
graph TD
S[Send 方法] --> RL[应用 Relabel]
RL --> CC{检查容量}
CC -- 超过 --> DO[丢弃旧警报]
CC -- 未超过 --> AQ[添加到队列]
DO --> AQ
AQ --> SM[发送 more 信号]
1 | // Run dispatches notifications continuously, returning once Stop has been called and all |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Joohwan!
评论