AI TL;DR Too lazy to read? Summarize this post with:
Logstash Modular Pipelines: Reusing Inputs, Filters, and Outputs Without Duplication#
This blog post covers a Logstash pipeline structure that removes duplicated code. Each input, filter, and output lives in its own file, and every application's pipeline is composed from those modules with a glob pattern and an environment variable. It includes an example configuration, a Docker Compose lab to try it, and my opinion on when the structure is worth it.
How a Logstash Pipeline Is Structured#
Logstash is a great tool for building log pipelines. Its plugins cover ingestion, processing, enrichment, and delivery to other systems. A Logstash pipeline is a configuration written in the Logstash configuration DSL (Domain-Specific Language), a high-level language designed for log processing.
According to the configuration file structure documentation, a pipeline has three main sections:
input: where the logs come from.filter: how they are parsed, processed, and enriched.output: where the processed logs are sent.
The Problem: More Pipelines, More Duplicated Code#
As the number of pipelines grows, so does the duplicated code. For simple needs, one file with input, filter, and output together is enough. With large applications, such as distributed services on Kubernetes, avoiding duplication becomes one of the main challenges.
When several applications need different processing logic, there are two common approaches, and both have a cost:
One shared pipeline with conditionals. The
filtersection checks a field such as a tag to decide which logic to apply. This avoids duplicatinginputandoutput, but it adds overhead to thefiltersection and makes it harder to find which application has a problem in the Logstash API metrics.One pipeline per application. Each application gets its own configuration file. This removes the
filteroverhead, but every pipeline repeats the sameinputandoutputconfiguration.
The Solution: Modular Pipelines#
The structure I use takes the good part of both approaches: split the pipeline sections (input, filter, output) into separate files, and load only the ones each application needs into its own dedicated pipeline.
Two Logstash features make this possible. Glob pattern support lets path.config load several files at once, and environment variables let you choose those files outside the configuration.
Configuration Example#
The first step is to create the modules: each input, filter, and output goes in its own file, focused on one job.
├── filter-app-a.cfg # filter of the application-a logic parser
├── filter-app-b.cfg # filter of the application-b logic parser
├── input-http.cfg # input to receive logs from http protocol
├── input-rabbitmq.cfg # input to receive logs from rabbitmq queue
├── output-opensearch.cfg # output to forward logs processed to opensearch server
└── output-http.cfg # output to forward logs processed to http server
Next, define one pipeline per application, with path.config pointing to an environment variable:
- pipeline.id: application-a-pipeline
path.config: "/usr/share/logstash/pipeline/${LOGSTASH_PIPELINE_APPLICATION_A}.cfg"
- pipeline.id: application-b-pipeline
path.config: "/usr/share/logstash/pipeline/${LOGSTASH_PIPELINE_APPLICATION_B}.cfg"
Finally, set each variable to a glob pattern that lists the modules for that pipeline:
LOGSTASH_PIPELINE_APPLICATION_A="{input-http,input-rabbitmq,filter-app-a,output-opensearch,output-http}"
LOGSTASH_PIPELINE_APPLICATION_B="{input-http,filter-app-b,output-opensearch}"
With these values, application-a-pipeline receives logs from HTTP and RabbitMQ, parses them with filter-app-a.cfg, and sends them to both OpenSearch and an HTTP server. application-b-pipeline receives logs only from HTTP, parses them with filter-app-b.cfg, and sends them only to OpenSearch.
Advantages of the Modular Structure#
No duplicated code. The same module can be used by many pipelines, for example one
inputand oneoutputshared by all of them.Flexible composition. A pipeline can load several modules of the same type, such as two outputs and three inputs.
Simpler filters. Each
filterfile holds only one application's parsing logic, with no conditionals on tags to decide which logic to run.Easier troubleshooting. Each application keeps its own pipeline, so the Logstash metrics API still shows which one is slow or failing.
Easier tests. Because inputs and outputs are chosen by an environment variable, you can test a
filterwith a mockinputandstdoutas theoutput, and check that the logic works before deploying it.
I created a Docker Compose lab to try this structure. You can find it on my GitHub: c-neto/my-devops-labs/blog/2023-11-12.
When the Modular Structure Is Worth It#
Logstash handles both small and large workloads, and this structure is one way, not the only way, to deal with pipelines that keep growing. With only a few applications, it can add complexity you don't need.
I have used this structure and seen a big improvement in maintainability, especially in testing: I can test my filter logic by changing a single environment variable. My main goal is always to make pipelines easier to maintain, but every context has its own challenges. I recommend this structure when pipeline maintenance is getting hard, duplicated code is growing, and the Logstash configuration has become error-prone.
If you are sending these logs to OpenSearch, two related posts may help:
Logstash Sniffing for spreading ingestion across a scalable cluster, and
Logstash DB Enrichment for adding database data to events inside a filter.
Key Takeaways#
A Logstash pipeline has three sections:
input,filter, andoutput.One shared pipeline with conditionals overloads the
filtersection. One file per application duplicatesinputandoutput.Split each section into its own file, and compose each application's pipeline from those modules.
path.configwith an environment variable and a glob pattern such as{input-http,filter-app-a,output-opensearch}selects the modules per pipeline.Separate pipelines keep the Logstash metrics API useful for troubleshooting each application.
Swapping inputs and outputs through an environment variable makes it easy to test
filterlogic with mock data andstdout.With only a few applications, a single simple pipeline may be enough.
References#
I'm Carlos Neto, and I've been running OpenSearch on EKS for over five years. More about me on my home page. If you have questions or a different approach, contact me on LinkedIn.