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, source_loader = nil) ⇒ 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



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/logstash/agent.rb', line 36

def initialize(settings = LogStash::SETTINGS, source_loader = nil)
  @logger = self.class.logger
  @settings = settings
  @auto_reload = setting("config.reload.automatic")
  @ephemeral_id = SecureRandom.uuid

  # Do not use @pipelines directly. Use #with_pipelines which does locking
  @pipelines = {}
  @pipelines_lock = java.util.concurrent.locks.ReentrantLock.new

  @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

  # This is for backward compatibility in the tests
  if source_loader.nil?
    @source_loader = LogStash::Config::SourceLoader.new
    @source_loader.add_source(LogStash::Config::Source::Local.new(@settings))
  else
    @source_loader = source_loader
  end

  # Normalize time interval to seconds
  @reload_interval = setting("config.reload.interval") / 1_000_000_000.0

  @collect_metric = setting("metric.collect")

  # Create the collectors and configured it with the library
  configure_metrics_collectors

  @state_resolver = LogStash::StateResolver.new(metric)

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

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

  @running = Concurrent::AtomicBoolean.new(false)
end

Instance Attribute Details

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



28
29
30
# File 'lib/logstash/agent.rb', line 28

def dispatcher
  @dispatcher
end

#ephemeral_idObject (readonly)

Returns the value of attribute ephemeral_id.



28
29
30
# File 'lib/logstash/agent.rb', line 28

def ephemeral_id
  @ephemeral_id
end

#loggerObject

Returns the value of attribute logger.



29
30
31
# File 'lib/logstash/agent.rb', line 29

def logger
  @logger
end

#metricObject (readonly)

Returns the value of attribute metric.



28
29
30
# File 'lib/logstash/agent.rb', line 28

def metric
  @metric
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/logstash/agent.rb', line 28

def name
  @name
end

#settingsObject (readonly)

Returns the value of attribute settings.



28
29
30
# File 'lib/logstash/agent.rb', line 28

def settings
  @settings
end

#webserverObject (readonly)

Returns the value of attribute webserver.



28
29
30
# File 'lib/logstash/agent.rb', line 28

def webserver
  @webserver
end

Instance Method Details

#auto_reload?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/logstash/agent.rb', line 124

def auto_reload?
  @auto_reload
end

#converge_state_and_updateObject



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
175
176
# File 'lib/logstash/agent.rb', line 147

def converge_state_and_update
  results = @source_loader.fetch

  unless results.success?
    if auto_reload?
      logger.debug("Could not fetch the configuration to converge, will retry", :message => results.error, :retrying_in => @reload_interval)
      return
    else
      raise "Could not fetch the configuration, message: #{results.error}"
    end
  end

  # We Lock any access on the pipelines, since the actions will modify the
  # content of it.
  converge_result = nil

  # we don't use the variable here, but we want the locking
  with_pipelines do |pipelines|
    pipeline_actions = resolve_actions(results.response)
    converge_result = converge_state(pipeline_actions)
    update_metrics(converge_result)
  end

  report_currently_running_pipelines(converge_result)
  dispatch_events(converge_result)

  converge_result
rescue => e
  logger.error("An exception happened when converging configuration", :exception => e.class, :message => e.message, :backtrace => e.backtrace)
end

#executeObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/logstash/agent.rb', line 82

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

  start_webserver

  transition_to_running

  converge_state_and_update

  if auto_reload?
    # `sleep_then_run` instead of firing the interval right away
    Stud.interval(@reload_interval, :sleep_then_run => true) do
      # TODO(ph) OK, in reality, we should get out of the loop, but I am
      # worried about the implication of that change so instead when we are stopped
      # we don't converge.
      #
      # Logstash currently expect to be block here, the signal will force a kill on the agent making
      # the agent thread unblock
      #
      # Actually what we really need is one more state:
      #
      # init => running => stopping => stopped
      converge_state_and_update unless stopped?
    end
  else
    return 1 if clean_state?

    while !Stud.stop?
      if clean_state? || running_user_defined_pipelines?
        sleep(0.5)
      else
        break
      end
    end
  end

  return 0
ensure
  transition_to_stopped
end

#get_pipeline(pipeline_id) ⇒ Object



231
232
233
234
235
# File 'lib/logstash/agent.rb', line 231

def get_pipeline(pipeline_id)
  with_pipelines do |pipelines|
    pipelines[pipeline_id]
  end
end

#idObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/logstash/agent.rb', line 193

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



227
228
229
# File 'lib/logstash/agent.rb', line 227

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

#pipelines_countObject



237
238
239
240
241
# File 'lib/logstash/agent.rb', line 237

def pipelines_count
  with_pipelines do |pipelines|
    pipelines.size
  end
end

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  @running.value
end

#running_pipelines?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/logstash/agent.rb', line 249

def running_pipelines?
  running_pipelines_count > 0
end

#running_pipelines_countObject



253
254
255
256
257
# File 'lib/logstash/agent.rb', line 253

def running_pipelines_count
  with_running_pipelines do |pipelines|
    pipelines.size
  end
end

#running_user_defined_pipelines?Boolean

Returns:

  • (Boolean)


259
260
261
262
263
# File 'lib/logstash/agent.rb', line 259

def running_user_defined_pipelines?
  with_running_user_defined_pipelines do |pipelines|
    pipelines.size > 0
  end
end

#shutdownObject



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

def shutdown
  stop_collecting_metrics
  stop_webserver
  transition_to_stopped
  converge_result = shutdown_pipelines
  converge_result
end

#stopped?Boolean

Returns:

  • (Boolean)


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

def stopped?
  !@running.value
end

#uptimeFixnum

Calculate the Logstash uptime in milliseconds

Returns:

  • (Fixnum)

    Uptime in milliseconds



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

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

#with_pipelinesObject

Safely perform an operation on the pipelines hash Using the correct synchronization



138
139
140
141
142
143
144
145
# File 'lib/logstash/agent.rb', line 138

def with_pipelines
  begin
    @pipelines_lock.lock
    yield @pipelines
  ensure
    @pipelines_lock.unlock
  end
end

#with_running_pipelinesObject



243
244
245
246
247
# File 'lib/logstash/agent.rb', line 243

def with_running_pipelines
  with_pipelines do |pipelines|
    yield pipelines.select {|pipeline_id, _| running_pipeline?(pipeline_id) }
  end
end

#with_running_user_defined_pipelinesObject



265
266
267
268
269
270
271
272
273
# File 'lib/logstash/agent.rb', line 265

def with_running_user_defined_pipelines
  with_pipelines do |pipelines|
    found = pipelines.select do |_, pipeline|
      pipeline.running? && !pipeline.system?
    end

    yield found
  end
end