Fluentd
Search…
Plugin Helper: Storage
The storage plugin helper manages the plugin's internal states.
Here is an example:
1
require 'fluent/plugin/input'
2
3
module Fluent::Plugin
4
class ExampleInput < Input
5
Fluent::Plugin.register_input('awesome_example', self)
6
7
# 1. Load storage helper
8
helpers :storage, :thread
9
10
DEFAULT_STORAGE_TYPE = 'local'
11
12
# Omit `shutdown` and other plugin APIs
13
14
def initialize
15
super
16
17
@storage = nil
18
end
19
20
def configure(conf)
21
super
22
23
# 2. Create storage with unique name
24
config = conf.elements(name: 'storage').first
25
@storage = storage_create(usage: 'awesome_index', conf: config, default_type: DEFAULT_STORAGE_TYPE)
26
end
27
28
def start
29
super
30
31
# 3. Call storage plugin helpers get/put methods
32
@storage.put(:awesome_index, 0) unless @storage.get(:awesome_index)
33
thread_create(:awesome_input_runner, &method(:run))
34
end
35
36
def run
37
while thread_current_running?
38
current_time = Time.now.to_i
39
break unless (thread_current_running? && Time.now.to_i <= current_time)
40
router.emit('awesome', Fluent::Engine.now, generate)
41
sleep 0.1
42
end
43
end
44
45
def generate
46
# 4. Update storage plugin helper's storing value
47
@storage.update(:awesome_index) { |v| v + 1 }
48
end
49
end
50
end
Copied!
The created storage is managed by the plugin helper. No need of storage shutdown code in plugin's shutdown. It shutdowns the created storages automatically.
For more details, see Storage section.

Methods

storage_create(usage: '', type: nil, conf: nil, default_type: nil)

This method executes storage with the given parameters and routine.
    usage: unique string value (default: '')
    type: storage plugin type (default: nil)
    conf: storage plugin configuration (default: nil)
    default_conf: storage plugin default configuration (default: nil)

Storage Plugin Helper Instance Types

Instance Type
Attributes
Raw | persistent && persistent_always? || otherwise | Persistent Wrapper | persistent | Synchronized Wrapper | !synchronized? |

Raw

Storage plugins will handle as-is.

Persistent Wrapper

This wrapper makes the handled storage plugin operate values persistently.

Synchronized Wrapper

This wrapper makes handled storage plugin operate values synchronically.

Common Methods

Storage plugin helper encapsulates storage plugin implementation. Normally, the following methods will be called by the owner plugin through this plugin helper:

load

This method loads persistently stored value.

save

This method saves value persistently.

get(key)

    key: symbol value.
This method obtains stored value by key.

fetch(key, defval)

    key: symbol value.
    defval: default value.
This method obtains the stored value by key. If missing, it returns the default value.

put(key, value)

    key: symbol value.
    value: store value.
This method updates the stored value by key.

delete(key)

    key: symbol value.
This method deletes the stored value by key.

update(key, &block)

    key: symbol value.
    &block: a Proc object.
This method updates the stored value using a Proc object.

Plugins using storage

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 4mo ago