# Logstash count key words in Kafka and increment statsd

**URL:** <https://discuss.elastic.co/t/logstash-count-key-words-in-kafka-and-increment-statsd/34064>\
**Category:** Logstash\
**Created:** [November 6, 2015, 10:36pm UTC](https://discuss.elastic.co/t/logstash-count-key-words-in-kafka-and-increment-statsd/34064 "2015-11-06T22:36:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chengcheng\_Pei](https://avatars.discourse-cdn.com/v4/letter/c/919ad9/32.png) [@Chengcheng\_Pei](https://discuss.elastic.co/u/Chengcheng_Pei)\
**Post date:** [November 6, 2015, 10:36pm UTC](https://discuss.elastic.co/t/logstash-count-key-words-in-kafka-and-increment-statsd/34064/1 "2015-11-06T22:36:30Z")

</div>

New to logstash.  
Assume messages in kafka look like:

```
{"key":"val1"}
{"key":"val2"}
{"key":"val2"}
{"key":"val1"}
...

```

Try to count the numbers of val1 and val2 in key field, and increase statsd keys: total.val1 and total.val2

How to write the filter and statsd output.  
Searching by Google, hard to find good examples.

Any good example or suggestions?

```
input
{
    kafka
    {
        topic_id => 'test2'
    }
}

filter
{
    metrics
    {
        meter => ["'key':%{number}"]
        add_tag => ["metric"]
    }
}

output
{
    if "metric" in [tags]
    {
        stdout
        {
            codec => line
            {
                format => "count: %{number.count}"
            }
        }
        statsd { increment => "test.%{number.count}" }
    }
}
```

---

<div class="post-metadata">

**Author:** ![suyograo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/suyograo/32/44898_2.png) [@suyograo](https://discuss.elastic.co/u/suyograo)\
**Post date:** [November 8, 2015, 2:49am UTC](https://discuss.elastic.co/t/logstash-count-key-words-in-kafka-and-increment-statsd/34064/2 "2015-11-08T02:49:35Z")

</div>

Not sure why you need metrics filter here. You can just do:

```auto
statsd { increment => "%{key}" }

```

statds will increment the `val1`, `val2` key etc.

---

<div class="post-metadata">

**Author:** ![Chengcheng\_Pei](https://avatars.discourse-cdn.com/v4/letter/c/919ad9/32.png) [@Chengcheng\_Pei](https://discuss.elastic.co/u/Chengcheng_Pei)\
**Post date:** [November 8, 2015, 3:14am UTC](https://discuss.elastic.co/t/logstash-count-key-words-in-kafka-and-increment-statsd/34064/3 "2015-11-08T03:14:08Z")

</div>

I need to metrics filter to count. Is there problem in my filter (meter) ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 5:23am UTC](https://discuss.elastic.co/t/logstash-count-key-words-in-kafka-and-increment-statsd/34064/4 "2017-07-06T05:23:28Z")

</div>


