Class: Fluent::KafkaOutputBuffered

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_kafka_buffered.rb

Overview

encode: utf-8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKafkaOutputBuffered

Returns a new instance of KafkaOutputBuffered.



5
6
7
8
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 5

def initialize
  super
  require 'poseidon'
end

Instance Attribute Details

#field_separatorObject

Returns the value of attribute field_separator.



27
28
29
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 27

def field_separator
  @field_separator
end

#output_data_typeObject

Returns the value of attribute output_data_type.



26
27
28
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 26

def output_data_type
  @output_data_type
end

Instance Method Details

#configure(conf) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 57

def configure(conf)
  super
  if @zookeeper
    require 'zookeeper'
    require 'yajl'
  else
    @seed_brokers = @brokers.match(",").nil? ? [@brokers] : @brokers.split(",")
    log.info "brokers has been set directly: #{@seed_brokers}"
  end
  if @compression_codec == 'snappy'
    require 'snappy'
  end
  case @output_data_type
  when 'json'
    require 'yajl'
  when 'ltsv'
    require 'ltsv'
  when 'msgpack'
    require 'msgpack'
  end

  @f_separator = case @field_separator
                 when /SPACE/i then ' '
                 when /COMMA/i then ','
                 when /SOH/i then "\x01"
                 else "\t"
                 end

  @custom_attributes = if @output_data_type == 'json'
                         nil
                       elsif @output_data_type == 'ltsv'
                         nil
                       elsif @output_data_type == 'msgpack'
                         nil
                       elsif @output_data_type =~ /^attr:(.*)$/
                         $1.split(',').map(&:strip).reject(&:empty?)
                       else
                         @formatter = Fluent::Plugin.new_formatter(@output_data_type)
                         @formatter.configure(conf)
                         nil
                       end
end

#format(tag, time, record) ⇒ Object



109
110
111
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 109

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#parse_record(record) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 113

def parse_record(record)
  if @custom_attributes.nil?
    case @output_data_type
    when 'json'
      Yajl::Encoder.encode(record)
    when 'ltsv'
      LTSV.dump(record)
    when 'msgpack'
      record.to_msgpack
    else
      record.to_s
    end
  else
    @custom_attributes.unshift('time') if @output_include_time
    @custom_attributes.unshift('tag') if @output_include_tag
    @custom_attributes.map { |attr|
      record[attr].nil? ? '' : record[attr].to_s
    }.join(@f_separator)
  end
end

#refresh_producerObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 35

def refresh_producer()
  if @zookeeper
    @seed_brokers = []
    z = Zookeeper.new(@zookeeper)
    z.get_children(:path => '/brokers/ids')[:children].each do |id|
      broker = Yajl.load(z.get(:path => "/brokers/ids/#{id}")[:data])
      @seed_brokers.push("#{broker['host']}:#{broker['port']}")
    end
    log.info "brokers has been refreshed via Zookeeper: #{@seed_brokers}"
  end
  begin
    if @seed_brokers.length > 0
      @producer = Poseidon::Producer.new(@seed_brokers, @client_id, :max_send_retries => @max_send_retries, :required_acks => @required_acks, :ack_timeout_ms => @ack_timeout_ms, :compression_codec => @compression_codec.to_sym)
      log.info "initialized producer #{@client_id}"
    else
      log.warn "No brokers found on Zookeeper"
    end
  rescue Exception => e
    log.error e
  end
end

#shutdownObject



105
106
107
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 105

def shutdown
  super
end

#startObject



100
101
102
103
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 100

def start
  super
  refresh_producer()
end

#write(chunk) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 134

def write(chunk)
  records_by_topic = {}
  bytes_by_topic = {}
  messages = []
  messages_bytes = 0
  begin
    chunk.msgpack_each { |tag, time, record|
      record['time'] = time if @output_include_time
      record['tag'] = tag if @output_include_tag
      topic = record['topic'] || @default_topic || tag
      partition_key = record['partition_key'] || @default_partition_key

      records_by_topic[topic] ||= 0
      bytes_by_topic[topic] ||= 0

      record_buf = @formatter.nil? ? parse_record(record) : @formatter.format(tag, time, record)
      record_buf_bytes = record_buf.bytesize
      if messages.length > 0 and messages_bytes + record_buf_bytes > @kafka_agg_max_bytes
        log.trace("#{messages.length} messages send.")
        @producer.send_messages(messages)
        messages = []
        messages_bytes = 0
      end
      log.trace("message will send to #{topic} with key: #{partition_key} and value: #{record_buf}.")
      messages << Poseidon::MessageToSend.new(topic, record_buf, partition_key)
      messages_bytes += record_buf_bytes

      records_by_topic[topic] += 1
      bytes_by_topic[topic] += record_buf_bytes
    }
    if messages.length > 0
      log.trace("#{messages.length} messages send.")
      @producer.send_messages(messages)
    end
    log.debug "(records|bytes) (#{records_by_topic}|#{bytes_by_topic})"
  end
rescue Exception => e
  log.warn "Send exception occurred: #{e}"
  refresh_producer()
  # Raise exception to retry sendind messages
  raise e
end