# Kv - Drop messages that doesnt match

**URL:** <https://discuss.elastic.co/t/kv-drop-messages-that-doesnt-match/189757>\
**Category:** Logstash\
**Created:** [July 10, 2019, 12:11pm UTC](https://discuss.elastic.co/t/kv-drop-messages-that-doesnt-match/189757 "2019-07-10T12:11:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterH](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@PeterH](https://discuss.elastic.co/u/PeterH)\
**Post date:** [July 10, 2019, 12:11pm UTC](https://discuss.elastic.co/t/kv-drop-messages-that-doesnt-match/189757/1 "2019-07-10T12:11:39Z")

</div>

Hi,

i want to split up messages with that format with the kv filter plugin:  
"key1": "value1", "key2": "value2", "key3": "value3"

This is my kv config:  
kv {  
trim\_value =\> """  
trim\_key =\> ""\ ()"  
field\_split =\> ","  
value\_split =\> ":"  
}

Now I want to define that ervery message which cannot be parsed by the kv plugin will be dropped. I tried that from [Github](https://github.com/logstash-plugins/logstash-filter-kv/blob/master/lib/logstash/filters/kv.rb#L451):  
if "\_kv\_filter\_error" in [tags] {  
drop { }  
}

But unfortunately when nothing matched this tag will not assigned.

Has anybode an idea how to achive that?

Thank you!

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [July 10, 2019, 1:55pm UTC](https://discuss.elastic.co/t/kv-drop-messages-that-doesnt-match/189757/2 "2019-07-10T13:55:22Z")

</div>

> [@PeterH](#):
>
> Has anybode an idea how to achive that?

The \_kv\_filter\_error tag is only applied if an exception is caught by the kv filter. It can fail without raising an exception.

Perhaps you can check for a match against a regexp to decide whether the message has the right format?

---

<div class="post-metadata">

**Author:** ![PeterH](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@PeterH](https://discuss.elastic.co/u/PeterH)\
**Post date:** [July 10, 2019, 2:20pm UTC](https://discuss.elastic.co/t/kv-drop-messages-that-doesnt-match/189757/3 "2019-07-10T14:20:41Z")

</div>

Okay.  
How can i implement a standard regex? Or do I have to use a grok filter for that?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [July 10, 2019, 2:35pm UTC](https://discuss.elastic.co/t/kv-drop-messages-that-doesnt-match/189757/4 "2019-07-10T14:35:55Z")

</div>

Something like this:

```
    if [message] =~ /^\s*"[^"]+"\s*:\s*"[^"]+",\s*"[^"]+"\s*:\s*"[^"]+",\s*"[^"]+"\s*:\s*"[^"]+"\s*$/ {
        #Do something
    }
```

---

<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:** [August 7, 2019, 2:36pm UTC](https://discuss.elastic.co/t/kv-drop-messages-that-doesnt-match/189757/5 "2019-08-07T14:36:08Z")

</div>

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