Skip to content

告警升级与人工确认 ​

目标:一条告警进来,先发钉钉;10 分钟内没人确认就发短信;最多升三轮——每一轮都能在运行记录里看到「正在等待」。 前置条件:Herald 已启动,$BASE / $TOKEN 就绪,ch-dingtalk 与 ch-sms 两个渠道已建好(建法见写出第一个工作流)。

1. 先把「等确认」的边界说清楚 ​

wait-ack 节点(等待人工确认、超时再升级)尚未实现——flow.Validate 会直接拒绝 节点类型 wait-ack 未实现(M4c/后续)。所以「等确认」在今天只能拆成两件确定的事:

想做今天能做的
等一个窗口delay(等 10 分钟)或 wait-until(等到 09:00 这种钟点)
判断「确认了没有」读触发时就固定的输入:payload / variables / params
运行中途收到确认并停下做不到:脚本沙箱没有网络,variables 在运行开始就固化,也没有取消运行的管理接口

所以本文的语义是:窗口结束仍未确认 → 继续升级,最多三轮;variables.ack=true 的告警(触发时已确认)直接跳过升级。要把「人工确认」变成真正的停止条件,等 wait-ack,或用外部系统把确认做成另一条独立流程。

2. 升级链的完整 DSL ​

json
{
  "apiVersion": "herald/v1alpha1",
  "kind": "Pipeline",
  "metadata": { "id": "r-escalate", "name": "告警升级" },
  "spec": {
    "trigger": { "type": "api" },
    "limits": { "deadline": "2h", "maxLoopIterations": 5 },
    "nodes": [
      { "id": "c-urgent", "type": "condition", "with": { "expr": "priority == 'urgent' || payload.severity == 'P1'" } },
      { "id": "s-notice", "type": "send", "with": { "channel": "ch-dingtalk", "template": "【提醒】{{ .payload.title }}" } },
      { "id": "l-escalate", "type": "loop", "with": { "over": "[1,2,3]", "maxIterations": 3, "body": ["c-ack", "br-ack", "s-im", "d-wait", "s-sms"] } },
      { "id": "c-ack", "type": "condition", "with": { "expr": "variables.ack == 'true'" } },
      { "id": "br-ack", "type": "break" },
      { "id": "s-im", "type": "send", "with": { "channel": "ch-dingtalk", "await": true,
          "template": "【升级第 {{ .item }} 轮】{{ .payload.title }}:10 分钟内未确认将发短信" } },
      { "id": "d-wait", "type": "delay", "with": { "duration": "10m" } },
      { "id": "s-sms", "type": "send", "with": { "channel": "ch-sms", "target": "13800000000", "await": true,
          "template": "【未确认·第 {{ .item }} 轮】{{ .payload.title }}" } },
      { "id": "eh", "type": "error-handler" }
    ],
    "edges": [
      { "from": "c-urgent", "to": "l-escalate", "when": "true" },
      { "from": "c-urgent", "to": "s-notice", "when": "false" },
      { "from": "l-escalate", "to": "c-ack", "when": "body" },
      { "from": "c-ack", "to": "br-ack", "when": "true" },
      { "from": "c-ack", "to": "s-im", "when": "false" },
      { "from": "s-im", "to": "d-wait" },
      { "from": "d-wait", "to": "s-sms" },
      { "from": "s-im", "to": "eh", "when": "error" },
      { "from": "s-sms", "to": "eh", "when": "error" },
      { "from": "l-escalate", "to": "eh", "when": "error" },
      { "from": "s-notice", "to": "eh", "when": "error" }
    ]
  }
}

send 的 template 只管节点输出

send.with.template 的渲染结果只写进节点输出的 text(用于运行记录与预览),不改变实际发出的内容——出站内容是消息里的 payload。要改写它,用 script 的 apply 或直接在 notify 的 payload 里给。

读法:loop 用 over: "[1,2,3]" 迭代三次,每轮先判断是否已确认(c-ack),未确认就走 s-im → d-wait → s-sms——钉钉、等 10 分钟、短信。br-ack 只在真确认时触发,循环以 {iteration:1, broken:true} 提前收工。这里用 delay 表达「等 10 分钟」;等某个钟点的写法(比如非紧急提醒留到早上 9 点)见第 6 节变体 B 的 wait-until。

3. 每一轮的输出形态 ​

