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.



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

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.



9
10
11
# File 'lib/logstash/plugin.rb', line 9

def execution_context
  @execution_context
end

#paramsObject

Returns the value of attribute params.



9
10
11
# File 'lib/logstash/plugin.rb', line 9

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.



135
136
137
138
# File 'lib/logstash/plugin.rb', line 135

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

.reloadable?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/logstash/plugin.rb', line 101

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.)



78
79
80
# File 'lib/logstash/plugin.rb', line 78

def close
  # ..
end

#config_nameString

return the configured name of this plugin

Returns:

  • (String)

    The name of the plugin defined by ‘config_name`



126
127
128
# File 'lib/logstash/plugin.rb', line 126

def config_name
  self.class.config_name
end

#debug_infoObject



105
106
107
# File 'lib/logstash/plugin.rb', line 105

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

#do_closeObject

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



71
72
73
74
# File 'lib/logstash/plugin.rb', line 71

def do_close
  @logger.debug("Closing", :plugin => self.class.name)
  close
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/logstash/plugin.rb', line 41

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

#hashObject



36
37
38
39
# File 'lib/logstash/plugin.rb', line 36

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



65
66
67
# File 'lib/logstash/plugin.rb', line 65

def id
  @params["id"]
end

#inspectObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/logstash/plugin.rb', line 86

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



113
114
115
116
117
118
119
120
121
122
# File 'lib/logstash/plugin.rb', line 113

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



109
110
111
# File 'lib/logstash/plugin.rb', line 109

def metric=(new_metric)
  @metric = new_metric
end

#reloadable?Boolean

Returns:

  • (Boolean)


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

def reloadable?
  self.class.reloadable?
end

#to_sObject



82
83
84
# File 'lib/logstash/plugin.rb', line 82

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