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

适用版本: 6.8-8.9

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

could not parse message attachment field. found field [X]; but no [Y] is defined 是 Elasticsearch Watcher 在解析消息附件(message attachment)时抛出的异常。此错误表示附件配置中某个字段存在,但其依赖的必需字段却未定义。

典型的依赖关系包括:author_linkauthor_icon 必须配合 author_name 使用;image_url 可能需要配合其他字段使用等。

常见现象 #

  • 创建或更新 watch 时返回 HTTP 400 错误。
  • 报错信息明确指出哪个字段存在但缺少哪个依赖字段。
  • 常见于 Slack、邮件等 action 的 attachment 配置中。
  • 当手动编写 attachment JSON 时,容易遗漏字段间的依赖关系。

典型报错与异常栈 #

{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "could not parse message attachment field. found field [author_link]; but no [author_name] is defined"
      }
    ],
    "type": "parse_exception",
    "reason": "could not parse message attachment field. found field [author_link]; but no [author_name] is defined"
  },
  "status": 400
}

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

[2024-01-15T10:30:00,123][WARN ][o.e.x.w.a.e.SlackAction ] [node-1] failed to parse message attachment
ElasticsearchParseException[could not parse message attachment field. found field [author_link]; but no [author_name] is defined]
    at org.elasticsearch.xpack.watcher.actions.slack.SlackAttachment.parse(SlackAttachment.java:145)
    at org.elasticsearch.xpack.watcher.actions.slack.SlackAction.parse(SlackAction.java:89)

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

Watcher 的 message attachment 中某些字段之间存在依赖关系,当使用了依赖字段但未定义其必需的父字段时,就会触发此错误。

常见原因包括:

  • 缺少必需的主字段:例如定义了 author_linkauthor_icon,但未定义 author_name(因为 author_linkauthor_icon 必须配合 author_name 使用)。
  • 字段依赖关系理解错误:未仔细阅读官方文档中关于字段依赖关系的说明。
  • 复制粘贴导致的不完整配置:从其他配置复制时只复制了部分字段,遗漏了依赖字段。
  • 版本差异:不同版本的 Watcher 对字段依赖关系的要求可能不同。
  • JSON 结构不完整:在编写复杂的嵌套 attachment 结构时,遗漏了某些必需字段。

常见的字段依赖关系:

  • author_link 依赖 author_name
  • author_icon 依赖 author_name
  • image_url 在某些场景下可能需要配合其他字段

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

排查步骤 #

  1. 确认报错信息中的字段依赖关系:从错误信息中提取哪个字段存在(found field [X])和缺少哪个依赖字段(no [Y] is defined)。

  2. 检查 Watch 中的 attachment 配置

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

# 检查 attachment 部分
  1. 对照官方文档验证字段依赖:确认所使用的字段是否满足所有依赖要求。

  2. 检查 JSON 结构完整性:确保 attachment 对象包含所有必需的字段。

排查时需要注意的问题 #

  • 注意区分"可选字段"和"条件必需字段":某些字段在特定条件下才需要。
  • 如果使用了动态模板,确保模板渲染后的结果也满足字段依赖关系。
  • 检查是否有拼写错误导致字段名不匹配(如 author_name 写成 authorName)。

4. 如何解决这个错误 #

常用修复思路 #

  1. 补充缺少的依赖字段:根据报错信息,添加缺少的必需字段。
# 修复前(缺少 author_name)
PUT _watcher/watch/<watch_id>
{
  "actions": {
    "send_slack": {
      "slack": {
        "message": {
          "attachments": [
            {
              "author_link": "https://example.com",
              "author_icon": "https://example.com/icon.png"
            }
          ]
        }
      }
    }
  }
}

# 修复后(添加 author_name)
PUT _watcher/watch/<watch_id>
{
  "actions": {
    "send_slack": {
      "slack": {
        "message": {
          "attachments": [
            {
              "author_name": "Example Author",
              "author_link": "https://example.com",
              "author_icon": "https://example.com/icon.png"
            }
          ]
        }
      }
    }
  }
}
  1. 移除多余的依赖字段:如果不需要主字段,也可以移除依赖字段。
# 如果不需要 author 信息,移除所有 author 相关字段
PUT _watcher/watch/<watch_id>
{
  "actions": {
    "send_slack": {
      "slack": {
        "message": {
          "attachments": [
            {
              "title": "Alert Title",
              "text": "Alert message"
            }
          ]
        }
      }
    }
  }
}
  1. 使用模板简化配置:使用预定义的 attachment 模板,避免字段遗漏。

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

  • 在编写 attachment 配置时,参照官方文档的示例,确保字段依赖关系正确。
  • 对常用的 attachment 配置建立模板库,减少手动编写导致的错误。
  • 在测试环境验证 attachment 配置,确保所有字段依赖关系正确。

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

  • INFINI Console 提供可视化的 Watch 编辑和验证功能,可以在保存前检查 attachment 配置的合法性,避免因字段依赖问题导致的解析错误。
  • INFINI Gateway 可以记录 Watcher action 的完整请求内容,帮助捕获 attachment 配置的详细错误上下文,并通过流量回放功能验证修复方案。
  • 通过 INFINI Console 的 Watch 测试功能,可以在不执行完整 Watch 的情况下验证 attachment 配置的合法性。

5. 小结 #

could not parse message attachment field. found field [X]; but no [Y] is defined 是一个字段依赖关系错误,表示 attachment 中某个字段存在但缺少其依赖的必需字段。解决此问题的关键是:理解报错信息中的字段依赖关系,补充缺少的必需字段或移除多余的依赖字段。

通过 INFINI Console 的配置验证功能和 INFINI Gateway 的请求捕获能力,可以更高效地定位和修复 message attachment 的字段依赖问题。

相关错误 #

参考文档 #

附:日志上下文 #

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

currentFieldName);
}
}
if (authorName == null) {
    if (authorLink != null) {
        throw new ElasticsearchParseException("could not parse message attachment field. found field [{}]; but no [{}] is " +
            "defined", XField.AUTHOR_LINK, XField.AUTHOR_NAME);
    }
    if (authorIcon != null) {
        throw new ElasticsearchParseException("could not parse message attachment field. found field [{}]; but no [{}] is " +
            "defined", XField.AUTHOR_ICON, XField.AUTHOR_NAME);