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::ENV_PLACEHOLDER_REGEX, Config::Mixin::PLUGIN_VERSION_0_9_0, Config::Mixin::PLUGIN_VERSION_1_0_0

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, #replace_env_placeholders

Methods included from Util::Loggable

included, #logger, #slow_logger

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

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



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

def self.lookup(type, name)
  LogStash::PLUGIN_REGISTRY.lookup_pipeline_plugin(type, name)
end

.reloadable?Boolean

Returns:

  • (Boolean)


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

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



80
81
82
# File 'lib/logstash/plugin.rb', line 80

def close
  # ..
end

#config_nameString

return the configured name of this plugin

Returns:

  • (String)

    The name of the plugin defined by ‘config_name`



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

def config_name
  self.class.config_name
end

#debug_infoObject



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

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
# File 'lib/logstash/plugin.rb', line 73

def do_close
  @logger.debug("closing", :plugin => self.class.name)
  close
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



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

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



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

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



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

def metric=(new_metric)
  @metric = new_metric
end

#reloadable?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/logstash/plugin.rb', line 99

def reloadable?
  self.class.reloadable?
end

#to_sObject



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

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