Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add option to encode as binary RubyString instead of base64 #30

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/logstash/codecs/avro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,29 @@ class LogStash::Codecs::Avro < LogStash::Codecs::Base
# tag events with `_avroparsefailure` when decode fails
config :tag_on_failure, :validate => :boolean, :default => false

config :encode_as_base64, :validate => :boolean, :default => true

def open_and_read(uri_string)
open(uri_string).read
end

public
def register
@schema = Avro::Schema.parse(open_and_read(schema_uri))
if encode_as_base64
define_singleton_method :encode do |event|
buffer = StringIO.new
write_event(event, buffer)
@on_event.call(event, Base64.strict_encode64(buffer.string))
end
else
define_singleton_method :encode do |event|
buffer = StringIO.new
buffer.set_encoding("ASCII-8BIT")
write_event(event, buffer)
@on_event.call(event, buffer.string)
end
end
end

public
Expand All @@ -84,12 +100,8 @@ def decode(data)
end
end

public
def encode(event)
dw = Avro::IO::DatumWriter.new(@schema)
buffer = StringIO.new
encoder = Avro::IO::BinaryEncoder.new(buffer)
dw.write(event.to_hash, encoder)
@on_event.call(event, Base64.strict_encode64(buffer.string))
private
def write_event(event, buffer)
Avro::IO::DatumWriter.new(@schema).write(event.to_hash, Avro::IO::BinaryEncoder.new(buffer))
end
end