节点输出说明
c-ack{"result": false}每轮都有一条记录(iteration 0/1/2)
s-im{"delivery_id":"out_…","external_id":"…","text":"渲染后的文案"}await: true,等到投递终态才有 external_id
d-wait挂起时 {"duration":"10m","wake_at_ms":…};唤醒后同一条记录转 succeeded断点续跑锚点
s-sms{"delivery_id":"out_…","external_id":"…","text":…}同上
br-ack{"break":true} 或 {"break":false}只有 true 才结束整个循环
l-escalate运行中 {"iteration":N};结束 {"iteration":3} 或 {"iteration":k,"broken":true}循环自己的锚点

4. 跑起来 ​

bash
{ printf '{"id":"r-escalate","name":"告警升级","enabled":true,"workflow":'; cat esc.json; printf '}'; } > esc-route.json

curl -sS -X POST "$BASE/v1/admin/routes" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d @esc-route.json

curl -sS -X POST "$BASE/v1/notify" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"route_id":"r-escalate","idempotency_key":"esc-1","priority":"urgent","payload":{"title":"支付成功率跌破 90%"}}'

esc.json 就是上一节的 DSL。发出去后立刻查运行:

bash
curl -sS "$BASE/v1/admin/routes/r-escalate/runs" -H "Authorization: Bearer $TOKEN"
# → {"items":[{"id":"run_…","status":"waiting","wake_at":"2026-11-28T16:52:00+08:00", …}], …}

正在等待在运行记录里有两层证据:run.status=waiting 且 run.wake_at 有值;详情里对应节点也是 waiting:

bash
curl -sS "$BASE/v1/admin/runs/run_…" -H "Authorization: Bearer $TOKEN"
json
[
  { "node_id": "l-escalate", "status": "waiting", "iteration": -1, "output": { "iteration": 0 } },
  { "node_id": "c-ack", "status": "succeeded", "iteration": 0, "output": { "result": false } },
  { "node_id": "s-im", "status": "succeeded", "iteration": 0, "output": { "delivery_id": "out_…", "external_id": "…" } },
  { "node_id": "d-wait", "status": "waiting", "iteration": 0, "output": { "duration": "10m", "wake_at_ms": 1764321120000 } }
]

到期后 worker 唤醒续跑,d-wait 变 succeeded,接着发短信、进下一轮。三轮走完 run.status 变 succeeded,l-escalate 的输出是 {"iteration":3}。

想快点验证

把 d-wait 的 "duration": "10m" 临时改成 "30s",整套流程一分半就能走完;确认接线无误再改回 10m。改完 PUT 一次即可,在途运行用的是旧快照。

5. 放大 limits.deadline 覆盖等待 ​

运行的总预算 = run.created_at + limits.deadline,缺省只有 10 分钟。本工作流光等待就要 3 × 10 = 30 分钟,所以必须显式放大,否则节点失败:

  • delay 超预算:延迟 10m 唤醒时间超出运行 deadline
  • wait-until 超预算:等待到 2026-11-29T09:00:00+08:00 超出运行 deadline(limits.deadline 需覆盖等待时长)

本例写 "deadline": "2h":够 3 轮窗口,也留出投递重试的余量。同时用 "maxLoopIterations": 5 把循环上限收紧到比缺省 100 更小的值(DSL 只能收紧不能放宽)。

6. 变体:白天/夜间用不同升级间隔 ​

夜间不必每 10 分钟叫一次人。两种改法,可叠加。

变体 A:白天 5 分钟、夜间 30 分钟。 把 d-wait 换成一个条件加两个 delay,按 payload 里的 night 选择(这份 spec 可整份替换第 2 节的那份):

