Class: LogStashLogger::Device::HTTP

Inherits:
Connectable show all
Defined in:
lib/logstash-logger/device/http.rb

Overview

Rudimentary write to Logstash HTTP Input relying on buffering rather than persistent HTTP connections for efficiency.

Instance Attribute Summary

Attributes inherited from Connectable

#buffer_logger

Attributes inherited from Base

#error_logger, #io, #sync

Instance Method Summary collapse

Methods inherited from Connectable

#close, #connected?, #flush, #on_full_buffer_receive, #reconnect, #reset, #to_io, #with_connection, #write

Methods included from Buffer

#buffer_flush, #buffer_full?, #buffer_initialize, #buffer_receive, #reset_buffer

Methods inherited from Base

#close, #close!, #flush, #reset, #to_io, #unrecoverable_error?, #write

Constructor Details

#initialize(opts) ⇒ HTTP

Returns a new instance of HTTP.



11
12
13
14
# File 'lib/logstash-logger/device/http.rb', line 11

def initialize(opts)
  super
  @url = URI(opts[:url])
end

Instance Method Details

#connectObject



16
17
18
# File 'lib/logstash-logger/device/http.rb', line 16

def connect
  # no-op
end

#write_batch(messages, _group = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/logstash-logger/device/http.rb', line 24

def write_batch(messages, _group = nil)
  # Logstash HTTP input expects JSON array instead of lines of JSON
  body = "[#{messages.join(',')}]"
  resp = Net::HTTP.post @url, body, {"Content-Type" => "application/json"}
  raise resp.message if Net::HTTPError === resp
end

#write_one(message) ⇒ Object



20
21
22
# File 'lib/logstash-logger/device/http.rb', line 20

def write_one(message)
  write_batch([message])
end