

Enrich logs with Fluentd filters
What can sit in the middle of Fluentd’s pipeline and change a record on its way past? The answer is the filter section of Fluentd’s configuration — often treated as optional, but essential for log enrichment. A filter is a processing step between input and output that can modify each record: adding fields, rewriting values, or dropping records entirely. Fluentd ships with many built-in filters, but when none of them matches your needs you can write a custom Fluentd plugin. A plugin is a small Ruby file that Fluentd loads at startup to perform one job. A custom filter plugin typically:- inspects each record,
- mutates or adds fields (for example
environmentorteam), - returns the modified record for the rest of the pipeline.

environment: production. If a built-in Fluentd filter won’t do the job, write a small plugin file, drop it into a folder Fluentd can read, add a <filter> stanza to fluent.conf, and restart Fluentd so it loads the plugin.
Example minimal filter stanza in fluent.conf:
Index lifecycle: stop the disk from filling up
Every day Fluentd typically creates a new index (for examplefluentd-2026.10.13). If indices are never removed, disk usage grows until a node runs out of space.


- while an index is being written it is in the hot phase,
- when it reaches a size threshold (e.g. 5 GB) or age threshold (e.g. 30 days) it is rolled over to a new index,
- after a longer retention period (e.g. 90 days) the retired index is deleted.
- rollover: switch from an actively-written index to a new one when size/age limits are hit,
- lifecycle policy: rules that automatically move indices through hot → delete (and other) phases.
Lab: build a custom filter and an ILM policy
Two practical goals:- Create a simple custom Fluentd filter plugin (
my_custom_filter) that addsenvironmentandteamfields. - Create an Elasticsearch ILM policy and template so Fluentd indices rollover at 5 GB or 30 days and are deleted after 90 days.
After adding or changing a Fluentd plugin file, restart the Fluentd process (or pod) so it loads the new plugin. Place plugin files in a directory mounted into the Fluentd container (for example,
/fluentd/plugins) and ensure Fluentd’s config or startup includes that directory.1) Create a simple custom Fluentd filter plugin (Ruby)
Place a file namedmy_custom_filter.rb into Fluentd’s plugins directory. This example adds environment and team fields based on the record’s hostname or Kubernetes labels. Customize the detection logic for your environment.
fluent.conf:
2) Create an Index Lifecycle Management (ILM) policy in Elasticsearch
This ILM policy rolls indices over when they reach 5 GB or 30 days and deletes indices older than 90 days.3) Create an index template that attaches the ILM policy and sets a rollover alias
Use a composable index template (recommended on modern Elasticsearch versions). This template:- matches indices named
fluentd-*, - applies the ILM policy
fluentd_policy, - configures the rollover alias
fluentd-write.
4) Create the initial write index with the alias so rollover can occur
Rollover requires an initial index that is write-enabled for the alias. Createfluentd-000001 and attach the alias fluentd-write:
5) Configure Fluentd to write to the alias
Ensure Fluentd’s Elasticsearch output writes to the rollover alias. This enables Elasticsearch to switch the write target when the ILM rollover condition is met. Example Fluentd output configuration (plugin parameters may vary by plugin version — consult the plugin docs):Sample enriched record arriving in Elasticsearch
After the plugin runs, records should include additional fields:- Fluentd enriches logs before they are indexed.
- Elasticsearch automatically rolls over and deletes indices per the ILM policy, preventing unexpected disk growth.
Test your filter plugin and ILM policy in a staging environment first. Use smaller
max_size/max_age values to validate rollover behavior and confirm enrichment by inspecting sample records in Kibana before rolling changes to production.Two rules to remember
Links and references
- Fluentd docs — plugins and filters: https://docs.fluentd.org/
- Fluentd Elasticsearch output plugin: https://docs.fluentd.org/output/elasticsearch
- Elasticsearch ILM documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/index-lifecycle-management.html
- Kibana docs: https://www.elastic.co/guide/en/kibana/current/index.html