json
{
  "trigger": { "type": "api" },
  "limits": { "deadline": "2h", "maxLoopIterations": 5 },
  "nodes": [
    { "id": "c-urgent", "type": "condition", "with": { "expr": "priority == 'urgent' || payload.severity == 'P1'" } },
    { "id": "s-notice", "type": "send", "with": { "channel": "ch-dingtalk", "template": "【提醒】{{ .payload.title }}" } },
    { "id": "l-escalate", "type": "loop", "with": { "over": "[1,2,3]", "maxIterations": 3,
        "body": ["c-ack", "br-ack", "s-im", "c-night", "d-long", "d-short", "s-sms"] } },
    { "id": "c-ack", "type": "condition", "with": { "expr": "variables.ack == 'true'" } },
    { "id": "br-ack", "type": "break" },
    { "id": "s-im", "type": "send", "with": { "channel": "ch-dingtalk", "await": true,
        "template": "【升级第 {{ .item }} 轮】{{ .payload.title }}" } },
    { "id": "c-night", "type": "condition", "with": { "expr": "payload.night == true" } },
    { "id": "d-long", "type": "delay", "with": { "duration": "30m" } },
    { "id": "d-short", "type": "delay", "with": { "duration": "5m" } },
    { "id": "s-sms", "type": "send", "with": { "channel": "ch-sms", "target": "13800000000", "await": true,
        "template": "【未确认·第 {{ .item }} 轮】{{ .payload.title }}" } },
    { "id": "eh", "type": "error-handler" }
  ],
  "edges": [
    { "from": "c-urgent", "to": "l-escalate", "when": "true" },
    { "from": "c-urgent", "to": "s-notice", "when": "false" },
    { "from": "l-escalate", "to": "c-ack", "when": "body" },
    { "from": "c-ack", "to": "br-ack", "when": "true" },
    { "from": "c-ack", "to": "s-im", "when": "false" },
    { "from": "s-im", "to": "c-night" },
    { "from": "c-night", "to": "d-long", "when": "true" },
    { "from": "c-night", "to": "d-short", "when": "false" },
    { "from": "d-long", "to": "s-sms" },
    { "from": "d-short", "to": "s-sms" },
    { "from": "s-im", "to": "eh", "when": "error" },
    { "from": "s-sms", "to": "eh", "when": "error" },
    { "from": "l-escalate", "to": "eh", "when": "error" },
    { "from": "s-notice", "to": "eh", "when": "error" }
  ]
}

最坏情况是 3 × 30 分钟,"deadline": "2h" 仍然够;夜间 30 分钟只是把「叫醒人」的频率降下来。

变体 B:非紧急提醒攒到早上 9 点。 在第 2 节的主 DSL 上改三处:nodes 加一个 {"id":"w-morning","type":"wait-until","with":{"daily":"09:00","timezone":"Asia/Shanghai"}};删掉 c-urgent → s-notice 那条边;补上 {"from":"c-urgent","to":"w-morning","when":"false"} 与 {"from":"w-morning","to":"s-notice"},并把 limits.deadline 放到 "24h"。daily 的语义正好是「下一个该时刻」:已过或恰好等于当前时刻就取明天,所以白天触发的非紧急提醒会留到明早 9 点。

别把 daily 的 wait-until 放进循环

wait-until.daily 每轮都会重新计算「下一个 HH:MM」。第一轮等到 9:00 之后,第二轮的目标就成了明天 9:00——三轮循环会拖成三天。循环里的间隔用 delay,把 daily 留在循环外。

7. 几个必须知道的坑 ​

  • 升级链上的 send 一定要 await: true:不带 await 的 send 只把投递任务展开进 outbox 就成功返回,之后对端的失败不会触发 error 边——升级链会「看起来升级了,其实一条没发出去」。
  • 等待中的投递不产生节点失败:await 节点每 2 秒轮询一次投递终态,投递还在重试时节点保持 waiting;只有 dead 才失败并给出 投递 out_… 死信: …。
  • 循环体内的失败被兜底吸收后,本轮结束、进下一轮:s-im 失败走 eh 后,本迭代的 d-wait / s-sms 不再执行;兜底分支自己也失败 = 双重故障,循环直接失败(不吞)。
  • break 只能在循环体或兜底分支内,且不能有出边:它一跳出,循环立即以「已跑轮数 + broken」成功收工,done 线继续往下走。
  • send.with.template 不决定对方收到的内容:它只渲染进节点输出(运行记录里可见);出站内容是消息 payload。想让短信带上「第几轮」,在 s-sms 前加 script 节点用 apply: true 改写 payload.content。
  • 节点记录按(节点, 迭代)落账:断点续跑以记录为准,已成功的节点不重发;所以「运行中途改了工作流」不影响在途运行,它用的仍是入队时的 DSL 快照。

如果卡住了 ​

  • 保存报 break 节点 … 必须位于 loop 的循环体或兜底分支内:把该节点加进 with.body,并让它的入边来自 body 成员。
  • 保存报 loop … 的 body 未闭合: 存在跨界边 …:body 成员的边只能指向成员或回连 loop,兜底边只能指向 error-handler。
  • 运行一直 waiting、wake_at 却不前进:看节点记录的 wake_at_ms 是否已过;再检查 worker 是否在跑(queue.workers ≥ 1)。
  • 运行 failed 且错误是「超出运行 deadline」:按第 5 节放大 limits.deadline。
  • 只发了一轮就 succeeded:多半是 br-ack 被无条件触发(写成 condition 误连到 break),或 over 求值成了单元素列表。

完整字段与边界见 loop、break、delay、wait-until、error-handler;找运行记录的方法见排障。

基于 Apache License 2.0 发布