Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regex plugin does not support matching on the same key and result key #4374

Closed
srclosson opened this issue Jul 3, 2018 · 1 comment · Fixed by #4396
Closed

regex plugin does not support matching on the same key and result key #4374

srclosson opened this issue Jul 3, 2018 · 1 comment · Fixed by #4396
Assignees
Labels
bug unexpected problem or unintended behavior
Milestone

Comments

@srclosson
Copy link

srclosson commented Jul 3, 2018

Relevant telegraf.conf:

[[processors.regex]]
  namepass = ["RandomData"]
  [[processors.regex.tags]]
    key = "ComputerName"
    pattern = "^(.*?)a$"
    replacement = "${1}"
    result_key = "ServerName"
  [[processors.regex.tags]]
    key = "ComputerName"
    pattern = "^(.*?)b$"
    replacement = "${1}"
    result_key = "ServerName"

System info:

Telegraf Version: 1.8.0
OS: Windows Server 2012

Steps to reproduce:

  1. Use any input plugin. Try to strip off the last character

Expected behavior:

In the above configuration, I would expect that ServerName should be a tag that holds the value of the tag "ComputerName" without the trailing a or b.

Actual behavior:

Only the the configuration of the last regex is honored. I believe what is happening is that when a subsequent regex match fails, it returns an empty string, which then get's reassigned to the tag, overwriting the value that I was interested in.

Additional info:

I believe this could be a quick fix to the regex processor plugin. The fix I made is below, and seems to be working:

		for _, converter := range r.Tags {
			if value, ok := metric.GetTag(converter.Key); ok {
				k, v := r.convert(converter, value)
				if k != "" && v != "" {
					metric.AddTag(k, v)
				}
			}
		}

		for _, converter := range r.Fields {
			if value, ok := metric.GetField(converter.Key); ok {
				switch value := value.(type) {
				case string:
					k, v := r.convert(converter, value)
					if k != "" && v != "" {
						metric.AddField(r.convert(converter, value))
					}
				}
			}
		}
@glinton glinton self-assigned this Jul 3, 2018
@glinton glinton added the bug unexpected problem or unintended behavior label Jul 3, 2018
@glinton
Copy link
Contributor

glinton commented Jul 3, 2018

Thanks for submitting this!

Expected Behavior

deal,computer_name=hosta,host=tester,server_name=host message="stuff" 1530654676000000000
deal,computer_name=hostb,host=tester,server_name=host message="stuff" 1530654676000000000

Actual Behavior

deal,computer_name=hosta,host=tester message="stuff" 1530654676000000000
deal,computer_name=hostb,host=tester,server_name=host message="stuff" 1530654676000000000

Working telegraf.conf

[agent]
  interval="1s"
  flush_interval="1s"

[[inputs.exec]]
  timeout = "1s"
  data_format = "influx"
  commands = [
    "echo 'deal,computer_name=hosta message=\"stuff\" 1530654676316265790'",
    "echo 'deal,computer_name=hostb message=\"stuff\" 1530654676316265790'",
  ]

[[processors.regex]]
  [[processors.regex.tags]]
    key = "computer_name"
    pattern = "^(.*?)a$"
    replacement = "${1}"
    result_key = "server_name"
  [[processors.regex.tags]]
    key = "computer_name"
    pattern = "^(.*?)b$"
    replacement = "${1}"
    result_key = "server_name"

[[outputs.file]]
  files = ["stdout"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug unexpected problem or unintended behavior
Projects
None yet
3 participants