0

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

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

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

它不会为 CmRcService 创建替换

Expected output
> Process,host=A700459-W10,custom_instance=chrome,instance=chrome#28,objectname=Process
> Process,host=A700459-W10,custom_instance=CmRcService,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 匹配到第一次出现 else 只需复制相同的值

4

1 回答 1

0

想出第二个模式将有替换=“${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"
于 2021-06-03T16:52:46.603 回答