📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

适用版本: 6.8-8.9

1. 错误异常的基本描述 #

could not parse pager duty event template. unexpected field [field] 是 Elasticsearch Watcher 在解析 PagerDuty 事件模板时抛出的异常。当事件模板对象中出现当前解析器不支持的字段时,就会触发此错误。

此错误指向模板 JSON 结构本身,而不是 PagerDuty 渠道的发送阶段。常见于手动编写 PagerDuty action 配置时,或从不同版本的配置示例复制粘贴时。

常见现象 #

  • 创建或更新 watch 时返回 HTTP 400 错误。
  • 报错信息明确指出哪个字段是未预期的(unexpected field [field])。
  • 常见于 PagerDuty action 的 event 模板配置中。
  • 如果字段名拼写错误或字段放错层级,就容易触发此错误。

典型报错与异常栈 #

{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "could not parse pager duty event template. unexpected field [invalid_field]"
      }
    ],
    "type": "parse_exception",
    "reason": "could not parse pager duty event template. unexpected field [invalid_field]"
  },
  "status": 400
}

服务端日志中可能出现类似以下内容:

[2024-01-15T10:30:00,123][WARN ][o.e.x.w.a.p.PagerDutyAction] [node-1] failed to parse pager duty event template
ElasticsearchParseException[could not parse pager duty event template. unexpected field [invalid_field]]
    at org.elasticsearch.xpack.watcher.actions.pagerduty.PagerDutyEventTemplate.parse(PagerDutyEventTemplate.java:145)
    at org.elasticsearch.xpack.watcher.actions.pagerduty.PagerDutyAction.parse(PagerDutyAction.java:89)

2. 为什么会发生这个错误 #

Watcher 的 PagerDuty 事件模板解析器只接受一组预定义的固定字段(如 descriptioneventTypeincidentKeyclientcontexts 等)。当解析器遇到不在支持列表中的字段时,就会抛出 unexpected field 异常。

常见原因包括:

  • 字段拼写错误:例如将 description 误写为 desceventType 误写为 event_type 等。
  • 字段放错层级:把应该属于 action 顶层的字段放到了事件模板中,或反之。
  • 版本不兼容:参考了不同版本的配置示例,字段集不兼容。
  • 混淆 PagerDuty API 字段:使用了 PagerDuty REST API 支持的字段,但这些字段在 Watcher 的事件模板中不被支持。
  • 复制粘贴错误:从其他通知类型(如 Slack、Email)的配置复制字段,这些字段在 PagerDuty 事件模板中不支持。
  • JSON 结构嵌套错误:字段嵌套层级不正确,导致解析器在某个层级遇到了不期望的字段。

3. 如何排查和解决这个异常和解决这个异常 #

排查步骤 #

  1. 确认报错中的字段名:从错误信息中提取具体是哪个字段未预期(unexpected field [field])。

  2. 检查 Watch 中的 PagerDuty action 配置

# 获取 watch 配置
GET _watcher/watch/<watch_id>?pretty

# 检查 PagerDuty action 的 event 模板部分
  1. 对照官方文档验证字段合法性:确认该字段是否在当前版本的 PagerDuty 事件模板支持列表中。

  2. 检查字段层级是否正确

# 正确的 PagerDuty event 模板结构示例
PUT _watcher/watch/<watch_id>
{
  "actions": {
    "send_pagerduty": {
      "pagerduty": {
        "account": "pagerduty_account",
        "event": {
          "description": "Elasticsearch Alert: {{ctx.payload.hits.total}} hits",
          "eventType": "trigger",
          "incidentKey": "{{ctx.watch_id}}",
          "client": "Elasticsearch Watcher",
          "clientUrl": "https://example.com"
        }
      }
    }
  }
}

排查时需要注意的问题 #

  • 注意区分事件模板字段和 action 级别字段:它们支持的字段集合不同。
  • 如果使用了 contexts 字段,确保其结构是数组且每个元素包含必需的字段。
  • 检查是否有从其他通知类型复制过来的字段,这些字段在 PagerDuty 中可能不支持。

4. 如何解决这个错误 #

常用修复思路 #

  1. 删除非法字段:从事件模板对象中移除不支持的字段。
# 修复前(包含不支持的字段)
PUT _watcher/watch/<watch_id>
{
  "actions": {
    "send_pagerduty": {
      "pagerduty": {
        "event": {
          "description": "Alert",
          "invalid_field": "some value"  # 不支持的字段
        }
      }
    }
  }
}

# 修复后(移除不支持的字段)
PUT _watcher/watch/<watch_id>
{
  "actions": {
    "send_pagerduty": {
      "pagerduty": {
        "event": {
          "description": "Alert"
        }
      }
    }
  }
}
  1. 调整字段层级:将字段移到正确的层级。
# 如果字段应该属于 action 而非 event 模板
# 错误:放在 event 中
"event": {
  "description": "Alert",
  "account": "my_account"  # 错误位置
}

# 正确:account 应该属于 pagerduty action
"pagerduty": {
  "account": "my_account",  # 正确位置
  "event": {
    "description": "Alert"
  }
}
  1. 使用版本兼容的配置:确保配置与当前 Elasticsearch 版本兼容。

后续注意事项与推荐建议 #

  • 在编写 PagerDuty 事件模板时,严格参照官方文档中的字段列表。
  • 建立配置模板库,复用经过验证的配置片段。
  • 在测试环境验证 PagerDuty 配置,特别是在从其他来源复制配置时。

借助 INFINI 产品提升排障效率 #

  • INFINI Console 提供可视化的 Watch 编辑功能,可以在保存前验证 PagerDuty 事件模板的字段合法性,自动检测不支持的字段。
  • INFINI Gateway 可以记录 Watcher action 的完整请求内容,帮助捕获 PagerDuty 配置的详细错误上下文,并通过流量回放验证修复方案。
  • 通过 INFINI Console 的配置对比功能,可以对比不同版本间的配置差异,快速定位版本兼容性问题。

5. 小结 #

could not parse pager duty event template. unexpected field [field] 是一个字段合法性错误,表示 PagerDuty 事件模板中包含了不支持的字段。解决此问题的关键是:确认报错字段名,检查字段是否应该存在于当前层级,移除不支持的字段或将字段移到正确位置。

通过 INFINI Console 的配置验证功能和 INFINI Gateway 的请求捕获能力,可以更高效地定位和修复 PagerDuty 事件模板的字段层级问题。

相关错误 #

参考文档 #

附:日志上下文 #

下面保留当前页面中的源码或日志片段,便于继续结合异常调用栈定位问题:

} else {
    throw new ElasticsearchParseException("could not parse pager duty event template. unexpected field [{}]",
        currentFieldName);
}
return new Template(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts, proxy);