authoring-log-alerts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAuthoring log alerts
创建日志告警
Authoring an alert is a measurement problem, not a guessing problem. You are not trying to be exhaustive — you
are trying to land thresholds that fire 0–3 times per week on real production patterns, on services that matter.
创建告警是一个量化问题,而非猜测问题。无需追求面面俱到,只需针对重要服务设定符合真实生产模式的阈值,确保每周触发0-3次告警即可。
When to use this skill
何时使用此技能
- The user asks to "set up alerts" / "suggest alerts" for their project.
- The user wants to evaluate whether a service is producing alertable signal.
- The user has just enabled log alerting and wants a starter set.
- 用户要求为其项目“设置告警”/“建议告警”时。
- 用户希望评估某项服务是否产生可告警的信号时。
- 用户刚启用日志告警功能,需要一套初始配置时。
When not to use this skill
何时不使用此技能
- Tuning an alert that already exists — that's a different job (use to inspect fire/resolve cadence and
posthog:logs-alerts-events-listto adjust).posthog:logs-alerts-partial-update - Investigating an active incident — pull rows with , don't author an alert mid-incident.
posthog:query-logs
- 调整已存在的告警——这是另一项任务(使用查看告警触发/恢复频率,使用
posthog:logs-alerts-events-list进行调整)。posthog:logs-alerts-partial-update - 调查活跃事件——使用提取日志行,不要在事件处理过程中创建告警。
posthog:query-logs
Tools
工具
| Tool | Job | Where it fits |
|---|---|---|
| Top-25 services in window with log_count, error_count, error_rate, sparkline. | Step 1 — triage. |
| Discover keys/values for narrower filters. | Step 2, optional. |
| Adaptive time-bucketed counts for a filter. | Step 3 — baseline. |
| Replay a draft config against | Step 4 — validate. |
| Persist the alert. | Step 5 — ship. |
| Wire the alert to Slack or webhook. | Step 5 — ship. |
Do not call during authoring. You need distributions, not rows. Reserve for
the very end if the user asks "show me a sample of what would have fired" — is plenty.
posthog:query-logsposthog:query-logslimit: 10| 工具 | 用途 | 适用步骤 |
|---|---|---|
| 返回指定时间窗口内的前25个服务,包含log_count、error_count、error_rate和趋势图。 | 步骤1 — 分类排查。 |
| 发现用于缩小过滤范围的键/值。 | 步骤2(可选)。 |
| 针对过滤条件返回自适应时间桶的计数数据。 | 步骤3 — 基线分析。 |
| 使用完整状态机,基于过去7天的历史数据回放草稿配置。 | 步骤4 — 验证。 |
| 保存告警配置。 | 步骤5 — 上线。 |
| 将告警关联至Slack或webhook。 | 步骤5 — 上线。 |
创建告警过程中不要调用。你需要的是分布数据,而非日志行。仅当用户询问“展示一些本应触发告警的示例”时,才在最后调用该工具——设置足够。
posthog:query-logslimit: 10Workflow
工作流程
1. Triage — pick candidate services
1. 分类排查——挑选候选服务
Call for the last 24h with no filters. The response is capped at 25 services and includes a
sparkline, so it is small and bounded.
posthog:logs-servicesA service is a candidate when both are true:
- is non-trivial (≥ ~1k in 24h — quieter services produce too little signal to alert on).
log_count - is non-zero, or the user has named the service explicitly.
error_rate
Skip services with high volume but unless the user wants a volume-shape alert (e.g. "warn me
if api-gateway suddenly stops producing logs"). Volume-floor alerts use and need
different reasoning — see references/volume-floor-alerts.md.
error_rate == 0threshold_operator: belowIf the user names a service, treat it as a candidate even without error signal.
调用获取过去24小时无过滤条件的数据。响应结果最多包含25个服务,且带有趋势图,数据量小且可控。
posthog:logs-services满足以下两个条件的服务可作为候选:
- 达到一定规模(24小时内≥约1000条——流量过低的服务信号不足,不适合告警)。
log_count - 不为0,或用户明确指定该服务。
error_rate
除非用户需要流量形态告警(例如“如果api-gateway突然停止生成日志,请提醒我”),否则跳过流量高但的服务。流量下限告警需使用,且逻辑不同——详见references/volume-floor-alerts.md。
error_rate == 0threshold_operator: below如果用户指定了某服务,即使没有错误信号也将其视为候选。
2. (Optional) Narrow the filter
2.(可选)缩小过滤范围
If a service has many error sub-types, an alert on "all errors" is usually too broad. Use
(try ) and to find a discriminator —
common ones are , , . Add the narrowing filter to your draft.
posthog:logs-attributes-listattribute_type: logposthog:logs-attribute-values-listhttp.status_codeerror.typek8s.container.nameKeep it simple: one severity filter + one or two attribute filters is plenty. Multi-clause filters are
harder to reason about and rarely improve precision.
如果某服务存在多种错误子类型,针对“所有错误”的告警通常范围过宽。使用(尝试设置)和找到区分条件——常见的有、、。将缩小后的过滤条件添加至草稿。
posthog:logs-attributes-listattribute_type: logposthog:logs-attribute-values-listhttp.status_codeerror.typek8s.container.name保持简洁:一个严重性过滤条件+一到两个属性过滤条件足够。多条件过滤更难理解,且很少能提升精度。
3. Baseline — characterise the candidate over 7 days
3. 基线分析——分析候选服务过去7天的特征
Call with the candidate's filters, , and
(one bucket ≈ 7h). The response gives you bucket counts.
posthog:logs-count-rangesdateRange: { date_from: "-7d" }targetBuckets: 24Do not eyeball the percentiles or scale the threshold to the alert window manually. Pipe the
count-ranges response into the helper script:
bash
echo '<count-ranges JSON>' | python3 scripts/baseline_stats.py --window-minutes 5The script returns:
json
{
"n_buckets": 12,
"bucket_minutes": 420.0,
"alert_window_minutes": 5,
"stats": { "p50": 12.0, "p95": 71.25, "p99": 126.25, "max": 140 },
"suggested_threshold_count": 5,
"rationale": "max(p99=126.25, median*3=36.0, floor=5) scaled from 420m bucket to 5m window",
"health": []
}Use as your starting threshold. Read :
suggested_threshold_counthealth | What it means | What to do |
|---|---|---|
| Too few non-empty buckets for a 7d baseline. | Widen filter, extend to |
| All buckets are zero. | Skip — no signal. |
| | Count-threshold alerts work well. Proceed. |
| | Be cautious — either no incidents in lookback, or the metric is too smooth. Try a longer lookback or skip. |
| Healthy distribution. | Proceed. |
调用,传入候选服务的过滤条件、和(每个时间桶≈7小时)。响应结果会给出各时间桶的计数。
posthog:logs-count-rangesdateRange: { date_from: "-7d" }targetBuckets: 24**不要手动估算百分位数或按告警窗口缩放阈值。**将count-ranges的响应传入辅助脚本:
bash
echo '<count-ranges JSON>' | python3 scripts/baseline_stats.py --window-minutes 5脚本返回结果示例:
json
{
"n_buckets": 12,
"bucket_minutes": 420.0,
"alert_window_minutes": 5,
"stats": { "p50": 12.0, "p95": 71.25, "p99": 126.25, "max": 140 },
"suggested_threshold_count": 5,
"rationale": "max(p99=126.25, median*3=36.0, floor=5) scaled from 420m bucket to 5m window",
"health": []
}将作为初始阈值。查看字段:
suggested_threshold_counthealth | 含义 | 处理方式 |
|---|---|---|
| 7天基线中非空时间桶过少。 | 扩大过滤范围、延长至 |
| 所有时间桶计数均为0。 | 跳过——无有效信号。 |
| | 基于计数的阈值告警效果良好,继续执行。 |
| | 需谨慎——要么回溯期内无事件,要么指标过于平稳。尝试延长回溯期或跳过。 |
| 数据分布健康。 | 继续执行。 |
4. Draft and simulate
4. 草拟并模拟
Pick a starter draft from these defaults — see references/threshold-defaults.md
for the reasoning:
| Setting | Default | Notes |
|---|---|---|
| | Already scaled to the alert window. |
| | Use |
| | Allowed: 5, 10, 15, 30, 60. Must match what you passed to the script. |
| | M in N-of-M. |
| | N in N-of-M. 2-of-3 reduces flap from a single noisy bucket. |
| | Minimum time between repeat fires. |
Call with these settings and . The response gives you
and .
posthog:logs-alerts-simulate-createdate_from: "-7d"fire_countresolve_count从以下默认配置中选择初始草稿——详见references/threshold-defaults.md中的设计思路:
| 设置项 | 默认值 | 说明 |
|---|---|---|
| 脚本返回的 | 已按告警窗口缩放。 |
| | 仅流量下限告警使用 |
| | 允许值:5、10、15、30、60。必须与传入脚本的参数一致。 |
| | N-of-M中的M值。 |
| | N-of-M中的N值。2-of-3可减少单个噪声时间桶导致的告警波动。 |
| | 重复触发告警的最小间隔时间。 |
调用,传入上述配置和。响应结果会给出和。
posthog:logs-alerts-simulate-createdate_from: "-7d"fire_countresolve_count5. Iterate — three rounds, then ship or skip
5. 迭代——最多三轮,然后上线或跳过
Target: between 0 and ~3 over . If outside the band:
fire_count-7d| Outcome | Adjustment |
|---|---|
| Lower |
| The service has no alertable signal. Skip it; log why. |
| Raise |
| Cooldown is too long, or the underlying state is genuinely sticky. Acceptable for now. |
When adjusting the threshold, read values from the script's block — never recompute percentiles
by hand.
statsCap iteration at 3 simulate calls per candidate. If you can't land in the band in 3 rounds, the metric
is wrong — either the filter is too broad, the window is wrong, or the service genuinely doesn't have a
threshold-shape signal. Note it and move on.
目标:过去7天内在0到约3之间。若超出范围:
fire_count| 结果 | 调整方案 |
|---|---|
7天内 | 将 |
| 该服务无有效告警信号,跳过并记录原因。 |
| 将 |
| 冷却时间过长,或底层状态持续异常。暂时可接受。 |
调整阈值时,务必从脚本的块中读取数值——切勿手动重新计算百分位数。
stats每个候选服务最多进行3次simulate调用。若3轮后仍无法达到目标范围,说明指标存在问题——要么过滤范围过宽,要么窗口设置错误,要么该服务确实没有符合阈值形态的信号。记录问题并继续处理其他服务。
6. Ship — create + attach destination
6. 上线——创建告警+关联通知目标
Once a draft simulates cleanly:
- Call with the validated config. Use a name like
posthog:logs-alerts-createso the user can see at a glance which alerts came from this skill.<service> error rate (auto) - Call to wire it to a notification target. An alert with no destination is silent. Always confirm the channel name or webhook URL with the user before attaching — never wire an auto-generated alert to a production channel without explicit confirmation. If the user is unsure, suggest a low-traffic testing channel for the first few alerts.
posthog:logs-alerts-destinations-create
If the user wants alerts created in state for review-then-flip, pass to
and tell them how many drafts you produced.
enabled: falseenabled: false-create草稿模拟验证通过后:
- 调用传入验证后的配置。命名格式建议为
posthog:logs-alerts-create,方便用户一眼识别哪些告警是通过此技能创建的。<service> error rate (auto) - 调用关联通知目标。**无通知目标的告警是静默的。**关联前务必与用户确认渠道名称或webhook URL——切勿未经明确确认就将自动生成的告警关联至生产渠道。若用户不确定,建议先关联低流量的测试渠道。
posthog:logs-alerts-destinations-create
如果用户希望创建的告警处于状态,以便先审核再启用,可在接口中传入,并告知用户已生成的草稿数量。
enabled: false-createenabled: falseFilter shape — required
过滤条件格式——必填
The field on takes a subset of and must contain at
least one of:
filtersposthog:logs-alerts-createLogsViewerFilters- — list of
severityLevels["trace","debug","info","warn","error","fatal"] - — list of service name strings
serviceNames - — property filter group
filterGroup
The same shape goes into 's field. Match the simulate filters to the alert filters
exactly — otherwise the simulation is testing a different alert than the one you ship.
posthog:logs-alerts-simulate-createfiltersExample minimum:
json
{
"severityLevels": ["error", "fatal"],
"serviceNames": ["api-gateway"]
}posthog:logs-alerts-createfiltersLogsViewerFilters- —
severityLevels列表["trace","debug","info","warn","error","fatal"] - — 服务名称字符串列表
serviceNames - — 属性过滤组
filterGroup
posthog:logs-alerts-simulate-createfilters最简示例:
json
{
"severityLevels": ["error", "fatal"],
"serviceNames": ["api-gateway"]
}Token-economy rules
调用次数规则
- One call at the start, not per-candidate.
posthog:logs-services - One call per candidate at
posthog:logs-count-ranges. Don't go above 30 during authoring.targetBuckets: 24 - ≤ 3 calls per candidate.
posthog:logs-alerts-simulate-create - Zero calls during the authoring loop.
posthog:query-logs - Prefer reporting a small set of well-validated alerts over a long list of unvalidated drafts.
- 仅在开始时调用一次,而非每个候选服务都调用。
posthog:logs-services - 每个候选服务调用一次,设置
posthog:logs-count-ranges。创建告警过程中不要超过30。targetBuckets: 24 - 每个候选服务最多调用3次。
posthog:logs-alerts-simulate-create - 创建告警流程中不调用。
posthog:query-logs - 优先报告少量经过充分验证的告警,而非大量未验证的草稿。
Output
输出内容
Report what you did, in this shape:
- For each shipped alert: name, filters, threshold, simulated fire_count over 7d, destination.
- For each skipped candidate: service name + why (flat baseline, can't land threshold, low volume).
- Total simulate calls made, total alerts created.
The user should be able to read this and decide whether to disable any drafts before they go live.
按以下格式报告操作结果:
- 每个上线告警:名称、过滤条件、阈值、过去7天模拟触发次数、关联的通知目标。
- 每个跳过的候选服务:服务名称+原因(基线平稳、无法设定合适阈值、流量过低)。
- 总模拟调用次数、总创建告警数量。
用户应能通过此报告决定是否在告警生效前禁用某些草稿。