Class: LogStash::Agent

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/agent.rb

Constant Summary collapse

STARTED_AT =
Time.now.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Loggable

included, #slow_logger

Constructor Details

#initialize(settings = LogStash::SETTINGS) ⇒ Agent

initialize method for LogStash::Agent

Parameters:

  • params (Hash)

    potential parameters are: :name [String] - identifier for the agent :auto_reload [Boolean] - enable reloading of pipelines :reload_interval [Integer] - reload pipelines every X seconds



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/logstash/agent.rb', line 33

def initialize(settings = LogStash::SETTINGS)
  @logger = self.class.logger
  @settings = settings
  @auto_reload = setting("config.reload.automatic")

  @pipelines = {}
  @name = setting("node.name")
  @http_host = setting("http.host")
  @http_port = setting("http.port")
  @http_environment = setting("http.environment")
  # Generate / load the persistent uuid
  id

  @config_loader = LogStash::Config::Loader.new(@logger)
  @reload_interval = setting("config.reload.interval")
  @upgrade_mutex = Mutex.new

  @collect_metric = setting("metric.collect")

  # Create the collectors and configured it with the library
  configure_metrics_collectors

  @pipeline_reload_metric = metric.namespace([:stats, :pipelines])
  @instance_reload_metric = metric.namespace([:stats, :reloads])

  @dispatcher = LogStash::EventDispatcher.new(self)
  LogStash::PLUGIN_REGISTRY.hooks.register_emitter(self.class, dispatcher)
  dispatcher.fire(:after_initialize)
end

Instance Attribute Details

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



25
26
27
# File 'lib/logstash/agent.rb', line 25

def dispatcher
  @dispatcher
end

#loggerObject

Returns the value of attribute logger.



26
27
28
# File 'lib/logstash/agent.rb', line 26

def logger
  @logger
end

#metricObject (readonly)

Returns the value of attribute metric.



25
26
27
# File 'lib/logstash/agent.rb', line 25

def metric
  @metric
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/logstash/agent.rb', line 25

def name
  @name
end

#pipelinesObject (readonly)

Returns the value of attribute pipelines.



25
26
27
# File 'lib/logstash/agent.rb', line 25

def pipelines
  @pipelines
end

#settingsObject (readonly)

Returns the value of attribute settings.



25
26
27
# File 'lib/logstash/agent.rb', line 25

def settings
  @settings
end

#webserverObject (readonly)

Returns the value of attribute webserver.



25
26
27
# File 'lib/logstash/agent.rb', line 25

def webserver
  @webserver
end

Instance Method Details

#close_pipeline(id) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/logstash/agent.rb', line 192

def close_pipeline(id)
  pipeline = @pipelines[id]
  if pipeline
    @logger.warn("closing pipeline", :id => id)
    pipeline.close
  end
end

#close_pipelinesObject



200
201
202
203
204
# File 'lib/logstash/agent.rb', line 200

def close_pipelines
  @pipelines.each  do |id, _|
    close_pipeline(id)
  end
end

#executeObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/logstash/agent.rb', line 63

def execute
  @thread = Thread.current # this var is implicilty used by Stud.stop?
  @logger.debug("starting agent")

  start_pipelines
  start_webserver

  return 1 if clean_state?

  Stud.stoppable_sleep(@reload_interval) # sleep before looping

  if @auto_reload
    Stud.interval(@reload_interval) { reload_state! }
  else
    while !Stud.stop?
      if clean_state? || running_pipelines?
        sleep 0.5
      else
        break
      end
    end
  end
end

#idObject



142
143
144
145
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
173
174
# File 'lib/logstash/agent.rb', line 142

def id
  return @id if @id

  uuid = nil
  if ::File.exists?(id_path)
    begin
      uuid = ::File.open(id_path) {|f| f.each_line.first.chomp }
    rescue => e
      logger.warn("Could not open persistent UUID file!",
                  :path => id_path,
                  :error => e.message,
                  :class => e.class.name)
    end
  end

  if !uuid
    uuid = SecureRandom.uuid
    logger.info("No persistent UUID file found. Generating new UUID",
                :uuid => uuid,
                :path => id_path)
    begin
      ::File.open(id_path, 'w') {|f| f.write(uuid) }
    rescue => e
      logger.warn("Could not write persistent UUID file! Will use ephemeral UUID",
                  :uuid => uuid,
                  :path => id_path,
                  :error => e.message,
                  :class => e.class.name)
    end
  end

  @id = uuid
end

#id_pathObject



176
177
178
# File 'lib/logstash/agent.rb', line 176

def id_path
  @id_path ||= ::File.join(settings.get("path.data"), "uuid")
end

#register_pipeline(settings) ⇒ Object

register_pipeline - adds a pipeline to the agent’s state

Parameters:

  • pipeline_id (String)

    pipeline string identifier

  • settings (Hash)

    settings that will be passed when creating the pipeline. keys should be symbols such as :pipeline_workers and :pipeline_batch_delay



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/logstash/agent.rb', line 91

def register_pipeline(settings)
  pipeline_settings = settings.clone
  pipeline_id = pipeline_settings.get("pipeline.id")

  pipeline = create_pipeline(pipeline_settings)
  return unless pipeline.is_a?(LogStash::Pipeline)
  if @auto_reload && !pipeline.reloadable?
    @logger.error(I18n.t("logstash.agent.non_reloadable_config_register"),
                  :pipeline_id => pipeline_id,
                  :plugins => pipeline.non_reloadable_plugins.map(&:class))
    return
  end
  @pipelines[pipeline_id] = pipeline
end

#reload_state!Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/logstash/agent.rb', line 106

def reload_state!
  @upgrade_mutex.synchronize do
    @pipelines.each do |pipeline_id, pipeline|
      next if pipeline.settings.get("config.reload.automatic") == false
      begin
        reload_pipeline!(pipeline_id)
      rescue => e
        @instance_reload_metric.increment(:failures)
        @pipeline_reload_metric.namespace([pipeline_id.to_sym, :reloads]).tap do |n|
          n.increment(:failures)
          n.gauge(:last_error, { :message => e.message, :backtrace => e.backtrace})
          n.gauge(:last_failure_timestamp, LogStash::Timestamp.now)
        end
        @logger.error(I18n.t("oops"), :message => e.message, :class => e.class.name, :backtrace => e.backtrace)
      end
    end
  end
end

#running_pipelinesObject



180
181
182
183
184
# File 'lib/logstash/agent.rb', line 180

def running_pipelines
  @upgrade_mutex.synchronize do
    @pipelines.select {|pipeline_id, _| running_pipeline?(pipeline_id) }
  end
end

#running_pipelines?Boolean

Returns:

  • (Boolean)


186
187
188
189
190
# File 'lib/logstash/agent.rb', line 186

def running_pipelines?
  @upgrade_mutex.synchronize do
    @pipelines.select {|pipeline_id, _| running_pipeline?(pipeline_id) }.any?
  end
end

#shutdownObject



136
137
138
139
140
# File 'lib/logstash/agent.rb', line 136

def shutdown
  stop_collecting_metrics
  stop_webserver
  shutdown_pipelines
end

#stop_collecting_metricsObject



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

def stop_collecting_metrics
  @periodic_pollers.stop
end

#uptimeFixnum

Calculate the Logstash uptime in milliseconds

Returns:

  • (Fixnum)

    Uptime in milliseconds



128
129
130
# File 'lib/logstash/agent.rb', line 128

def uptime
  ((Time.now.to_f - STARTED_AT.to_f) * 1000.0).to_i
end