Filtering out nil values in Logstash

PATCHing data in ElasticSearch with Logstash.

Let’s imaging you are trying to aggregate certain data about entity in ElasticSearch (ES further). The naive way to do it is to use Elasticsearch output with update action. But suddenly you realize that entities got overridden after each requests resulting with null values in ES. So you lose your data!

Let’s assume, we would like to get the following entity after collecting and patching all pieces of data:

If you have micro service infrastructure, it is likely that you don’t have all your data at once at some moment because you are operating on domains (merchants, order, transaction, etc.).

In our company we are using Kafka to collect every kind of data for further visualization, statistics and investigation.

Okay, talk is cheap, show me the code!

Naive attempt (wrong!):

This will produce the same behavior as described in the beginning of topic.

Our next thought is to use filters. Unfortunately, Logstash has not built-in to remove nil values from events.


There is awesome Logstash filter called Ruby filter. We can programmatically remove all nil values by the following Ruby code:

What it does is recursively traverse Event object and removes all nil values so that in the output section we have only existing key-value pairs.

Here is a complete Logstash pipeline to achieve this:

I am about to submit this code as a new filter for Logstash for easier reuse, so stay tuned!

P.S. This is my first article on Medium, so if you have some suggestions or note I am open to discussion in comments.

Thanks for reading.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrii Abramov

Fearless engineer, crazy about FinTech, microservices, and functional programming. Currently mastering NestJS & Terraform.