Class: Syslogstash::LogstashWriter

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

Overview

Write messages to a logstash server.

Defined Under Namespace

Classes: Target

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg, stats) ⇒ LogstashWriter

Create a new logstash writer.

Once the object is created, you’re ready to give it messages by calling #send_entry. No messages will actually be delivered to logstash, though, until you call #run.



17
18
19
20
21
22
23
# File 'lib/syslogstash/logstash_writer.rb', line 17

def initialize(cfg, stats)
	@server_name, @logger, @backlog, @stats = cfg.logstash_server, cfg.logger, cfg.backlog_size, stats

	@entries = []
	@entries_mutex = Mutex.new
	@cs_mutex = Mutex.new
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



9
10
11
# File 'lib/syslogstash/logstash_writer.rb', line 9

def thread
  @thread
end

Instance Method Details

#force_disconnect!Object

Cause the writer to disconnect from the currently-active server.



51
52
53
54
55
56
57
# File 'lib/syslogstash/logstash_writer.rb', line 51

def force_disconnect!
	@cs_mutex.synchronize do
		@logger.info("writer") { "Forced disconnect from #{server_id(@current_server) }" }
		@current_server.close if @current_server
		@current_server = nil
	end
end

#runObject

Start sending messages to logstash servers. This method will return almost immediately, and actual message sending will occur in a separate thread.



45
46
47
# File 'lib/syslogstash/logstash_writer.rb', line 45

def run
	@thread = 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
39
# 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
			@stats.dropped
		end
	end

	@thread.run if @thread
end