Telegraf processors.regex create a new tag based on two patterns

1.9k Views Asked by At

I am using Telegraf's [[processors.regex]] to create a replacement tag.

Need to add a new tag "custom_instance" from existing tag "instance".

  1. Matching upto first occurrence of "#"
  2. If tag does not have "#" then it should simply copy the same value
Actual output
> Process,host=A700459-W10,custom_instance=chrome,instance=chrome#28,objectname=Process
> Process,host=A700459-W10,instance=CmRcService,objectname=Process

It does not create replacement for 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

Got two patterns : This pattern "^(.?)#.$" works and creates a replacement while this "^[^#]+$" does not, not sure why?

[[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"

Is there any other way of doing this where it will simply create a replacement tag based on existing values? if "#" then Matching upto first occurrence else simply copy the same value

1

There are 1 best solutions below

0
On

Figured out Second pattern will have replacement = "${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"