Telegraf processor.regex 基于两种模式创建一个新标签

问题描述

我正在使用 Telegraf 的 [[processors.regex]] 创建替换标签

需要从现有标签“instance”添加一个标签“custom_instance”。

  1. 匹配到第一次出现的“#”
  2. 如果标签没有“#”,那么它应该简单地复制相同的值
Actual output
> Process,host=A700459-W10,custom_instance=chrome,instance=chrome#28,objectname=Process
> Process,instance=CmRcService,objectname=Process

它不会创建 CmRcService 的替代

Expected output
> Process,custom_instance=CmRcService,objectname=Process

有两种模式:这个模式“^(.?)#.$”有效并创建了一个替代,而这个“^[^#]+$”没有,不知道为什么?

[[processors.regex]]
  namepass = ["Process"]
  # Tag and field conversions defined in a separate sub-tables
  [[processors.regex.tags]]
    ## Tag to change
    key = "instance"
    ## Regular expression to match on a tag value
    pattern = "^(.*?)#.*$"
    ## Matches of the pattern will be replaced with this string.  Use ${1}
    ## notation to use the text of the first submatch.
    replacement = "${1}"
    result_key = "custom_instance"
  [[processors.regex.tags]]
    ## Tag to change
    key = "instance"
    ## Regular expression to match on a tag value
    pattern = "^[^#]+$"
    ## Matches of the pattern will be replaced with this string.  Use ${1}
    ## notation to use the text of the first submatch.
    replacement = "${1}"
    result_key = "custom_instance"

是否有其他方法可以根据现有值简单地创建替换标签? if "#" then 匹配到第一次出现 否则只需复制相同的值

解决方法

想通了 第二个模式将替换 = "${0}"

[[processors.regex]]
  namepass = ["Process"]
  # Tag and field conversions defined in a separate sub-tables
  [[processors.regex.tags]]
    ## Tag to change
    key = "instance"
    ## Regular expression to match on a tag value
    pattern = "^(.*?)#.*$"
    ## Matches of the pattern will be replaced with this string.  Use ${1}
    ## notation to use the text of the first submatch.
    replacement = "${1}"
    result_key = "custom_instance"
  [[processors.regex.tags]]
    ## Tag to change
    key = "instance"
    ## Regular expression to match on a tag value
    pattern = "^[^#]+$"
    ## Matches of the pattern will be replaced with this string.  Use ${1}
    ## notation to use the text of the first submatch.
    replacement = "${0}"
    result_key = "custom_instance"