Class: Fluent::Plugin::ProtobufParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/fluent/plugin/parser_protobuf.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fluent/plugin/parser_protobuf.rb', line 20

def configure(conf)
  super

  if !@include_paths.empty? && !@class_file.nil?
    raise Fluent::ConfigError, "Cannot use `include_paths` and `class_file` at the same time."
  end

  if @include_paths.empty? && @class_file.nil?
    raise Fluent::ConfigError, "Need to specify `include_paths` or `class_file`."
  end

  loading_required = Google::Protobuf::DescriptorPool.generated_pool.lookup(class_name).nil?

  load_protobuf_class(@class_file) if loading_required && @class_file

  include_paths.each {|path| load_protobuf_definition(path)} if !include_paths.empty? && loading_required

  if @protobuf_version == :protobuf2
    @protobuf_descriptor = create_protobuf2_instance(@class_name)
  elsif @protobuf_version == :protobuf3
    @protobuf_descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup(@class_name).msgclass
  end
end

#create_protobuf2_instance(class_name) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/fluent/plugin/parser_protobuf.rb', line 70

def create_protobuf2_instance(class_name)
  unless Object.const_defined?(class_name)
    raise Fluent::ConfigError, "Cannot find class #{class_name}."
  else
    Object.const_get(class_name)
  end
end

#load_protobuf_class(filename) ⇒ Object



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

def load_protobuf_class(filename)
  if Pathname.new(filename).absolute?
    begin
      require filename
    rescue LoadError => e
      raise Fluent::ConfigError, "Unable to load protobuf definition class file: #{filename}. error: #{e.backtrace}"
    end
  else
    begin
      require_relative filename
    rescue LoadError => e
      raise Fluent::ConfigError, "Unable to load protobuf definition class file: #{filename}. error: #{e.backtrace}"
    end
  end
end

#parse(binary) ⇒ Object Also known as: parse_partial_data



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent/plugin/parser_protobuf.rb', line 48

def parse(binary)
  begin
    if @protobuf_version == :protobuf3
      decoded = @protobuf_descriptor.decode(binary.to_s)
      time = @estimate_current_event ? Fluent::EventTime.now : nil
      yield time, decoded.to_h
    elsif @protobuf_version == :protobuf2
      decoded = @protobuf_descriptor.parse(binary.to_s)
      time = @estimate_current_event ? Fluent::EventTime.now : nil
      yield time, decoded.to_hash
    end
  rescue => e
    log.warn("Couldn't decode protobuf: #{e.inspect}, message: #{binary}")
    if @suppress_decoding_error
      yield nil, nil
    else
      raise e
    end
  end
end

#parser_typeObject



44
45
46
# File 'lib/fluent/plugin/parser_protobuf.rb', line 44

def parser_type
  :binary
end