=== 1. 「数据库连接失败」匹配模式 3 → 25 个 ===
原来只匹配 connection refused / reset by peer / too many connections,
漏掉了 PG 的 FATAL: too many clients、Redis 的 NOAUTH / WRONGPASS /
connection pool timeout 等实际最常见的报错。
按数据库分类补齐:
通用网络层
connection refused / connection reset by peer / broken pipe /
no route to host / cannot assign requested address /
connection timed out / i/o timeout / unexpected EOF /
server closed the connection unexpectedly
MySQL
too many connections / communications link failure /
driver: bad connection / could not create connection /
Access denied for user
PostgreSQL
FATAL: password authentication failed / sorry, too many clients /
terminating connection
Redis
NOAUTH / WRONGPASS / READONLY you can't write against /
CLUSTERDOWN / max number of clients reached
连接池(Java/Go 应用层最先炸的地方)
connection pool / connection is not available
**刻意不加**的(会匹配到几乎所有网络日志,等于没告警):
裸 "EOF"、"timeout"、"error"、"refused"、"deadlock"
阈值保持 1000/15min(模式变多不代表命中变多,都是真错误才命中)。
迁移 v9→v10 只替换"仍是旧默认查询"的规则,用户自定义过的不动。
=== 2. 规则表单三个字段没对齐 ===
现象:统计窗口(分) / 触发阈值(条) / 冷却(分) 三格并排,
但只有中间的「触发阈值」下方多了一行提示文字,
导致中间那格更高、提示文字悬在底部,看起来像没对齐。
修法:三格统一为「标签 + 输入框 + 一行提示(min-h-4 固定高度)」,
底边自然齐平;提示文案改为:
统计窗口 → 统计最近多少分钟
触发阈值 → 超过该条数才告警
冷却 → 冷却内不重复发送
(顺带把阈值提示从"0 = 出现即告警"改掉:默认阈值已无 0,0 反而是危险配置)
小屏改单列堆叠(grid-cols-1 sm:grid-cols-3)。
=== 实测(936a127b1d15)===
v10 迁移:告警规则 "数据库连接失败" 扩展匹配模式
线上规则:25 个模式,含 NOAUTH / too many clients,阈值 1000,冷却 240m
Pod = latest = v0.1 = 936a127b1d15
145 lines
6.7 KiB
Go
145 lines
6.7 KiB
Go
package seed
|
|
|
|
// 内置告警规则 — 新环境开箱即用的通用日志告警(参照运维告警脚本的两类模式):
|
|
// 致命类(出现即报):FATAL / panic / OutOfMemoryError / StackOverflowError
|
|
// 突增类(超阈值才报):应用 error 过多 / ingress-nginx 4xx / 5xx
|
|
// 绑定数据源:部署成功自动注册的 LogSource(managed)。
|
|
// 规则默认不启用(通知通道由管理员后期在设置页配置后再开启),ChannelIDs 留空。
|
|
import (
|
|
"time"
|
|
|
|
"lokistack/models"
|
|
"lokistack/repository"
|
|
)
|
|
|
|
type builtinRule struct {
|
|
name string
|
|
query string
|
|
windowMin int
|
|
threshold int
|
|
cooldown int
|
|
}
|
|
|
|
// 阈值设计原则(踩过坑,写清楚免得后来人再调回 0):
|
|
//
|
|
// 1. **绝不用 threshold: 0**
|
|
// 0 表示"命中一条就报"。日志平台里一条 FATAL 或一个 error 关键字
|
|
// 在高峰期每秒就有几百条,阈值 0 等于把告警变成日志流的复读机
|
|
// (实测某环境 15 分钟 3051 万条 → 一直报警)。
|
|
// 2. 默认阈值按 **15 分钟窗口 + 大集群基线** 取,
|
|
// 宁可漏报一点,也不要让运维对告警脱敏——告警疲劳比漏报更致命。
|
|
// 3. cooldown 统一 ≥ 120 分钟:同一问题 2 小时内只提醒一次。
|
|
// 4. 绝对阈值天然不跨环境通用,大集群(如 300GB/天)必须按 app/namespace
|
|
// 收窄查询,否则任何阈值都会被常态流量击穿。这一点在 UI 上有提示。
|
|
//
|
|
// ensurePlaceholderChannel 返回一个可挂靠的通道 ID:
|
|
// 已有通道取第一个;没有则建一个禁用的占位通道。
|
|
func ensurePlaceholderChannel(db *repository.JsonDB) uint {
|
|
db.Lock()
|
|
defer db.Unlock()
|
|
if len(db.NotifyChannels) > 0 {
|
|
return db.NotifyChannels[0].ID
|
|
}
|
|
next := uint(0)
|
|
for _, ch := range db.NotifyChannels {
|
|
if ch.ID > next {
|
|
next = ch.ID
|
|
}
|
|
}
|
|
id := next + 1
|
|
db.NotifyChannels = append(db.NotifyChannels, models.NotifyChannel{
|
|
ID: id,
|
|
Name: "默认告警",
|
|
Enabled: false,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
})
|
|
return id
|
|
}
|
|
|
|
// channelIDsFor 把单个通道 ID 包成切片;0 表示没有可用通道(保持空)。
|
|
func channelIDsFor(id uint) []uint {
|
|
if id == 0 {
|
|
return nil
|
|
}
|
|
return []uint{id}
|
|
}
|
|
|
|
var builtinRules = []builtinRule{
|
|
// —— 致命类:这类日志本该极少出现,但也不能一条就报 ——
|
|
{name: "致命日志 FATAL", query: `{namespace=~".+"} |= "FATAL"`, windowMin: 15, threshold: 10, cooldown: 120},
|
|
{name: "Go panic", query: `{namespace=~".+"} |= "panic:"`, windowMin: 15, threshold: 10, cooldown: 120},
|
|
{name: "JVM OutOfMemoryError", query: `{namespace=~".+"} |~ "(^|[^a-zA-Z])OutOfMemoryError"`, windowMin: 15, threshold: 5, cooldown: 120},
|
|
{name: "StackOverflowError", query: `{namespace=~".+"} |= "StackOverflowError"`, windowMin: 15, threshold: 5, cooldown: 120},
|
|
// 注意:这里**只放日志类告警**。
|
|
// 「节点 NotReady」「容器 OOMKilled」属于基础设施指标监控(kube-state-metrics /
|
|
// Prometheus)的职责,用日志去匹配这些字符串既不可靠(日志没开或轮转就漏),
|
|
// 又与日志平台的定位混淆。这类告警请交给 Prometheus + Alertmanager。
|
|
// —— 突增类:只有明显高于常态才报 ——
|
|
// 第二轮上调(500~5000 这一档实测仍然太敏感):
|
|
// 这几个量级在业务高峰期本来就是常态,阈值贴着基线就等于"每天准点报警"。
|
|
// 原则:宁可只报"明显异常",也不要让人对告警脱敏。
|
|
{name: "应用错误激增(error > 10万)", query: `{namespace=~".+"} |~ "(?i)\berror\b"`, windowMin: 15, threshold: 100000, cooldown: 240},
|
|
{name: "ingress-nginx 5xx 错误", query: `{app=~"ingress-nginx|nginx-ingress-controller"} |~ " (50[0-4]|5[2-9][0-9]) "`, windowMin: 15, threshold: 2000, cooldown: 240},
|
|
{name: "ingress-nginx 4xx 激增", query: `{app=~"ingress-nginx|nginx-ingress-controller"} |~ " 4[0-9][0-9] "`, windowMin: 15, threshold: 20000, cooldown: 240},
|
|
// 数据库连接类错误:覆盖 MySQL / PostgreSQL / Redis 的主流报错文案
|
|
// (详见函数上方注释,不要加裸 "EOF" / "timeout",那会匹配到几乎所有网络日志)
|
|
{name: "数据库连接失败", query: `{namespace=~".+"} |~ "(?i)(connection refused|connection reset by peer|broken pipe|no route to host|cannot assign requested address|connection timed out|i/o timeout|too many connections|too many clients already|max number of clients reached|connection pool|connection is not available|could not create connection|communications link failure|server closed the connection unexpectedly|unexpected EOF|driver: bad connection|FATAL:\\s*(password authentication failed|sorry, too many clients|terminating connection)|NOAUTH|WRONGPASS|READONLY you can.t write|CLUSTERDOWN)"`, windowMin: 15, threshold: 1000, cooldown: 240},
|
|
}
|
|
|
|
// seedBuiltinAlertRules 为每个平台管理的 LogSource(managed=true)补齐内置规则:
|
|
// 首次部署 Loki 后即有一套可直接启用的告警模板;通知通道留空,管理员配置后启用。
|
|
// SeedAlertRules 补齐内置告警规则(幂等;公开给启动与部署成功两处调用)
|
|
func SeedAlertRules() {
|
|
db := repository.GetDB()
|
|
db.RLock()
|
|
type srcRef struct {
|
|
id uint
|
|
name string
|
|
}
|
|
var sources []srcRef
|
|
for i := range db.LogSources {
|
|
if db.LogSources[i].Managed {
|
|
sources = append(sources, srcRef{id: db.LogSources[i].ID, name: db.LogSources[i].Name})
|
|
}
|
|
}
|
|
// 已有规则(按 name)跳过
|
|
existing := map[string]bool{}
|
|
for i := range db.AlertRules {
|
|
existing[db.AlertRules[i].Name] = true
|
|
}
|
|
db.RUnlock()
|
|
|
|
if len(sources) == 0 {
|
|
return
|
|
}
|
|
// 绑定第一个 managed 数据源(通常是唯一部署)
|
|
target := sources[0]
|
|
|
|
// 内置规则一律挂到默认通知通道。
|
|
// 首次部署没有任何通道时,这里建一个**禁用**的占位通道(Webhook 留空):
|
|
// - 规则不会变成"孤儿"(发送历史会明确写"通道未投递"而不是静默)
|
|
// - 管理员填好 Webhook 再启用即可,不必逐条给规则关联通道
|
|
// - 之所以不放"已启用 + 空地址":那样每次命中都会投递失败刷屏
|
|
// 这里直接操作 db 而不调 services,避免 seed ↔ services 的包循环依赖。
|
|
defaultChannelID := ensurePlaceholderChannel(db)
|
|
|
|
newID := db.NextID("alertRules")
|
|
db.Lock()
|
|
defer db.Unlock()
|
|
for _, r := range builtinRules {
|
|
if existing[r.name] {
|
|
continue
|
|
}
|
|
newID++
|
|
db.AlertRules = append(db.AlertRules, models.AlertRule{
|
|
ID: newID, Name: r.name, SourceID: target.id,
|
|
Query: r.query, WindowMin: r.windowMin, Threshold: r.threshold,
|
|
Enabled: false, CooldownMin: r.cooldown,
|
|
ChannelIDs: channelIDsFor(defaultChannelID),
|
|
CreatedAt: time.Now(), UpdatedAt: time.Now(),
|
|
})
|
|
}
|
|
db.RequestSave()
|
|
}
|