Fluentd
Search…
Plugin Helper: Record Accessor
The record_accessor plugin helper provides unified access to the event record. It uses jsonpath like syntax for the target field. With this helper, you can easily access/delete a nested field in the plugin.
Here is an example:
1
require 'fluent/plugin/filter'
2
3
module Fluent::Plugin
4
class ExampleFilter < Filter
5
Fluent::Plugin.register_filter('example', self)
6
7
# 1. Load record_accessor helper
8
helpers :record_accessor
9
10
def configure(conf)
11
super
12
13
# 2. Call `record_accessor_create` to create object
14
@accessor = record_accessor_create('$.user.name')
15
end
16
17
# Omit `super`, `shutdown` and other plugin APIs
18
19
def filter(tag, time, record)
20
# 3. Call `call` method to get value
21
value = @accessor.call(record) # With `$.user.name`, access to record["user"]["name"]
22
# ...
23
end
24
end
25
end
Copied!

Syntax

  • dot notation: $. is the starting parameter. Chain fields with dots ..
For example:
$.event.level for record["event"]["level"]
$.key1[0].key2 for record["key1"][0]["key2"]
  • bracket notation: $[ starting parameter. Chain fields with [].
Useful for special characters, ., whitespace, etc.
$['dot.key'][0]['space key'] for record["dot.key"][0]["space key"]
If you set non $. or $[ starting value, e.g. key log, it is the same as record["log"]. So, using record_accessor does not break the existing plugin behavior.

Methods

record_accessor_create(param)

This method returns the accessor object of the event record.
The param is a String.
See the "Syntax" section for more details.
1
record_accessor_create("log")
2
record_accessor_create("$.key1.key2")
3
record_accessor_create("$['key1'][0]['key2']")
Copied!
After creating an object, call call/delete/set method with the record object.
1
accessor.call(record) # get record field
2
accessor.delete(record) # delete record field
3
accessor.set(record, value) # set new value to record field
Copied!
NOTE: set method is supported since v1.10.3

Plugins using record_accessor

If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last modified 6mo ago