Class: Syslogstash::LogstashWriter
- Inherits:
-
Object
- Object
- Syslogstash::LogstashWriter
- Includes:
- Worker
- Defined in:
- lib/syslogstash/logstash_writer.rb
Overview
Write messages to one of a collection of logstash servers.
Instance Method Summary collapse
-
#initialize(servers, backlog, metrics) ⇒ LogstashWriter
constructor
Create a new logstash writer.
-
#run ⇒ Object
Start sending messages to logstash servers.
-
#send_entry(e) ⇒ Object
Add an entry to the list of messages to be sent to logstash.
Methods included from Worker
Constructor Details
#initialize(servers, backlog, metrics) ⇒ LogstashWriter
Create a new logstash writer.
Give it a list of servers, and your writer will be ready to go. No messages will actually be delivered, though, until you call #run.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/syslogstash/logstash_writer.rb', line 13 def initialize(servers, backlog, metrics) @servers, @backlog, @metrics = servers.map { |s| URI(s) }, backlog, metrics unless @servers.all? { |url| url.scheme == 'tcp' } raise ArgumentError, "Unsupported URL scheme: #{@servers.select { |url| url.scheme != 'tcp' }.join(', ')}" end @entries = [] @entries_mutex = Mutex.new end |
Instance Method Details
#run ⇒ Object
Start sending messages to logstash servers. This method will return almost immediately, and actual message sending will occur in a separate worker thread.
44 45 46 |
# File 'lib/syslogstash/logstash_writer.rb', line 44 def run @worker = Thread.new { } end |
#send_entry(e) ⇒ Object
Add an entry to the list of messages to be sent to logstash. Actual message delivery will happen in a worker thread that is started with #run.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/syslogstash/logstash_writer.rb', line 29 def send_entry(e) @entries_mutex.synchronize do @entries << { content: e, arrival_timestamp: Time.now } while @entries.length > @backlog @entries.shift @metrics.dropped end end @worker.run if @worker end |