2

I would like to start a logstash instance with following setting:

input {
  kafka {
    topic_id => "topic_a"
    .......
  }
  kafka {
    topic_id => "topic_b"
    .......
  }

}
filter {
  json {
    source => "message"
  }
  uuid {
    target => "@uuid"
  }
  mutate {
    replace => { "message" => "%{message}" } # want to get the full json literal but does not work
    add_field => {
      "topic" => "%{topic_id}" # it does not work either
    }
  }

  # logic to apply different filter base on topic_id
  if [topic_id] =~ 'topic_a' { # this block seems never entered        
    mutate {
       replace => { "topic" => "topic_a" }
    }
  } else {
    .....
  }
}
output {
  .....
}

The output on my Kibana would should something like below:

topic : %{topic_id}

It suggested that the configuration above could not extract the topic_id. I have no idea on how to configure the filter part. Could anyone give a hint on this? Thanks.

BTW I'm using logstash-2.2.2

Edit: updated config according to logstash document, result still the same

2 Answers 2

5

By default Kafka Input Plugin do not include metadata info like: topic_id ..
You have to enable decorate_events options:

kafka {
    topic_id => "topic_a"
    decorate_events => true
  }

When this done, you can find your topic_id in: kafka array with topic key.

decorate_events

Value type is boolean Default value is false Option to add Kafka metadata like topic, message size to the event. This will add a field named kafka to the logstash event containing the following attributes: msg_size: The complete serialized size of this message in bytes (including crc, header attributes, etc) topic: The topic this message is associated with consumer_group: The consumer group used to read in this event partition: The partition this message is associated with key: A ByteBuffer containing the message key https://www.elastic.co/guide/en/logstash/current/plugins-inputs-kafka.html#plugins-inputs-kafka-decorate_events

Sign up to request clarification or add additional context in comments.

Comments

1

The documentation for add_field shows a different syntax than the one you're using. You might try it.

filter {
  mutate {
    add_field => { "foo_%{somefield}" => "Hello world, from %{host}" }
  }  
}

2 Comments

hi, I have updated my question. I was actually using "%" instead of "$", but it doesn't work.
If the [topic] field is still not being set, it's because either your input is not coming through one of the two inputs you've shown (where topic_id is set), or your conditional (if topic_id = xxx) is not true, or the mutate->replace fails (in which case the add_field will not run). Or something else you're not showing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.