forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#778 from dakrone/edn-codec
Add EDN codec Thanks @dakrone Thanks for making Logstash more awesome!
- Loading branch information
Showing
3 changed files
with
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "logstash/codecs/base" | ||
require "logstash/codecs/line" | ||
|
||
class LogStash::Codecs::EDN < LogStash::Codecs::Base | ||
config_name "edn" | ||
|
||
milestone 1 | ||
|
||
def register | ||
require "edn" | ||
end | ||
|
||
public | ||
def decode(data) | ||
begin | ||
yield LogStash::Event.new(EDN.read(data)) | ||
rescue | ||
@logger.info("EDN parse failure. Falling back to plain-text", :error => e, :data => data) | ||
yield LogStash::Event.new("message" => data) | ||
end | ||
end | ||
|
||
public | ||
def encode(data) | ||
@on_event.call(data.to_hash.to_edn) | ||
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
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,40 @@ | ||
require "logstash/codecs/edn" | ||
require "logstash/event" | ||
require "insist" | ||
require "edn" | ||
|
||
describe LogStash::Codecs::EDN do | ||
subject do | ||
next LogStash::Codecs::EDN.new | ||
end | ||
|
||
context "#decode" do | ||
it "should return an event from edn data" do | ||
data = {"foo" => "bar", "baz" => {"bah" => ["a", "b", "c"]}} | ||
subject.decode(data.to_edn) do |event| | ||
insist { event }.is_a?(LogStash::Event) | ||
insist { event["foo"] } == data["foo"] | ||
insist { event["baz"] } == data["baz"] | ||
insist { event["bah"] } == data["bah"] | ||
end | ||
end | ||
end | ||
|
||
context "#encode" do | ||
it "should return edn data" do | ||
data = {"foo" => "bar", "baz" => {"bah" => ["a","b","c"]}} | ||
event = LogStash::Event.new(data) | ||
got_event = false | ||
subject.on_event do |d| | ||
insist { d.chomp } == LogStash::Event.new(data).to_hash.to_edn | ||
insist { EDN.read(d)["foo"] } == data["foo"] | ||
insist { EDN.read(d)["baz"] } == data["baz"] | ||
insist { EDN.read(d)["bah"] } == data["bah"] | ||
got_event = true | ||
end | ||
subject.encode(event) | ||
insist { got_event } | ||
end | ||
end | ||
|
||
end |