Documentation
¶
Overview ¶
Package alert 提供监控告警:轮询采集资源指标,水位超阈值时推送到 企业微信/钉钉/飞书机器人 webhook,支持连续触发确认、告警去重与恢复通知。
与 framework/monitor 配合:
collector := monitor.NewCollector()
watcher := alert.NewWatcher(alert.Config{
Interval: 15 * time.Second, // 轮询间隔
Collector: collector, // 指标来源
Notifiers: []alert.Notifier{alert.NewWeComWebhook("https://qyapi.weixin.qq.com/...")},
Rules: []alert.Rule{
alert.CPUUsage(90, 3), // CPU > 90% 连续 3 次告警
alert.MemoryUsage(85, 3),
alert.DiskUsage(85, 3),
},
})
watcher.Start(ctx) // 或 go watcher.Start(ctx) 异步
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Interval 轮询间隔(默认 15 秒)。
Interval time.Duration
// Collector 指标采集器(必填)。
Collector *monitor.Collector
// Notifiers 通知器列表(至少一个)。
Notifiers []Notifier
// Rules 告警规则列表。
Rules []Rule
// OnNotify 通知回调(测试/日志用;每条消息发送前调用)。
OnNotify func(message Message)
}
Config 监视器配置。
type Message ¶
type Message struct {
Title string `json:"title"`
Content string `json:"content"`
Level Level `json:"level"`
}
Message 告警消息(传给 Notifier)。
type Notifier ¶
type Notifier interface {
// Name 通知渠道名(日志/调试用)。
Name() string
// Notify 发送一条告警消息。
Notify(ctx context.Context, message Message) error
}
Notifier 通知器接口(企业微信/钉钉/飞书/自定义)。
type Rule ¶
type Rule struct {
// Name 规则名(如 "cpu"、"memory")。
Name string
// Threshold 触发阈值(使用率百分比 0-100)。
Threshold float64
// Consecutive 连续多少次采样超阈值才触发告警(防抖动)。
Consecutive int
// Check 从快照提取指标值(0-100);nil 时按 Metric 字段读取。
Check func(stats *monitor.Stats) float64
// Level 告警级别(默认 warning)。
Level Level
}
Rule 告警规则。
type Watcher ¶
type Watcher struct {
// contains filtered or unexported fields
}
Watcher 轮询监视器:周期采集指标,按规则触发告警与恢复通知。 去重语义:同一规则处于告警状态时不重复推送;恢复(低于阈值)后推送恢复消息。
type WebhookNotifier ¶
type WebhookNotifier struct {
// contains filtered or unexported fields
}
WebhookNotifier 通过机器人 webhook 推送告警(企业微信/钉钉/飞书)。
func NewDingTalkWebhook ¶
func NewDingTalkWebhook(webhookURL string) *WebhookNotifier
NewDingTalkWebhook 钉钉机器人通知器。
func NewFeishuWebhook ¶
func NewFeishuWebhook(webhookURL string) *WebhookNotifier
NewFeishuWebhook 飞书机器人通知器。
func NewWeComWebhook ¶
func NewWeComWebhook(webhookURL string) *WebhookNotifier
NewWeComWebhook 企业微信机器人通知器。 webhookURL 为机器人地址(https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx)。
type WebhookType ¶
type WebhookType string
WebhookType 机器人平台类型。
const ( // TypeWeCom 企业微信机器人。 TypeWeCom WebhookType = "wecom" // TypeDingTalk 钉钉机器人。 TypeDingTalk WebhookType = "dingtalk" // TypeFeishu 飞书机器人。 TypeFeishu WebhookType = "feishu" // TypeGeneric 通用 JSON webhook(自定义格式,自定义实现时使用)。 TypeGeneric WebhookType = "generic" )
Click to show internal directories.
Click to hide internal directories.