在 Logstash if 语句中使用非白名单字段

Use non whitelisted fields in Logstash if statement

我有一个 logstash 配置,我使用 prune 将几个字段列入白名单。

prune {
   whitelist_names => ["id","name"]
  }

问题是,我需要在 id 字段以外的字段的输出中使用 if 条件,例如:“type”。但由于我没有将“类型”列入白名单,if 条件不起作用。

 if ( [type] in  ["abc","efg"] )  {
        elasticsearch {
            action => "update"
            hosts => [ "localhost:9200" ]
            index => "index"
            document_id => "%{id}"
            doc_as_upsert => true
        }
      }

如何在 if 条件下使用非白名单字段?

在您的修剪过滤器之前,添加一个变异过滤器以将您要删除的字段 (type) 的值复制到一个新的元数据字段中。然后,修剪。然后,在您的输出条件中使用新的元数据字段。

...
filter {
  ...
  mutate {
    add_field => {
      "[@metadata][type]" => "%{type}"
    }
  } 
  prune {
   whitelist_names => ["id","name"]
  }
  ...
}

output {
  if [@metadata][type] in  ["abc","efg"] {
    elasticsearch {
      action => "update"
      hosts => [ "localhost:9200" ]
      index => "index"
      document_id => "%{id}"
      doc_as_upsert => true
    }
  }
}