Class: Syslogstash::LogstashWriter

Inherits:
Object
  • Object
show all
Includes:
Worker
Defined in:
lib/syslogstash/logstash_writer.rb

Overview

Write messages to one of a collection of logstash servers.

Instance Method Summary collapse

Methods included from Worker

#stop, #thread, #wait

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

#runObject

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 { send_messages }
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