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

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.



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

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

Instance Attribute Details

#hooksObject (readonly)

Returns the value of attribute hooks.



95
96
97
# File 'lib/logstash/plugins/registry.rb', line 95

def hooks
  @hooks
end

Instance Method Details

#add(type, name, klass) ⇒ Object



193
194
195
196
# File 'lib/logstash/plugins/registry.rb', line 193

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



107
108
109
110
111
# File 'lib/logstash/plugins/registry.rb', line 107

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)


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

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

#get(type, plugin_name) ⇒ Object



198
199
200
# File 'lib/logstash/plugins/registry.rb', line 198

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

#lazy_add(type, name, klass) ⇒ Object



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

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.



151
152
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
# File 'lib/logstash/plugins/registry.rb', line 151

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

    begin
      require path
    rescue LoadError
      # Plugin might be already defined in the current scope
      # This scenario often happen in test when we write an adhoc class
    end

    klass = namespace_lookup(type, plugin_name)
    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



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

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



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

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



179
180
181
182
183
184
185
186
# File 'lib/logstash/plugins/registry.rb', line 179

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



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

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

#setup!Object



102
103
104
105
# File 'lib/logstash/plugins/registry.rb', line 102

def setup!
  load_available_plugins
  execute_universal_plugins
end

#sizeObject



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

def size
  @registry.size
end