How to tell if tags is empty or does not exists?

Hi,

I want to be able to check if tags field is empty or if it does not exists in order to send the logstash output to different indexes. I'm using logstash 7.3

output {
  stdout { codec => rubydebug }
  if [tags] == [] {
     elasticsearch { hosts => ["https://192.168.90.119:9200"]
                           cacert => '/etc/logstash/config/certs/ca.crt'
                           user => 'logstash_writer'
                           password => '${lsw_pwd}'
                           index => "%{[type]}-%{type2}-%{+YYYY.MM.dd}"
                }
  }
}

however logstash does not start with the following error:

`[ERROR][logstash.agent ] Failed to execute action {:id=>:main, :action_type=>LogStash::ConvergeResult::FailedAction, :message=>"Failed to parse right-hand side of conditional [str]pipeline:599:6:\n[tags] == []\n", :backtrace=>["org.logstash.config.ir.expression.BinaryBooleanExpression.ensureNotNull(org/logstash/config/ir/expression/BinaryBooleanExpression.java:54)", "org.logstash.config.ir.expression.BinaryBooleanExpression.(org/logstash/config/ir/expression/BinaryBooleanExpression.java:34)", "org.logstash.config.ir.expression.binary.Eq.(org/logstash/config/ir/expression/binary/Eq.java:9)"

I also tried

if [tags] =~ /.+/  {
   #do something
}

it starts but never gets into the do something even when there are tags.
Can anyone suggest something?

Does

if not [tags]

work for you?

no it does not!

I finally found one way:

    if [tags] and !([tags][0]) {

it check for not null and if array is empty

reference: https://stackoverflow.com/questions/49047453/remove-array-field-if-empty/49051338

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.