Class: LogStash::Plugin

Inherits:
Object
  • Object
show all
Includes:
Config::Mixin, Util::Loggable
Defined in:
lib/logstash/plugin.rb

Constant Summary collapse

NL =
"\n"

Constants included from Config::Mixin

Config::Mixin::PLUGIN_VERSION_0_9_0, Config::Mixin::PLUGIN_VERSION_1_0_0

Constants included from Util::SubstitutionVariables

Util::SubstitutionVariables::SUBSTITUTION_PLACEHOLDER_REGEX

Instance Attribute Summary collapse

Attributes included from Config::Mixin

#config, #original_params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config::Mixin

#config_init, included

Methods included from Util::SubstitutionVariables

#deep_replace, #replace_placeholders

Constructor Details

#initialize(params = nil) ⇒ Plugin

Returns a new instance of Plugin.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/logstash/plugin.rb', line 47

def initialize(params=nil)
  @logger = self.logger
  # need to access settings statically because plugins are initialized in config_ast with no context.
  settings = LogStash::SETTINGS
  @slow_logger = self.slow_logger(settings.get("slowlog.threshold.warn"),
                                  settings.get("slowlog.threshold.info"),
                                  settings.get("slowlog.threshold.debug"),
                                  settings.get("slowlog.threshold.trace"))
  @params = LogStash::Util.deep_clone(params)
  # The id should always be defined normally, but in tests that might not be the case
  # In the future we may make this more strict in the Plugin API
  @params["id"] ||= "#{self.class.config_name}_#{SecureRandom.uuid}"
end

Instance Attribute Details

#execution_contextObject

Returns the value of attribute execution_context.



11
12
13
# File 'lib/logstash/plugin.rb', line 11

def execution_context
  @execution_context
end

#paramsObject

Returns the value of attribute params.



11
12
13
# File 'lib/logstash/plugin.rb', line 11

def params
  @params
end

Class Method Details

.lookup(type, name) ⇒ Object

This is keep for backward compatibility, the logic was moved into the registry class but some plugins use this method to return a specific instance on lookup

Should I remove this now and make sure the pipeline invoke the Registry or I should wait for 6.0 Its not really part of the public api but its used by the tests a lot to mock the plugins.



141
142
143
144
# File 'lib/logstash/plugin.rb', line 141

def self.lookup(type, name)
  require "logstash/plugins/registry"
  LogStash::PLUGIN_REGISTRY.lookup_pipeline_plugin(type, name)
end

.reloadable?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/logstash/plugin.rb', line 107

def self.reloadable?
  true
end

Instance Method Details

#closeObject

Subclasses should implement this close method if you need to perform any special tasks during shutdown (like flushing, etc.)



84
85
86
# File 'lib/logstash/plugin.rb', line 84

def close
  # ..
end

#config_nameString

return the configured name of this plugin

Returns:

  • (String)

    The name of the plugin defined by ‘config_name`



132
133
134
# File 'lib/logstash/plugin.rb', line 132

def config_name
  self.class.config_name
end

#debug_infoObject



111
112
113
# File 'lib/logstash/plugin.rb', line 111

def debug_info
  [self.class.to_s, original_params]
end

#do_closeObject

close is called during shutdown, after the plugin worker main task terminates



73
74
75
76
77
78
79
80
# File 'lib/logstash/plugin.rb', line 73

def do_close
  @logger.debug("Closing", :plugin => self.class.name)
  begin
    close
  ensure
    LogStash::PluginMetadata.delete_for_plugin(self.id)
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/logstash/plugin.rb', line 43

def eql?(other)
  self.class.name == other.class.name && @params == other.params
end

#hashObject



38
39
40
41
# File 'lib/logstash/plugin.rb', line 38

def hash
  params.hash ^
  self.class.name.hash
end

#idString

Return a uniq ID for this plugin configuration, by default we will generate a UUID

If the user defines a ‘id => ’ABC’‘ in the configuration we will return

Returns:

  • (String)

    A plugin ID



67
68
69
# File 'lib/logstash/plugin.rb', line 67

def id
  @params["id"]
end

#inspectObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/logstash/plugin.rb', line 92

def inspect
  if !@params.nil?
    description = @params
      .reject { |k, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
      .collect { |k, v| "#{k}=>#{v.inspect}" }
    return "<#{self.class.name} #{description.join(", ")}>"
  else
    return "<#{self.class.name} --->"
  end
end

#metricObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/logstash/plugin.rb', line 119

def metric
  # We can disable metric per plugin if we want in the configuration
  # we will use the NullMetric in this case.
  @metric_plugin ||= if @enable_metric
                       # Fallback when testing plugin and no metric collector are correctly configured.
                       @metric.nil? ? LogStash::Instrument::NamespacedNullMetric.new : @metric
                     else
                       LogStash::Instrument::NamespacedNullMetric.new(@metric, :null)
                     end
end

#metric=(new_metric) ⇒ Object



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

def metric=(new_metric)
  @metric = new_metric
end

#plugin_metadataLogStash::PluginMetadata

Returns this plugin’s metadata key/value store.

@usage: ~~~ if defined?(plugin_metadata)

.set(:foo, 'value')

end ~~~



160
161
162
# File 'lib/logstash/plugin.rb', line 160

def 
  LogStash::PluginMetadata.for_plugin(self.id)
end

#reloadable?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/logstash/plugin.rb', line 103

def reloadable?
  self.class.reloadable?
end

#to_sObject



88
89
90
# File 'lib/logstash/plugin.rb', line 88

def to_s
  return "#{self.class.name}: #{@params}"
end