Documentation
¶
Overview ¶
Package notify 提供一个可复用的告警通知模块:所有渠道统一实现 Notifier 接口, 监控/调度方只依赖接口即可发送通知;新增渠道(邮件 / 短信 / 通用 webhook …)只需实现接口并注册。
当前内置实现:
- WeComNotifier 企业微信群机器人(markdown,支持 @ 成员)
- DingTalkNotifier 钉钉自定义机器人(markdown + 加签)
- FeishuNotifier 飞书/Lark 自定义机器人(text + 加签)
- EmailNotifier SMTP 邮件
- WebhookNotifier 通用 webhook(支持 Go text/template 自定义报文,兜底任意系统)
- SlackNotifier / TelegramNotifier / PagerDutyNotifier SaaS 原生 webhook 适配(PagerDuty 按 Alert.Kind 自动 trigger↔resolve),详见 saas_channels.go
设计要点(与日志模块 logutil 同构):渠道只在「注册表」里定义一次(含 webhook), 之后在业务侧按名字引用,不必到处粘贴 webhook。
Index ¶
- func BuildChannelRegistry(channels []NamedChannel) map[string]NotifierConfig
- func ConfigComplete(nc NotifierConfig) error
- func DingTalkSign(secret string, timestamp int64) string
- func FeishuSign(secret string, timestamp int64) string
- type Alert
- type DingTalkNotifier
- type EmailConfig
- type EmailNotifier
- type FeishuNotifier
- type NamedChannel
- type Notifier
- func BuildFromConfig(channels []NamedChannel, names []string, inline []NotifierConfig, dryRun bool) []Notifier
- func BuildNotifiers(targetNotifiers []NotifierConfig, dryRun bool) []Notifier
- func NewNotifier(nc NotifierConfig, dryRun bool) Notifier
- func ResolveNamedChannels(names []string, registry map[string]NotifierConfig, dryRun bool) []Notifier
- type NotifierConfig
- type PagerDutyNotifier
- type SlackNotifier
- type TelegramNotifier
- type WeComNotifier
- type WebhookNotifier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildChannelRegistry ¶
func BuildChannelRegistry(channels []NamedChannel) map[string]NotifierConfig
BuildChannelRegistry 把 channels 注册表转成 map[name]NotifierConfig,供按名引用查找。
func ConfigComplete ¶
func ConfigComplete(nc NotifierConfig) error
ConfigComplete 校验单个渠道配置是否具备发送所需的必填字段(不含网络连通性)。 用于「通知渠道可用性自检」:先校验配置完整性,再实际发送探测连通性, 避免「未配置 webhook 却被当作发送成功」的误报。 返回 nil 表示配置完整;否则返回缺失项/未知类型的描述。
func DingTalkSign ¶
DingTalkSign 计算加签:timestamp(毫秒) + "\n" + secret,HMAC-SHA256 后 base64。
func FeishuSign ¶
FeishuSign 计算加签:HMAC-SHA256(secret, fmt.Sprint(timestamp)) 后 base64。
Types ¶
type Alert ¶
type Alert struct {
Name string // 目标名称
Host string // 目标主机
Kind string // "down" | "recover" | "down-still" | "flapping" | "selfcheck"
Level string // "normal" | "escalated"(escalated 表示已升级到高级别通道)
FailStreak int // 当前连续失败次数(down / down-still 时有意义)
DownTime time.Duration // 中断时长(recover / down-still 时有意义)
Time time.Time // 事件时间
}
Alert 是发送给通知渠道的结构化事件。各渠道自行渲染成自己的消息格式。 注意:@ 成员(mention)由各渠道自己持有,不在 Alert 里,从而不同渠道可用不同 mention。
type DingTalkNotifier ¶
DingTalkNotifier 钉钉自定义机器人。支持「关键词 / 加签 / IP 白名单」中的加签方式: 在 webhook 后追加 ?timestamp=...&sign=...,sign = HMAC-SHA256(secret, timestamp+"\n"+secret) 的 URL 编码。
func (DingTalkNotifier) Notify ¶
func (n DingTalkNotifier) Notify(ctx context.Context, a Alert) error
func (DingTalkNotifier) Type ¶
func (n DingTalkNotifier) Type() string
type EmailConfig ¶
type EmailConfig struct {
Host string `yaml:"host"` // SMTP 服务器,如 "smtp.example.com"
Port int `yaml:"port"` // SMTP 端口,通常 465(SSL) / 587(TLS) / 25
User string `yaml:"user"` // 发件账号
Pass string `yaml:"pass"` // 授权码 / 密码
From string `yaml:"from"` // 发件人地址
To string `yaml:"to"` // 收件人地址(逗号分隔多个)
Subject string `yaml:"subject"` // 邮件主题前缀,默认 "监控告警"
}
EmailConfig 是邮件渠道专属配置,单独成组避免与 IM 渠道字段混在一起。 同时被 EmailNotifier 复用为内嵌字段,保证「配置 ↔ 实例」字段一致。
type EmailNotifier ¶
type EmailNotifier struct {
EmailConfig
DryRun bool
}
EmailNotifier 通过 SMTP 发送告警邮件(标准库 net/smtp,无第三方依赖)。 适用于需要「离线也能留存」的告警通道(区别于即时 IM 机器人)。
func (EmailNotifier) Notify ¶
func (n EmailNotifier) Notify(ctx context.Context, a Alert) error
Notify 发送一封告警邮件。DryRun 时仅打印,不真正发出。
func (EmailNotifier) Type ¶
func (n EmailNotifier) Type() string
type FeishuNotifier ¶
FeishuNotifier 飞书/ Lark 自定义机器人。加签方式:请求体携带 timestamp 与 sign = HMAC-SHA256(key=secret, data=时间戳字符串) 的 base64。
func (FeishuNotifier) Type ¶
func (n FeishuNotifier) Type() string
type NamedChannel ¶
type NamedChannel struct {
Name string `yaml:"name"` // 渠道唯一名字,供引用
Type string `yaml:"type"` // "wecom" | "dingtalk" | "feishu"(或 "lark") | "email" | "webhook" | "slack" | "telegram" | "pagerduty"
Webhook string `yaml:"webhook"`
Mention string `yaml:"mention"`
Secret string `yaml:"secret"` // 可选:钉钉/飞书签名密钥(加签机器人需要)
URL string `yaml:"url"` // 通用 webhook 地址
Headers map[string]string `yaml:"headers"` // 通用 webhook 自定义请求头
Template string `yaml:"template"` // 通用 webhook 报文模板(Go text/template,作用于 Alert)
Channel string `yaml:"channel"` // slack:目标频道
Token string `yaml:"token"` // telegram:Bot token
ChatID string `yaml:"chat_id"` // telegram:目标 chat_id
RoutingKey string `yaml:"routing_key"` // pagerduty:Integration Key
Severity string `yaml:"severity"` // pagerduty:事件级别(缺省 critical)
EmailConfig `yaml:",inline"` // 邮件(email)专属字段,内联展开
}
NamedChannel 是「渠道注册表(channels)」里的一个具名渠道: 先在此用名字定义一次(含 webhook / mention 等),之后在 defaults / target 里 只要写 channels: [名字] 引用即可,无需重复粘贴 webhook。
type Notifier ¶
Notifier 是所有通知渠道的统一接口。
func BuildFromConfig ¶
func BuildFromConfig(channels []NamedChannel, names []string, inline []NotifierConfig, dryRun bool) []Notifier
BuildFromConfig 是「注册表 → 解析 → 构造」一步到位的门面,覆盖 90% 调用方的需求: 传入具名渠道注册表 channels、引用了渠道名的 names、以及内联 notifiers, 直接得到可用的 []Notifier。等价于先 BuildChannelRegistry 再 ResolveNamedChannels / BuildNotifiers。
解析优先级(与 Monitor.toMonitor 的约定保持一致):
names(具名引用)优先于 inline(内联),二者都提供时合并去重。
返回列表顺序:先 names 命中项,再 inline 项。dryRun 透传给每个 Notifier。
func BuildNotifiers ¶
func BuildNotifiers(targetNotifiers []NotifierConfig, dryRun bool) []Notifier
BuildNotifiers 把内联 notifiers 列表(兼容写法)构造为 Notifier 列表。 仅处理显式内联的渠道;全局/遗留默认已移除,渠道统一走 channels 注册表。
func NewNotifier ¶
func NewNotifier(nc NotifierConfig, dryRun bool) Notifier
NewNotifier 根据配置构造一个 Notifier 实例;不支持的类型返回 nil(并打警告)。
func ResolveNamedChannels ¶
func ResolveNamedChannels(names []string, registry map[string]NotifierConfig, dryRun bool) []Notifier
ResolveNamedChannels 按名字从注册表查到渠道配置,构造 Notifier 列表。 名字在注册表中不存在时打警告并跳过该渠道。
type NotifierConfig ¶
type NotifierConfig struct {
Type string `yaml:"type"`
Webhook string `yaml:"webhook"`
Mention string `yaml:"mention"`
Secret string `yaml:"secret"`
URL string `yaml:"url"`
Headers map[string]string `yaml:"headers"`
Template string `yaml:"template"`
Channel string `yaml:"channel"`
Token string `yaml:"token"`
ChatID string `yaml:"chat_id"`
RoutingKey string `yaml:"routing_key"`
Severity string `yaml:"severity"`
EmailConfig `yaml:",inline"` // 邮件(email)专属字段,内联展开
}
NotifierConfig 是单个渠道的精简配置,用于「内联 notifiers 列表」兼容写法。
type PagerDutyNotifier ¶
type PagerDutyNotifier struct {
RoutingKey string // Integration Key(Events API v2 routing_key)
Severity string // 事件级别:critical|error|warning|info;空默认 critical
DryRun bool
}
PagerDutyNotifier PagerDuty 渠道(Events API v2)。 根据 Alert.Kind 自动映射事件动作:down / down-still → trigger,recover → resolve, 确保一次事件只产生 / 只关闭一条 incident。
func (PagerDutyNotifier) Notify ¶
func (n PagerDutyNotifier) Notify(ctx context.Context, a Alert) error
func (PagerDutyNotifier) Type ¶
func (n PagerDutyNotifier) Type() string
type SlackNotifier ¶
type SlackNotifier struct {
Webhook string // https://hooks.slack.com/services/...
Channel string // 可选:目标频道,如 #alerts
DryRun bool
}
SlackNotifier Slack 渠道。把统一渲染文案 POST 到 incoming webhook。
func (SlackNotifier) Type ¶
func (n SlackNotifier) Type() string
type TelegramNotifier ¶
type TelegramNotifier struct {
Token string // Bot token,形如 123456:ABC-...
ChatID string // 目标 chat_id(群/私聊),可传数字或 @频道名
DryRun bool
}
TelegramNotifier Telegram 渠道。通过 Bot token 调官方 sendMessage 接口。
func (TelegramNotifier) Notify ¶
func (n TelegramNotifier) Notify(ctx context.Context, a Alert) error
func (TelegramNotifier) Type ¶
func (n TelegramNotifier) Type() string
type WeComNotifier ¶
WeComNotifier 企业微信渠道实现(当前默认渠道)。@ 成员自持于本结构体。
func (WeComNotifier) Type ¶
func (n WeComNotifier) Type() string
type WebhookNotifier ¶
type WebhookNotifier struct {
URL string // 目标 webhook 地址(必填)
Headers map[string]string // 可选:自定义请求头(如 Authorization / Content-Type)
// Template 可选:Go text/template 字符串,作用于 Alert,产出请求体。
// 不设置则发送默认 JSON。模板内可用 .Name .Host .Kind .Level .FailStreak .DownTime .Time。
Template string
DryRun bool
}
WebhookNotifier 是「通用 Webhook」渠道:向任意 HTTP 端点 POST 一条消息, 用于对接 Slack / Telegram / PagerDuty / 企业自建告警网关等任意系统, 是补齐「通知渠道数量」差距的通用兜底(无需为每个平台写专用适配器)。
默认发送 JSON:{"title":..., "body":..., "alert":{...Alert 原样}}。 如需自定义报文,可设置 Template(Go text/template,作用对象为 Alert)。
func (WebhookNotifier) Type ¶
func (n WebhookNotifier) Type() string