Skip to content

Commit

Permalink
Merge pull request elastic#778 from dakrone/edn-codec
Browse files Browse the repository at this point in the history
Add EDN codec

Thanks @dakrone 

Thanks for making Logstash more awesome!
  • Loading branch information
untergeek committed Nov 20, 2013
2 parents 6c20979 + 7f1dbfa commit 84af5d7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/logstash/codecs/edn.rb
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
1 change: 1 addition & 0 deletions logstash.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "bindata", [">= 1.5.0"] #(ruby license)
gem.add_runtime_dependency "twitter", "5.0.0.rc.1" #(MIT license)
gem.add_runtime_dependency "rsolr" #(Apache 2.0 license)
gem.add_runtime_dependency "edn" #(MIT license)

if RUBY_PLATFORM == 'java'
gem.platform = RUBY_PLATFORM
Expand Down
40 changes: 40 additions & 0 deletions spec/codecs/edn.rb
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

0 comments on commit 84af5d7

Please sign in to comment.