Class: Fluent::Plugin::Base

Inherits:
Object
  • Object
show all
Includes:
Configurable, SystemConfig::Mixin
Defined in:
lib/fluent/plugin/base.rb

Defined Under Namespace

Classes: State

Constant Summary

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, #configure_proxy_generate, #configured_section_create, included, lookup_type, register_type

Constructor Details

#initializeBase

Returns a new instance of Base.



31
32
33
34
35
36
37
38
39
# File 'lib/fluent/plugin/base.rb', line 31

def initialize
  @log = nil
  super
  @fluentd_lock_dir = ENV['FLUENTD_LOCK_DIR']
  @_state = State.new(false, false, false, false, false, false, false, false, false)
  @_context_router = nil
  @_fluentd_worker_id = nil
  @under_plugin_development = false
end

Instance Attribute Details

#under_plugin_developmentObject

Returns the value of attribute under_plugin_development.



29
30
31
# File 'lib/fluent/plugin/base.rb', line 29

def under_plugin_development
  @under_plugin_development
end

Instance Method Details

#acquire_worker_lock(name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fluent/plugin/base.rb', line 77

def acquire_worker_lock(name)
  if @fluentd_lock_dir.nil?
    raise InvalidLockDirectory, "can't acquire lock because FLUENTD_LOCK_DIR isn't set"
  end
  lock_path = get_lock_path(name)
  File.open(lock_path, "w") do |f|
    f.flock(File::LOCK_EX)
    yield
  end
  # Update access time to prevent tmpwatch from deleting a lock file.
  FileUtils.touch(lock_path);
end

#after_shutdownObject



137
138
139
140
# File 'lib/fluent/plugin/base.rb', line 137

def after_shutdown
  @_state.after_shutdown = true
  self
end

#after_shutdown?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/fluent/plugin/base.rb', line 176

def after_shutdown?
  @_state.after_shutdown
end

#after_startObject



117
118
119
120
# File 'lib/fluent/plugin/base.rb', line 117

def after_start
  @_state.after_start = true
  self
end

#after_started?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/fluent/plugin/base.rb', line 160

def after_started?
  @_state.after_start
end

#before_shutdownObject



127
128
129
130
# File 'lib/fluent/plugin/base.rb', line 127

def before_shutdown
  @_state.before_shutdown = true
  self
end

#before_shutdown?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/fluent/plugin/base.rb', line 168

def before_shutdown?
  @_state.before_shutdown
end

#called_in_test?Boolean

Returns:

  • (Boolean)


188
189
190
191
192
193
194
195
196
197
198
# File 'lib/fluent/plugin/base.rb', line 188

def called_in_test?
  caller_locations.each do |location|
    # Thread::Backtrace::Location#path returns base filename or absolute path.
    # #absolute_path returns absolute_path always.
    # https://bugs.ruby-lang.org/issues/12159
    if /\/test_[^\/]+\.rb$/.match?(location.absolute_path) # location.path =~ /test_.+\.rb$/
      return true
    end
  end
  false
end

#closeObject



142
143
144
145
# File 'lib/fluent/plugin/base.rb', line 142

def close
  @_state.close = true
  self
end

#closed?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/fluent/plugin/base.rb', line 180

def closed?
  @_state.close
end

#configure(conf) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fluent/plugin/base.rb', line 55

def configure(conf)
  raise ArgumentError, "BUG: type of conf must be Fluent::Config::Element, but #{conf.class} is passed." unless conf.is_a?(Fluent::Config::Element)

  if conf.for_this_worker? || (Fluent::Engine.supervisor_mode && !conf.for_every_workers?)
    system_config_override(workers: conf.target_worker_ids.size)
  end

  super(conf, system_config.strict_config_value)
  @_state ||= State.new(false, false, false, false, false, false, false, false, false)
  @_state.configure = true
  self
end

#configured?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/fluent/plugin/base.rb', line 152

def configured?
  @_state.configure
end

#context_routerObject



