Skip to content

Commit

Permalink
Add buffered device to write to Logstash HTTP input
Browse files Browse the repository at this point in the history
  • Loading branch information
hle-skillz committed Aug 18, 2020
1 parent b8f5403 commit 243745f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,24 @@ LogStashLogger.new \
verify_hostname: false
```

## HTTP
Supports rudimentary writes (buffered, non-persistent connections) to the[ Logstash HTTP Input](https://www.elastic.co/guide/en/logstash/current/plugins-inputs-http.html):
```ruby
input {
http {
port => 8080
}
}
```

```ruby
LogStashLogger.new \
type: :http,
url: 'http://localhost:8080'
```

Note the parameter is `url` and not `uri`. Relies on [Net:HTTP](https://ruby-doc.org/stdlib-2.7.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-HTTPS) to auto-detect SSL usage from the scheme.

## Custom Log Fields

`LogStashLogger` by default will log a JSON object with the format below.
Expand Down
2 changes: 2 additions & 0 deletions lib/logstash-logger/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Device
autoload :Stderr, 'logstash-logger/device/stderr'
autoload :Balancer, 'logstash-logger/device/balancer'
autoload :MultiDelegator, 'logstash-logger/device/multi_delegator'
autoload :HTTP, 'logstash-logger/device/http'

def self.new(opts)
opts = opts.dup
Expand Down Expand Up @@ -60,6 +61,7 @@ def self.device_klass_for(type)
when :stderr then Stderr
when :multi_delegator then MultiDelegator
when :balancer then Balancer
when :http then HTTP
else fail ArgumentError, 'Invalid device type'
end
end
Expand Down
33 changes: 33 additions & 0 deletions lib/logstash-logger/device/http.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'uri'
require 'net/http'

module LogStashLogger
module Device

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

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

def connect
# no-op
end

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

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

end
end
end
11 changes: 11 additions & 0 deletions spec/device/http_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'logstash-logger'

describe LogStashLogger::Device::HTTP do
include_context 'device'

it "Post event to HTTP" do
expect(Net::HTTP).to receive(:post)
http_device.write('test')
end

end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def connection_type
let(:tcp_device) { LogStashLogger::Device.new(type: :tcp, port: port, sync: true) }
let(:ssl_tcp_device) { LogStashLogger::Device.new(type: :tcp, port: port, ssl_enable: true, sync: true) }
let(:unix_device) { LogStashLogger::Device.new(type: :unix, path: '/tmp/logstash', sync: true) }
let(:http_device) { LogStashLogger::Device.new(type: :http, url: 'http://localhost', sync: true) }

let(:file) { Tempfile.new('test') }
let(:file_device) { LogStashLogger::Device.new(type: :file, path: file.path)}
Expand Down

0 comments on commit 243745f

Please sign in to comment.