Link Search Menu Expand Document Documentation Menu

dissect

dissect 处理器从事件中提取值,并根据用户定义的 dissect 模式将其映射到各个字段。该处理器非常适合从具有已知结构的日志消息中提取字段。

基本用法

要使用 dissect 处理器,请创建以下 pipeline.yaml 文件

dissect-pipeline:
  source:
    file:
      path: "/full/path/to/logs_json.log"
      record_type: "event"
      format: "json"
  processor:
    - dissect:
        map:
          log: "%{Date} %{Time} %{Log_Type}: %{Message}"
  sink:
    - stdout:

然后创建以下名为 logs_json.log 的文件,并将您 pipeline.yaml 文件中文件源的 path 替换为包含以下 JSON 数据的文件路径

{"log": "07-25-2023 10:00:00 ERROR: error message"}

dissect 处理器将根据管道中配置的模式 %{Date} %{Time} %{Type}: %{Message},从 log 消息中检索字段(DateTimeLog_TypeMessage)。

运行管道后,您应该会收到以下标准输出

{
    "log" : "07-25-2023 10:00:00 ERROR: Some error",
    "Date" : "07-25-2023"
    "Time" : "10:00:00"
    "Log_Type" : "ERROR"
    "Message" : "error message"
}

配置

您可以使用以下选项配置 dissect 处理器。

选项 必需 类型 描述
map 映射 定义特定键的 dissect 模式。有关如何在 dissect 模式中定义字段的详细信息,请参阅字段表示法
target_types 映射 指定提取字段的数据类型。有效选项包括 integerdoublestringboolean。默认情况下,所有字段都为 string 类型。
dissect_when 字符串 指定使用 Data Prepper 表达式执行 dissect 操作的条件。如果指定,dissect 操作仅当表达式评估为 true 时才会运行。

字段表示法

您可以使用以下字段类型定义 dissect 模式。

普通字段

不带后缀或前缀的字段。该字段将直接添加到输出事件中。格式为 %{field_name}

跳过字段

不会包含在事件中的字段。格式为 %{}%{?field_name}

追加字段

将与其他字段合并的字段。要追加多个值并将最终值包含在字段中,请在 dissect 模式中的字段名前使用 +。格式为 %{+field_name}

例如,对于模式 %{+field_name}, %{+field_name},日志消息 "foo, bar" 将解析为 {"field_name": "foobar"}

您还可以借助后缀 /<integer> 定义连接顺序。

例如,对于模式 "%{+field_name/2}, %{+field_name/1}",日志消息 "foo, bar" 将解析为 {"field_name": "barfoo"}

如果未指定顺序,则追加操作将按照 dissect 模式中指定的字段顺序进行。

间接字段

使用另一个字段的值作为其字段名的字段。定义模式时,在该字段前加上 &,以将字段中找到的值作为键值对中的键。

例如,对于模式 "%{?field_name}, %{&field_name}",日志消息 "foo, bar" 将解析为 {“foo”: “bar”}。在日志消息中,foo 是从跳过字段 %{?field_name} 中捕获的。foo 随后作为从字段 %{&field_name} 中捕获的值的键。

填充字段

移除右侧填充的字段。-> 运算符可用作后缀,表示该字段后的空格可以忽略。

例如,对于模式 %{field1->} %{field2},日志消息 “firstname lastname” 将解析为 {“field1”: “firstname”, “field2”: “lastname”}

剩余 350 字符

有问题?

想要贡献?