利用 ML 结果触发 Slack 告警的 Watcher 示例
解读
在国内一线互联网与金融企业的生产环境中,Watcher 作为 Elasticsearch 官方商业功能(X-Pack 的一部分),负责把“异常检测结果”转化为“可行动的告警”。本题要求候选人把 ML 异常检测作业(Anomaly Detection Job) 的输出作为触发源,通过 Slack Webhook 把关键信息(实体、严重性、业务含义)推送到国内可访问的 Slack 频道或企业微信转 Slack 的代理网关。
面试官重点考察:
- 是否理解 ML 结果索引 .ml-anomalies- 的字段语义*(record_score、by_field_value、influencers)。
- 能否写出 Chain Input + Search Input + Condition + Action 的完整 Watcher JSON,并解决“国内网络无法直连 Slack”的落地细节(代理、白名单、加密)。
- 是否具备 告警风暴抑制(throttle)与 分级通知(P1/P2/P3)意识,避免半夜轰炸值班手机。
知识点
- ML 结果索引结构:
.ml-anomalies-shared中result_type: record代表具体异常点;record_score ≥ 75通常为“高危”。by_field_value/over_field_value标识异常实体(如 IP、UID、商户号)。
- Watcher 触发周期:
metadata.interval一般与 ML bucket_span 对齐(国内常用 5 min 或 15 min)。 - Slack Webhook 国内可用性:
- 需走 企业代理(如 nginx 正向代理)或 Elastic Cloud 私有环境;
- 若公司禁用 Slack,可替换为 钉钉自定义机器人(只需把
webhook.url换成钉钉地址,body 加msgtype: markdown)。
- 权限与加密:
- Watcher 存储的
webhook.url必须走 Keystore(bin/elasticsearch-keystore add xpack.notification.slack.account.myteam.secure_url),避免明文泄露。
- Watcher 存储的
- 告警去重:利用
throttle_period_in_millis = 30*60*1000实现 30 分钟同实体静默。
答案
以下示例假设:
- ML 作业 ID:
payment_delay_1h - bucket_span:15 min
- 只关注 record_score ≥ 75 且 影响字段为 merchant_id 的异常
- Slack 代理网关:
https://slack-proxy.corp.example.com/hooks/123 - 国内时区 UTC+8
PUT _watcher/watch/payment_delay_slack
{
"metadata": {
"interval": "15m",
"job_id": "payment_delay_1h",
"score_threshold": 75
},
"trigger": {
"schedule": {
"interval": "15m"
}
},
"input": {
"chain": {
"inputs": [
{
"ml": {
"search": {
"request": {
"indices": [".ml-anomalies-shared"],
"body": {
"size": 10,
"query": {
"bool": {
"filter": [
{ "term": { "job_id": "{{ctx.metadata.job_id}}" } },
{ "term": { "result_type": "record" } },
{ "range": { "timestamp": { "gte": "now-15m" } } },
{ "range": { "record_score": { "gte": "{{ctx.metadata.score_threshold}}" } } }
]
}
},
"sort": [{ "record_score": { "order": "desc" } }]
}
}
}
}
}
]
}
},
"condition": {
"compare": { "ctx.payload.ml.hits.total": { "gt": 0 } }
},
"transform": {
"script": {
"source": """
return ctx.payload.ml.hits.hits.stream().map(h -> [
'merchant': h._source.by_field_value,
'score': h._source.record_score,
'actual': h._source.actual,
'typical': h._source.typical,
'time': h._source.timestamp
]).collect(Collectors.toList());
""",
"lang": "painless"
}
},
"actions": {
"notify_slack": {
"throttle_period_in_millis": 1800000,
"webhook": {
"url": "{{ctx.metadata.slack_webhook}}",
"method": "POST",
"headers": { "Content-Type": "application/json" },
"body": {
"text": ":red_circle: **支付延迟异常**(评分≥75)",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*商户*: {{#ctx.payload._value}}{{merchant}}{{/ctx.payload._value}} \n*实际延迟*: {{actual}} ms \n*典型值*: {{typical}} ms \n*异常时间*: {{time}} \n<https://kibana.corp.example.com/app/ml/explorer?_g=(time:(from:now-1h,to:now))|查看详情>"
}
}
]
}
}
}
}
}
关键配置说明:
- Chain Input 先查 ML 结果,避免多余聚合。
- Transform 把 hits 数组转成扁平 Map,方便 Mustache 模板引用。
- throttle_period_in_millis 设置 30 分钟,防止同一商户频繁轰炸。
- webhook.url 通过集群级 Keystore 注入,满足国内安全合规要求。
- 若公司使用 钉钉,只需替换
body为钉钉格式,并调整url即可,无需改 Watcher 结构。
拓展思考
- 多级别告警:
在condition里用script区分record_score ≥ 50与≥ 75,分别走 钉钉群 与 电话语音(通过第三方网关如腾讯云告警)。 - 业务上下文 enrichment:
在input.chain第二步查 商户主数据索引,把merchant_name、tier带出来,让值班一眼识别是否为 VIP 商户。 - 告警收敛(Alert Fatigue):
利用 Watcher Acknowledge API 或 Elastic Alerting 的 Flapping 检测,结合 Redis 去重队列,实现“同一异常模式 1 小时内只通知一次”。 - 灰度与回滚:
在 Dev/Staging 环境先创建同 ID Watcher,但把 Slack 地址指向 #test-alert 频道;通过 Cluster Settings 动态开关xpack.watcher.enabled,实现秒级回滚。 - 成本优化:
对 PB 级集群,把 .ml-anomalies- 设置为冷节点 ILM 策略*,查询时带preference=_local避免跨机房拉数据,降低 Watcher 调度带来的 CPU 抖动。