perf(analysis): 分析页按索引/内容分层,告警默认阈值抬高

=== 1. 分析页"一直转圈/请求超时(40000ms)"的真实原因 ===
三个问题叠加,逐个量化:

a) 首屏用空 namespace 发查询
   页面初始 namespace='',等策略返回前就发出 `{namespace=~".+"}` 全集群查询,
   策略到位后又查一次 → 一次访问跑两遍。
   → 加 namespaceReady 门控:解析完再发(effectiveSourceId)。

b) histogram 索引查询和内容查询抢同一个信号量
   /logs/histogram 首选 index/volume_range(毫秒级),但仍要先抢重型令牌,
   而令牌被 4 条 count_over_time(各 14~29s)占满 → 排队/429/超时。
   → 索引路径**不再占用重型令牌**(15s 独立超时),只有回退到
     count_over_time 时才抢令牌。实测各跨度(15m/1h/6h/24h)均 0s。

c) 重查询条数过多
   原来 4 条全局 count_over_time(total/error/warn)+ 2 条 by-app,
   串行 1~2 分钟。现在只留 2 条 by-app:
     - 全局总量已由 histogram(索引)提供
     - 全局 error 由 errByApp 序列求和得出
     - 全局 warn 放弃(优先级最低却要 20s)
   实测 by-app 单条 26s,两条串行 ~52s;主图 0s 先出。

d) 日报 7 天 count_over_time(2.6TB 扫描)必然失败
   → 改走 /logs/histogram(索引)按自然日聚合,只出"每日总量",
     应用维度交给下方 Top 应用表格(有自己的时间范围)。
   实测 7 天跨度毫秒级。

=== 2. 告警默认阈值抬高(告警疲劳治理)===
验证时用 `{namespace="astralx"}` + 阈值 0 触发了一次:
15 分钟命中 3051 万条 —— 阈值 0 等于把告警变成日志流复读机。

新基线(15 分钟窗口,冷却统一 120 分钟):
    致命日志 FATAL           0 → 10
    Go panic                 0 → 10
    JVM OutOfMemoryError     0 → 5
    StackOverflowError       0 → 5
    应用错误激增             1000 → 50000(改名,原"error > 1000"名不副实)
    ingress-nginx 5xx         100 → 500
    ingress-nginx 4xx        1000 → 5000
    数据库连接失败             50 → 200

迁移只改"仍等于旧默认值"的规则,用户手动调过的阈值不动。
设计原则写进 seed 注释:绝不用 threshold 0;宁可少报也不要让人对告警脱敏;
绝对阈值不跨环境通用,大集群必须按 app/namespace 收窄。

=== 实测(v6-1028-2218 / 0bf66199de66)===
  规则 8 条,阈值=0 的规则数 0,冷却全部 120m
  histogram astralx(15m/1h/6h/24h): 0s
  by-app metrics astralx 15m: 26s,正常返回数据
This commit is contained in:
opsx committed 2026-10-28 22:20:19 +00:00
1 parent 95dcd9408c
commit fa7ddd67c1
6 files changed
+161 -77

No files matched your search

@@ -56,55 +56,42 @@ export function useDailyVolume({ sourceId, namespace, appLabel }: {
setLoading(true)
setError('')
try {
const series = await logsApi.metrics(
{ sourceId, start: Number(ns(start)), end: Number(ns(end)), query: `sum by (${appLabel}, namespace) (count_over_time(${selector}[$step]))` },
// 走索引接口而不是 count_over_time。
//
// 原来这条查 7 天 × count_over_time,实测该环境约 370GB/天 → 要扫 2.6TB,
// 后端 90s 也跑不完,图表永远空/超时。
// /logs/histogram 内部用 index/volume_range,只读 chunk 元数据,
// 7 天跨度也是毫秒级;代价是**只能拿到总量,拿不到 per-app 拆分**
// (Loki 的索引接口不支持按任意 label 分组)。
// 因此这里展示"每日日志总量",应用维度交给下方的 Top 应用表格(它有单独的时间范围)。
const hist = await logsApi.histogram(
{ sourceId, query: selector, start: Number(ns(start)), end: Number(ns(end)) },
{ signal: controller.signal },
)
if (version !== versionRef.current) return
// 小时桶 → 本地日历日分桶(app × 天 累加,并记录各 app 落在哪个命名空间)
const perDay = days.map(() => new Map<string, { total: number; perNs: Map<string, number> }>())
const dayIndexOf = (timestamp: string) => {
const day = Math.floor(new Date(timestamp).getTime() / DAY_MS) * DAY_MS
return days.indexOf(day)
// 索引返回的是字节数,按本地自然日聚合
const dayBytes = days.map(() => 0)
for (const bucket of (hist || [])) {
const ts = String((bucket as { timestamp: string }).timestamp ?? '')
const value = Number((bucket as { count: number }).count ?? 0)
const day = Math.floor(new Date(ts).getTime() / DAY_MS) * DAY_MS
const index = days.indexOf(day)
if (index >= 0) dayBytes[index] += value
}
for (const item of series) {
const app = item.labels[appLabel] ?? ''
const nsLabel = item.labels.namespace ?? ''
for (const point of item.points) {
const index = dayIndexOf(point.timestamp)
if (index < 0) continue
const bucket = perDay[index]
const entry = bucket.get(app) ?? { total: 0, perNs: new Map<string, number>() }
entry.total += point.value
entry.perNs.set(nsLabel, (entry.perNs.get(nsLabel) ?? 0) + point.value)
bucket.set(app, entry)
}
}
const merged = new Map<string, { totals: number[]; total: number; perNs: Map<string, number> }>()
perDay.forEach((bucket, index) => {
for (const [app, entry] of bucket) {
const row = merged.get(app) ?? { totals: days.map(() => 0), total: 0, perNs: new Map<string, number>() }
row.totals[index] += entry.total
row.total += entry.total
for (const [nsName, count] of entry.perNs) row.perNs.set(nsName, (row.perNs.get(nsName) ?? 0) + count)
merged.set(app, row)
}
setData({
days: days.map(dayLabel),
apps: [{
app: '全部应用',
namespace: namespace || '',
totals: dayBytes,
total: dayBytes.reduce((sum, value) => sum + value, 0),
}],
})
const apps: DailyAppRow[] = [...merged.entries()]
.map(([app, row]) => ({
app,
namespace: [...row.perNs.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? '',
totals: row.totals,
total: row.total,
}))
.filter(row => row.total > 0)
.sort((a, b) => b.total - a.total)
.slice(0, 10)
setData({ days: days.map(dayLabel), apps })
return
} catch (err) {
if (version !== versionRef.current) return
if ((err as Error).name === 'AbortError') return
setError(err instanceof Error ? err.message : 'metrics failed')
if (version === versionRef.current) setError(err instanceof Error ? err.message : 'daily volume failed')
} finally {
if (version === versionRef.current) setLoading(false)
}