Class: Fluent::Plugin::ProtobufFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/fluent/plugin/formatter_protobuf.rb

Overview

ProtobufFormatter for Fluentd

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object

Raises:

  • (Fluent::ConfigError)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fluent/plugin/formatter_protobuf.rb', line 78

def configure(conf)
  super(conf)

  raise Fluent::ConfigError, "Missing 'include_paths'" if @include_paths.empty?

  @include_paths.each { |path| require_proto!(path) } unless @include_paths.empty?

  class_lookup = Google::Protobuf::DescriptorPool.generated_pool.lookup(@class_name)
  raise Fluent::ConfigError, "class name '#{@class_name}' not found" if class_lookup.nil?

  @protobuf_class = class_lookup.msgclass
end

#format(_tag, _time, record) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fluent/plugin/formatter_protobuf.rb', line 95

def format(_tag, _time, record)
  format_record = @format_field == '' ? record : record[@format_field]

  protobuf_msg = if @decode_json
                   @protobuf_class.decode_json(Oj.dump(format_record),
                                               { ignore_unknown_fields: @ignore_unknown_fields })
                 else
                   @protobuf_class.new(format_record)
                 end
  @protobuf_class.encode(protobuf_msg)
end

#formatter_typeObject



91
92
93
# File 'lib/fluent/plugin/formatter_protobuf.rb', line 91

def formatter_type
  :binary
end

#require_proto!(filename) ⇒ Object



107
108
109
110
111
# File 'lib/fluent/plugin/formatter_protobuf.rb', line 107

def require_proto!(filename)
  Kernel.method(@require_method.to_sym).call(filename)
rescue LoadError => e
  raise Fluent::ConfigError, "Unable to load file '#{filename}'. Reason: #{e.message}"
end