Hi I am having trouble understanding the proper way to use metrics filter plugin to count the number of fields of a particular type. Say I have a file formatted with a name and age separated by a space e.g.:
dustin 40
emily 35
joe 40
I am trying to use a metric filter like the following:
filter {
grok {
match => { "message" => "%{WORD:name} %{NUMBER:age}" }
}
metrics {
meter => "age_%{age}"
add_tag => ["meter", "%{age}"]
add_field => { "age" => "%{age}" }
}
}
But in adding the tags I only get a literal "%{age}" string in the event instead of the value of the age field from the log e.g. 40, 35, etc.
{
"age" => "%{age}",
"@version" => "1",
"age_35" => {
"count" => 1,
"rate_1m" => 0.18400888292586468,
"rate_5m" => 0.19669429076432351,
"rate_15m" => 0.19889196960097935
},
"tags" => [
[0] "meter",
[1] "%{age}"
],
"message" => "hostname",
"@timestamp" => 2023-01-06T23:45:04.733452Z
}
What am I doing wrong here?
On a related note I am also having trouble outputting the counter values of different ages. For example I'd like the ultimate output to be the count of each age value:
age_40: 2
age_35: 1
I have tried an output statement like:
output {
if "meter" in [tags] {
stdout {
codec => line { format => "age_39: %{[age_39][count]}" }
}
}
}
which works okay but is hard-coded
There doesn't seem to be any way to interpolate the age into output string so that it I get my desired output; the aggregated count per age. Is there a better way to go about this? should I be using an aggregate filter instead?