Class: LogStash::Plugins::Registry

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/plugins/registry.rb

Defined Under Namespace

Classes: GemRegistry, PluginRawContext, PluginSpecification, UniversalPluginSpecification, UnknownPlugin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



100
101
102
103
104
105
106
107
108
109
# File 'lib/logstash/plugins/registry.rb', line 100

def initialize
  @mutex = Mutex.new
  # We need a threadsafe class here because we may perform
  # get/set operations concurrently despite the fact we don't use
  # the special atomic methods. That may not be apparent from this file,
  # but it is the case that we can call lookups from multiple threads,
  # when multiple pipelines are in play, and that a lookup may modify the registry.
  @registry = java.util.concurrent.ConcurrentHashMap.new
  @hooks = HooksRegistry.new
end

Instance Attribute Details

#hooksObject (readonly)

Returns the value of attribute hooks.



98
99
100
# File 'lib/logstash/plugins/registry.rb', line 98

def hooks
  @hooks
end

Instance Method Details

#add(type, name, klass) ⇒ Object



219
220
221
222
# File 'lib/logstash/plugins/registry.rb', line 219

def add(type, name, klass)
  logger.debug("Adding plugin to the registry", :name => name, :type => type, :class => klass)
  add_plugin(type, name, klass)
end

#execute_universal_pluginsObject



117
118
119
120
121
# File 'lib/logstash/plugins/registry.rb', line 117

def execute_universal_plugins
  @registry.values
    .select { |specification| specification.is_a?(UniversalPluginSpecification) }
    .each { |specification| specification.register(hooks, LogStash::SETTINGS) }
end

#exists?(type, name) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/logstash/plugins/registry.rb', line 232

def exists?(type, name)
  @registry.include?(key_for(type, name))
end

#get(type, plugin_name) ⇒ Object



228
229
230
# File 'lib/logstash/plugins/registry.rb', line 228

def get(type, plugin_name)
  @registry[key_for(type, plugin_name)]
end

#lazy_add(type, name, klass) ⇒ Object



214
215
216
217
# File 'lib/logstash/plugins/registry.rb', line 214

def lazy_add(type, name, klass)
  logger.debug("On demand adding plugin to the registry", :name => name, :type => type, :class => klass)
  add_plugin(type, name, klass)
end

#legacy_lookup(type, plugin_name) ⇒ Object

The legacy_lookup method uses the 1.5->5.0 file structure to find and match a plugin and will do a lookup on the namespace of the required class to find a matching plugin with the appropriate type.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/logstash/plugins/registry.rb', line 172

def legacy_lookup(type, plugin_name)
  begin
    path = "logstash/#{type}s/#{plugin_name}"

    klass = begin
      namespace_lookup(type, plugin_name)
    rescue UnknownPlugin => e
      # Plugin not registered. Try to load it.
      begin
        require path
        namespace_lookup(type, plugin_name)
      rescue LoadError => e
        logger.error("Tried to load a plugin's code, but failed.", :exception => e, :path => path, :type => type, :name => plugin_name)
        raise
      end
    end

    plugin = lazy_add(type, plugin_name, klass)
  rescue => e
    logger.error("Problems loading a plugin with",
                :type => type,
                :name => plugin_name,
                :path => path,
                :error_message => e.message,
                :error_class => e.class,
                :error_backtrace => e.backtrace)

    raise LoadError, "Problems loading the requested plugin named #{plugin_name} of type #{type}. Error: #{e.class} #{e.message}"
  end

  plugin
end

#load_available_pluginsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/logstash/plugins/registry.rb', line 132

def load_available_plugins
  require "logstash/plugins/builtin"

  GemRegistry.logstash_plugins.each do |plugin_context|
    # When a plugin has a HOOK_FILE defined, its the responsibility of the plugin
    # to register itself to the registry of available plugins.
    #
    # Legacy plugin will lazy register themselves
    if plugin_context.has_hooks?
      begin
        logger.debug("Executing hooks", :name => plugin_context.name, :type => plugin_context.type, :hooks_file => plugin_context.hooks_file)
        plugin_context.execute_hooks!
      rescue => e
        logger.error("error occured when loading plugins hooks file", :name => plugin_context.name, :type => plugin_context.type, :exception => e.message, :stacktrace => e.backtrace )
      end
    end
  end
end

#load_xpackObject



127
128
129
130
# File 'lib/logstash/plugins/registry.rb', line 127

def load_xpack
  logger.info("Loading x-pack")
  require("x-pack/logstash_registry")
end

#lookup(type, plugin_name, &block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/logstash/plugins/registry.rb', line 151

def lookup(type, plugin_name, &block)
  @mutex.synchronize do
    plugin_spec = get(type, plugin_name)
    # Assume that we have a legacy plugin
    if plugin_spec.nil?
      plugin_spec = legacy_lookup(type, plugin_name)
    end

    raise LoadError, "No plugin found with name '#{plugin_name}'" unless plugin_spec

    if block_given? # if provided pass a block to do validation
      raise LoadError, "Block validation fails for plugin named #{plugin_name} of type #{type}," unless block.call(plugin_spec.klass, plugin_name)
    end

    return plugin_spec.klass
  end
end

#lookup_pipeline_plugin(type, name) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/logstash/plugins/registry.rb', line 205

def lookup_pipeline_plugin(type, name)
  LogStash::PLUGIN_REGISTRY.lookup(type, name) do |plugin_klass, plugin_name|
    is_a_plugin?(plugin_klass, plugin_name)
  end
rescue LoadError, NameError => e
  logger.debug("Problems loading the plugin with", :type => type, :name => name)
  raise(LogStash::PluginLoadingError, I18n.t("logstash.pipeline.plugin-loading-error", :type => type, :name => name, :error => e.to_s))
end

#plugins_with_type(type) ⇒ Object



123
124
125
# File 'lib/logstash/plugins/registry.rb', line 123

def plugins_with_type(type)
  @registry.values.select { |specification| specification.type.to_sym == type.to_sym }.collect(&:klass)
end

#remove(type, plugin_name) ⇒ Object



224
225
226
# File 'lib/logstash/plugins/registry.rb', line 224

def remove(type, plugin_name)
  @registry.delete(key_for(type, plugin_name))
end

#setup!Object



111
112
113
114
115
# File 'lib/logstash/plugins/registry.rb', line 111

def setup!
  load_xpack unless LogStash::OSS
  load_available_plugins
  execute_universal_plugins
end

#sizeObject



236
237
238
# File 'lib/logstash/plugins/registry.rb', line 236

def size
  @registry.size
end