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

Methods included from Util::Loggable

included, #logger, #slow_logger

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



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

def initialize
  @registry = {}
  @hooks = HooksRegistry.new
end

Instance Attribute Details

#hooksObject (readonly)

Returns the value of attribute hooks.



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

def hooks
  @hooks
end

Instance Method Details

#add(type, name, klass) ⇒ Object



200
201
202
203
# File 'lib/logstash/plugins/registry.rb', line 200

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



109
110
111
112
113
# File 'lib/logstash/plugins/registry.rb', line 109

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)


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

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

#get(type, plugin_name) ⇒ Object



209
210
211
# File 'lib/logstash/plugins/registry.rb', line 209

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

#lazy_add(type, name, klass) ⇒ Object



195
196
197
198
# File 'lib/logstash/plugins/registry.rb', line 195

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.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/logstash/plugins/registry.rb', line 153

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/logstash/plugins/registry.rb', line 119

def load_available_plugins
  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

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



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

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

  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.klass, plugin_name)
  end

  return plugin.klass
end

#lookup_pipeline_plugin(type, name) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/logstash/plugins/registry.rb', line 186

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



115
116
117
# File 'lib/logstash/plugins/registry.rb', line 115

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

#remove(type, plugin_name) ⇒ Object



205
206
207
# File 'lib/logstash/plugins/registry.rb', line 205

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

#setup!Object



104
105
106
107
# File 'lib/logstash/plugins/registry.rb', line 104

def setup!
  load_available_plugins
  execute_universal_plugins
end

#sizeObject



217
218
219
# File 'lib/logstash/plugins/registry.rb', line 217

def size
  @registry.size
end