The mutate filter in Logstash is used to perform mutations on data fields. For example, to add a field, rename a field, or copy a field to another field.
This tutorial will show you all types of mutations you can perform with the mutate filter in Logstash.
The basic structure of a Logstash pipeline config is as follows:
So you can put the mutate filter in the filter section of the Logstash pipeline.
We will go deeper into each type of mutation in the next sections.
Contents
- 2 Commonly Used Options of the Mutate Filter and Other Filters
- 14 Options of the Mutate Filter
- Conclusion
2 Commonly Used Options of the Mutate Filter and Other Filters
While working with the mutate filter, you will often use the add_field and remove_field options.
Please note that those actions can also be used in other filters like the ruby filter or the grok filter, not just the mutate filter.
1. add_field
In Logstash, the add_field action is used to add a new field to the event.
- Value type: hash
- Default value: {}
The syntax to add a single field is as follows:
To add multiple fields:
For example, we can add a new field age
with the value 21
to the event:
Using the above config will yield the result:
2. remove_field
To remove a field from the event in Logstash, we can use the remove_field action.
- Value type: array
- Default value: []
The syntax is as follows:
You can also remove multiple fields:
For example, we can remove the status
field from the event:
14 Options of the Mutate Filter
The mutate filter can be used in the filter section of the Logstash pipeline, and specify the mutate actions to be performed.
1. coerce
The coerce action is used to set the default value of a field when it is null
.
The code above uses the coerce action to set the value of field_name
to default_value
, so that when the field_name
is null
, it will be set to default_value
.
2. rename
The rename action is used to rename a field.
There are 2 points to remember when using the rename action:
- If the destination field already exists, it will be overwritten.
- If the source field does not exist, the destination field will not be created.
3. update
The update action is used to update the value of a field.
If the field does not exist, nothing will happen.
4. replace
The replace action is used to replace the value of a field and create the field if it does not exist.
The replace
actions is different from the update
action in that it will create the field if it does not exist.
5. convert
The convert action is used to convert the value of a field to another data type, e.g. string to integer.
If the field is an array, the conversion will be applied to all elements of the array.
Valid data types are:
- integer
- integer_eu
- float
- float_eu
- string
- boolean
You can visit the docs for more information about the data types.
6. gsub
The gsub action is used to replace a string with another string.
For example, to remove all dots from a string:
7. uppercase
The uppercase action is used to convert every character of a string or an array of strings field to uppercase.
8. capitalize
The capitalize action is used to capitalize the first character of a string or an array of strings field.
9. lowercase
The lowercase action is used to convert every character of a string or an array of strings field to lowercase.
10. strip
The strip action is used to remove leading and trailing whitespaces from a string or an array of strings field.
11. split
The split action is used to split a string field into an array of strings.
For example, I want to split the message
field by the comma character:
12. join
The join action is used to join an array of strings into a string field.
The field being joined must be an array of strings. Otherwise, the join
action will not work.
For example, I want to join the tags
field by the comma character:
13. merge
The merge action is used to merge two fields into one field.
Valid data types are:
- string + string = array of 2 strings (
"elastic" + "logstash" => ["elastic", "logstash"]
) - array of strings + string = array of strings (
["elastic", "logstash"] + "tutorial" => ["elastic", "logstash", "tutorial"]
)
For example, I want to merge the tags
field and the category
field into the tags
field:
14. copy
The copy action is used to copy the value of a field to another field.
If the destination field already exists, it will be overwritten.
Conclusion
The mutate filter is a powerful plugin of Logstash that helps you to perform several actions on the data.
In this tutorial, we have learned all types of mutations you can perform with the mutate filter.
I hope you find this tutorial useful. Feel free to leave a comment below if you have any questions.
Comments
Be the first to comment!