Module: Fluent::PluginId

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fluent/plugin_id.rb', line 32

def configure(conf)
  @_plugin_id_variable_store = Fluent::VariableStore.fetch_or_build(:pluing_id, default_value: Set.new)
  @id = conf['@id']
  @_id_configured = !!@id # plugin id is explicitly configured by users (or not)
  if @id
    @id = @id.to_s
    if @_plugin_id_variable_store.include?(@id) && !plugin_id_for_test?
      raise Fluent::ConfigError, "Duplicated plugin id `#{@id}`. Check whole configuration and fix it."
    end
    @_plugin_id_variable_store.add(@id)
  end

  super
end

#initializeObject



24
25
26
27
28
29
30
# File 'lib/fluent/plugin_id.rb', line 24

def initialize
  super

  @_plugin_id_variable_store = nil
  @_plugin_root_dir = nil
  @id = nil
end

#plugin_idObject



65
66
67
68
69
70
71
# File 'lib/fluent/plugin_id.rb', line 65

def plugin_id
  if instance_variable_defined?("@id")
    @id || "object:#{object_id.to_s(16)}"
  else
    "object:#{object_id.to_s(16)}"
  end
end

#plugin_id_configured?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/fluent/plugin_id.rb', line 59

def plugin_id_configured?
  if instance_variable_defined?("@_id_configured")
    @_id_configured
  end
end

#plugin_id_for_test?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/plugin_id.rb', line 47

def plugin_id_for_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 location.absolute_path =~ /\/test_[^\/]+\.rb$/ # location.path =~ /test_.+\.rb$/
      return true
    end
  end
  false
end

#plugin_root_dirObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fluent/plugin_id.rb', line 73

def plugin_root_dir
  return @_plugin_root_dir if @_plugin_root_dir
  return nil unless system_config.root_dir
  return nil unless plugin_id_configured?

  # Fluent::Plugin::Base#fluentd_worker_id
  dir = File.join(system_config.root_dir, "worker#{fluentd_worker_id}", plugin_id)
  FileUtils.mkdir_p(dir, mode: system_config.dir_permission || Fluent::DEFAULT_DIR_PERMISSION) unless Dir.exist?(dir)
  @_plugin_root_dir = dir.freeze
  dir
end

#stopObject



85
86
87
88
89
90
91
# File 'lib/fluent/plugin_id.rb', line 85

def stop
  if @_plugin_id_variable_store
    @_plugin_id_variable_store.delete(@id)
  end

  super
end