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.



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

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

Instance Attribute Details

#hooksObject (readonly)

Returns the value of attribute hooks.



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

def hooks
  @hooks
end

Instance Method Details

#add(type, name, klass) ⇒ Object



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

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



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

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)


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

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

#get(type, plugin_name) ⇒ Object



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

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

#lazy_add(type, name, klass) ⇒ Object



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

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.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/logstash/plugins/registry.rb', line 146

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/logstash/plugins/registry.rb', line 112

def load_available_plugins
  GemRegistry.logstash_plugins.each do |plugin_context|
    # When a plugin has a HOOK_FILE defined, its the responsability 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



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/logstash/plugins/registry.rb', line 129

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



174
175
176
177
178
179
180
181
# File 'lib/logstash/plugins/registry.rb', line 174

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

#setup!Object



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

def setup!
  load_available_plugins
  execute_universal_plugins
end

#sizeObject



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

def size
  @registry.size
end