-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add regex processor plugin #3839
Conversation
very nice |
Would be possible to use this to convert a field k/v pair to a tag, or extract tag name and value from partial field name (while renaming the field name too)? |
Thanks for your interest! As for now, this plugin operates only on values, so it can't convert tag to field or change its name. I thought about some option which allows to convert between tag and field but later decided it would be better to have separate plugin for this. Such plugin can have more obvious name – "tag2field" instead of too general "regex" – so it would be easier to find it.
I'm not sure I properly understand your use case here. Can you give example of field name you have and what tag you want to create from it? |
Actually, what I was thinking would require splitting the metric point into multiple ones, which should probably be handled by the input plugin itself. Or by a stand-alone processor maybe (I have no idea if a processor can create metric points). Let's say a metric has field names which denote a thread index in a multi-threaded process:
This would translate to:
So this would be something more or less like:
I implement something like this as an option to an existing input plugin (targeting specific field names created by the plugin), so I was wondering if your plugin could be used as a generic way to do the same kind of thing. |
@voiprodrigo This seems like a fairly specialized operation, I'm not sure if it can be generalized nicely. I think for this it would make sense if we had a processor that could run a user script to filter messages. To avoid slowdown we would run the processor once and feed data in/out via stdin/stdout. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I just have a few performance suggestions since this will be ran so frequently.
plugins/processors/regex/regex.go
Outdated
} | ||
|
||
func getValue(c converter, value string) string { | ||
regex := regexp.MustCompile(c.Pattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should compile the regex pattern only once on startup, you could do this in Apply with a boolean field on the Regex type so it is only executed once.
plugins/processors/regex/regex.go
Outdated
func (r *Regex) Apply(in ...telegraf.Metric) []telegraf.Metric { | ||
for _, metric := range in { | ||
for _, converter := range r.Tags { | ||
if value, ok := metric.Tags()[converter.Key]; ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use metric.GetTag
to avoid allocating.
plugins/processors/regex/regex.go
Outdated
} | ||
|
||
for _, converter := range r.Fields { | ||
if value, ok := metric.Fields()[converter.Key]; ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use metric.GetField
here as well.
# Conflicts: # plugins/processors/all/all.go
Thanks for the tips! I applied Here are results of running Before:
After:
|
Based on proposal from @danielnelson in #2667 (comment)
Regex processor plugin allows to change tag and field values or create new tags/fields from existing ones.
My use-case for this plugin: it allows to use a simple predefined pattern such as
%{COMBINED_LOG_FORMAT}
inlogparser
plugin then do some processing to extract additional data from request field.Configuration:
Source Metric (from logparser):
Example Output:
Required for all PRs: