Skip to content

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

@srclosson

Description

@srclosson

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))
					}
				}
			}
		}

Metadata

Metadata

Assignees

Labels

bugunexpected problem or unintended behavior

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions