Common filter plugins

This page contains a list of common filter plugins.

mutate

You can use the mutate filter to change the data type of a field. For example, you can use the mutate filter if you’re sending events to OpenSearch and you need to change the data type of a field to match any existing mappings.

To convert the quantity field from a string type to an integer type:

  1. input {
  2. http {
  3. host => "127.0.0.1"
  4. port => 8080
  5. }
  6. }
  7. filter {
  8. mutate {
  9. convert => {"quantity" => "integer"}
  10. }
  11. }
  12. output {
  13. file {
  14. path => "output.txt"
  15. }
  16. }

Sample output

You can see that the type of the quantity field is changed from a string to an integer.

  1. {
  2. "quantity" => 3,
  3. "host" => "127.0.0.1",
  4. "@timestamp" => 2021-05-23T19:02:08.026Z,
  5. "amount" => 10,
  6. "@version" => "1",
  7. "headers" => {
  8. "request_path" => "/",
  9. "connection" => "keep-alive",
  10. "content_length" => "41",
  11. "http_user_agent" => "PostmanRuntime/7.26.8",
  12. "request_method" => "PUT",
  13. "cache_control" => "no-cache",
  14. "http_accept" => "*/*",
  15. "content_type" => "application/json",
  16. "http_version" => "HTTP/1.1",
  17. "http_host" => "127.0.0.1:8080",
  18. "accept_encoding" => "gzip, deflate, br",
  19. "postman_token" => "ffd1cdcb-7a1d-4d63-90f8-0f2773069205"
  20. }
  21. }

Other data types you can convert to are float, string, and boolean values. If you pass in an array, the mutate filter converts all the elements in the array. If you pass a string like “world” to cast to an integer type, the result is 0 and Logstash continues processing events.

Logstash supports a few common options for all filter plugins:

OptionDescription
add_fieldAdds one or more fields to the event.
remove_fieldRemoves one or more events from the field.
add_tagAdds one or more tags to the event. You can use tags to perform conditional processing on events depending on which tags they contain.
remove_tagRemoves one or more tags from the event.

For example, you can remove the host field from the event:

  1. input {
  2. http {
  3. host => "127.0.0.1"
  4. port => 8080
  5. }
  6. }
  7. filter {
  8. mutate {
  9. remove_field => {"host"}
  10. }
  11. }
  12. output {
  13. file {
  14. path => "output.txt"
  15. }
  16. }

grok

With the grok filter, you can parse unstructured data and and structure it into fields. The grok filter uses text patterns to match text in your logs. You can think of text patterns as variables containing regular expressions.

The format of a text pattern is as follows:

  1. %{SYNTAX:SEMANTIC}

SYNTAX is the format a piece of text should be in for the pattern to match. You can enter any of grok’s predefined patterns. For example, you can use the email identifier to match an email address from a given piece of text.

SEMANTIC is an arbitrary name for the matched text. For example, if you’re using the email identifier syntax, you can name it “email.”

The following request consists of the IP address of the visitor, name of the visitor, the timestamp of the request, the HTTP verb and URL, the HTTP status code, and the number of bytes:

  1. 184.252.108.229 - joe [20/Sep/2017:13:22:22 +0200] GET /products/view/123 200 12798

To split this request into different fields:

  1. filter {
  2. grok {
  3. match => { "message" => " %{IP: ip_address} %{USER:identity}
  4. %{USER:auth} \[%{HTTPDATE:reg_ts}\]
  5. \"%{WORD:http_verb}
  6. %{URIPATHPARAM: req_path}
  7. \" %{INT:http_status:int}
  8. %{INT:num_bytes:int}"}
  9. }
  10. }

where:

  • IP: matches the IP address field.
  • USER: matches the user name.
  • WORD: matches the HTTP verb.
  • URIPATHPARAM: matches the URI path.
  • INT: matches the HTTP status field.
  • INT: matches the number of bytes.

This is what the event looks like after the grok filter breaks it down into individual fields:

  1. ip_address: 184.252.108.229
  2. identity: joe
  3. reg_ts: 20/Sep/2017:13:22:22 +0200
  4. http_verb:GET
  5. req_path: /products/view/123
  6. http_status: 200
  7. num_bytes: 12798

For common log formats, you use the predefined patterns defined here⁠—Logstash patterns. You can make any adjustments to the results with the mutate filter.

Common filter plugins - 图1