Hi!
I wanted to optimize my Logstash config. The old one looked like this:
output {
if ("json_extracted" in [tags]) {
elasticsearch {
codec => "json"
hosts => <%= $es_hosts.prefix('"').suffix('"') %>
index => '%{elk_input}-%{+YYYY.MM.dd}'
}
} else {
elasticsearch {
codec => "plain"
hosts => <%= $es_hosts.prefix('"').suffix('"') %>
index => '%{elk_input}-%{+YYYY.MM.dd}'
}
}
...
}
This pattern is repeated multiple times. You can see the only difference is the codec setting (json vs. plain). I thought I could use a variable for that, but did not find a way how to set one. Luckily I found the @metadata field. So I changed my code to this:
filter {
if ("json_extracted" in [tags]) {
mutate { add_field => { "[@metadata][codec]" => "json" } }
} else {
mutate { add_field => { "[@metadata][codec]" => "plain" } }
}
}
output {
elasticsearch {
codec => "%{[@metadata][codec]}"
hosts => <%= $es_hosts.prefix('"').suffix('"') %>
index => '%{elk_input}-%{+YYYY.MM.dd}'
}
...
}
What it should do is decide if json or plain is used, store that in @metadata.codec and use it in the output on field codec.
Well, that doesn't work. Is the idea totally wrong or did I do a syntax error which I have missed?
Thanks for your help,
Alex