102
103
104
# File 'lib/fluent/plugin/base.rb', line 102

def context_router
  @_context_router
end

#context_router=(router) ⇒ Object



98
99
100
# File 'lib/fluent/plugin/base.rb', line 98

def context_router=(router)
  @_context_router = router
end

#fluentd_worker_idObject



49
50
51
52
53
# File 'lib/fluent/plugin/base.rb', line 49

def fluentd_worker_id
  return @_fluentd_worker_id if @_fluentd_worker_id
  @_fluentd_worker_id = (ENV['SERVERENGINE_WORKER_ID'] || 0).to_i
  @_fluentd_worker_id
end

#get_lock_path(name) ⇒ Object



72
73
74
75
# File 'lib/fluent/plugin/base.rb', line 72

def get_lock_path(name)
  name = name.gsub(/[^a-zA-Z0-9]/, "_")
  File.join(@fluentd_lock_dir, "fluentd-#{name}.lock")
end

#has_router?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/fluent/plugin/base.rb', line 41

def has_router?
  false
end

#inspectObject



200
201
202
203
204
205
206
# File 'lib/fluent/plugin/base.rb', line 200

def inspect
  # Plugin instances are sometimes too big to dump because it may have too many thins (buffer,storage, ...)
  # Original commit comment says that:
  #   To emulate normal inspect behavior `ruby -e'o=Object.new;p o;p (o.__id__<<1).to_s(16)'`.
  #   https://github.com/ruby/ruby/blob/trunk/gc.c#L788
  "#<%s:%014x>" % [self.class.name, '0x%014x' % (__id__ << 1)]
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/fluent/plugin/base.rb', line 68

def multi_workers_ready?
  true
end

#plugin_root_dirObject



45
46
47
# File 'lib/fluent/plugin/base.rb', line 45

def plugin_root_dir
  nil # override this in plugin_id.rb
end

#reloadable_plugin?Boolean

Returns:

  • (Boolean)


208
209
210
211
# File 'lib/fluent/plugin/base.rb', line 208

def reloadable_plugin?
  # Engine can't capture all class variables. so it's forbbiden to use class variables in each plugins if enabling reload.
  self.class.class_variables.empty?
end

#shutdownObject



132
133
134
135
# File 'lib/fluent/plugin/base.rb', line 132

def shutdown
  @_state.shutdown = true
  self
end

#shutdown?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/fluent/plugin/base.rb', line 172

def shutdown?
  @_state.shutdown
end

#startObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/fluent/plugin/base.rb', line 106

def start
  # By initialization order, plugin logger is created before set log_event_enabled.
  # It causes '@id' specified plugin, it uses plugin logger instead of global logger, ignores `<label @FLUENT_LOG>` setting.
  # This is adhoc approach but impact is minimal.
  if @log.is_a?(Fluent::PluginLogger) && $log.respond_to?(:log_event_enabled) # log_event_enabled check for tests
    @log.log_event_enabled = $log.log_event_enabled
  end
  @_state.start = true
  self
end

#started?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/fluent/plugin/base.rb', line 156

def started?
  @_state.start
end

#stopObject



122
123
124
125
# File 'lib/fluent/plugin/base.rb', line 122

def stop
  @_state.stop = true
  self
end

#stopped?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/fluent/plugin/base.rb', line 164

def stopped?
  @_state.stop
end

#string_safe_encoding(str) {|str| ... } ⇒ Object

Yields:

  • (str)


90
91
92
93
94
95
96
# File 'lib/fluent/plugin/base.rb', line 90

def string_safe_encoding(str)
  unless str.valid_encoding?
    str = str.scrub('?')
    log.info "invalid byte sequence is replaced in `#{str}`" if self.respond_to?(:log)
  end
  yield str
end

#terminateObject



147
148
149
150
# File 'lib/fluent/plugin/base.rb', line 147

def terminate
  @_state.terminate = true
  self
end

#terminated?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/fluent/plugin/base.rb', line 184

def terminated?
  @_state.terminate
end