lib/fluent/plugin/formatter_protobuf.rb in fluent-plugin-formatter-protobuf-0.1.0 vs lib/fluent/plugin/formatter_protobuf.rb in fluent-plugin-formatter-protobuf-0.1.1
- old
+ new
@@ -20,10 +20,11 @@
require 'pathname'
require 'google/protobuf'
require 'fluent/env'
require 'fluent/time'
+require 'oj'
module Fluent
module Plugin
# ProtobufFormatter for Fluentd
class ProtobufFormatter < Fluent::Plugin::Formatter
@@ -31,10 +32,13 @@
config_param :include_paths, :array, default: [], desc: 'Generated Ruby Protobuf class files path'
config_param :class_name, :string, desc: 'Ruby Protobuf class name. Used to encode into Protobuf binary'
+ config_param :decode_json, :bool, default: false,
+ desc: 'Serializes record from canonical proto3 JSON mapping (https://developers.google.com/protocol-buffers/docs/proto3#json) into binary' # rubocop:disable Layout/LineLength
+
def configure(conf)
super(conf)
raise Fluent::ConfigError, "Missing 'include_paths'" if @include_paths.empty?
@@ -49,10 +53,10 @@
def formatter_type
:binary
end
def format(_tag, _time, record)
- protobuf_msg = @protobuf_class.new(record)
+ protobuf_msg = @decode_json ? @protobuf_class.decode_json(Oj.dump(record)) : @protobuf_class.new(record)
@protobuf_class.encode(protobuf_msg)
end
def require_proto!(filename)
if Pathname.new(filename).absolute?