How to Write Base Plugin
All plugin types are subclasses of Fluent::Plugin::Base
in Fluentd v1 or later. The Base
class has some features and methods that provide the basic mechanism as plugins. This page shows these methods provided by Fluent::Plugin::Base
, and other methods provided commonly in some type of plugins.
The methods listed below are considered as public methods, and will be maintained not to break compatibility. Other methods may be changed without compatibility consideration.
Class Methods
Base
class provides methods to create configurable parameters for plugins. It also supports methods to provide system configurations to plugins.
.config_param(name, type = nil, **options, &block)
.config_param(name, type = nil, **options, &block)
Defines a parameter.
name
: the parameter name as a symboltype
: the parameter typeoptions
: the options for the parameterblock
: if given, convert the value via the given block
For details on types and options, see Types of Configuration Parameters.
Code Example:
.config_set_default(name, default_value)
.config_set_default(name, default_value)
Sets the default value of the parameter specified by name
. If the default value already exists, it raises ArgumentError
.
name
: The name of the parameter.default_value
: Default value of the parameter.
Code Example:
.config_set_desc(name, description)
.config_set_desc(name, description)
Sets description of the parameter specified by name
. If the description already exists, it raises ArgumentError
. For internal use only! Use desc
instead.
name
: The name of the parameter.description
: Description of the parameter.
.desc(description)
.desc(description)
Sets description of the immediately following parameter.
description
: Description of the parameter.
Code Example:
.config_section(name, **options, &block)
.config_section(name, **options, &block)
Defines a section to construct structured (nested) configuration.
name
: The name of the section.options
:root
: Iftrue
, this section is the root section. For internal use only!param_name
: The section name.final
: Iftrue
, the subclass of this class cannot override this section.For example, the third0party plugins cannot override the buffer section.
init
: Iftrue
, the parameters in this section must have default values.Not applicable to third-party plugins.
required
: Iftrue
, the section is required. Fluentd will raiseFluent::ConfigError
if the section is missing.multi
: Iftrue
, users can configure this section multiple times.alias
: Alias for this section.
Code Example:
Configuration Example:
.configured_in(section_name)
.configured_in(section_name)
Inherits section section_name
defined in the super class.
section_name
: The section name as Symbol.
.system_config
.system_config
Returns Fluent::SystemConfig
instance.
For more details, see System Configuration.
Code Example:
.system_config_override(options = {})
.system_config_override(options = {})
Overrides the system configuration.
This is for internal use and plugin testing.
For more details, see System Configuration.
options
: The system configuration as Hash.
Code Example:
Instance Methods
#initialize
#initialize
Initializes the internal states (instance variables).
Call super
if the plugin overrides this method.
Code Example:
#configure(conf)
#configure(conf)
The conf
parameter is an instance of Fluent::Config::Element
. Call super
if the plugin overrides this method. Fluentd's Configurable module (included in Base class) will traverse conf
object, and set values from configurations or default values into the instance variables in super
. So, the instance variables or accessor methods are available after super
in #configure
method.
The code to configure the plugin should be after super
.
Code Example:
NOTE: The return value of this method will be ignored.
#log
#log
Returns Fluent::Log
instance.
Log levels:
trace
debug
info
warn
error
fatal
For more details on Fluentd's logging mechanism, see Logging.
Code Example:
#has_router?
#has_router?
Indicates whether the #router
method exists or not. (default: false
)
If the plugin uses event_emitter
plugin helper, this method will return true
.
NOTE: Input plugin enables event_emitter
by default.
#start
#start
This method is automatically called when Fluentd starts after the configuration. Call super
if the plugin overrides this method.
Creating/opening timers, threads, listening sockets, file handles and others should be done in this method after super
. Many of these may be provided as plugin helpers. See API details of each plugin helper.
Code Example:
#stop
#stop
This method is automatically called first in the shutdown sequence of Fluentd. It should be used to manipulate flags to stop loops, e.g. network servers, gracefully. This method SHOULD NOT do anything that may raise errors.
Call super
if the plugin overrides this method.
Code Example:
super
should be called at last in methods of shutdown sequence: stop
, before_shutdown
, shutdown
, after_shutdown
, close
and terminate
.
#before_shutdown
#before_shutdown
This method is automatically called after #stop
and before #shutdown
. It may be used to control the flushing of buffered events in the shutdown sequence.
Call super
if the plugin overrides this method. The third-party plugins do not need to implement this method in most cases.
#shutdown
#shutdown
This method is automatically called while shutting down. It may be used to close file handles, network connections, listening servers, and other resources that need cleanup. The event can be emitted in this method but not after this method is called.
Call super
if the plugin overrides this method.
Code Example:
#after_shutdown
#after_shutdown
This method is automatically called after #shutdown
. It is used to control the emitting of events in shutdown sequence.
Call super
if the plugin overrides this method. Third-party plugins do not need to implement this method in most cases.
#close
#close
This method may be used to close those resources that cannot be closed in #shutdown
.
Call super
if the plugin overrides this method.
#terminate
#terminate
This method may be used to re-initialize the internal states for the reuse of plugin instances in tests, etc.
Call super
if the plugin overrides this method.
Methods for Input/Filter/Output
Following methods are available in the subclass of Input, Filter and Output:
.helpers(*symbols)
.helpers(*symbols)
Includes the features of the plugin helpers.
Code Example:
It is strongly recommended to call this method at the top of the plugin class definition (just after calling #register_
) to show what plugin helpers this plugin uses explicitly.
#plugin_id
#plugin_id
Provides a unique ID string for the plugin instance. It might be specified by users in the configuration files, or generated automatically. The plugin must not expect any fixed formats for its return value.
#plugin_id_configured?
#plugin_id_configured?
Indicates whether #plugin_id
is configured by users or not.
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 updated