-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add buffered device to write to Logstash HTTP input
- Loading branch information
1 parent
b8f5403
commit 243745f
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters