如果使用 ingestnode 管道存在一个字段,如何将值从一个字段复制到另一个字段

How to copy a value from one field to other if a field exists by using ingestnode pipeline

如果名为 prometheus.labels.pod 的字段存在于日志中,我想创建一个名为 kubernetes.pod.name 的新字段。我发现我可以从设置处理器中将 prometheus.labels.pod 中存在的值复制到新字段 kubernetes.pod.name 但我需要有条件地执行此操作,因为 pod 名称不断变化。

我如何设置一个条件,如果字段 prometheus.labels.pod 存在,那么我只需要添加一个名为 kubernetes.pod.name 的新字段(两者具有相同的值)

ctx.prometheus?.labels?.namespace== "name_of_namespace"

我们也可以这样做

ctx.prometheus?.labels?.pod== "*"

检查这个字段是否存在?

如果文本是一个字符串,并且如果它需要设置一个条件,如果它存在,那么最好的方法是在设置处理器中使用以下条件。

ctx.prometheus?.labels?.namespace!=null

这就是我使用摄取节点管道实现上述场景的方式。

"set": {
  "field": "kubernetes.pod.name",
  "copy_from": "prometheus.labels.pod",
  "if": "ctx.prometheus?.labels?.pod!=null",
  "ignore_failure": true
}