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.



49
50
51
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 49

def field_separator
  @field_separator
end

#output_data_typeObject

Returns the value of attribute output_data_type.



48
49
50
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 48

def output_data_type
  @output_data_type
end

Instance Method Details

#configure(conf) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 80

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

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

  @formatter_proc = setup_formatter(conf)
end

#format(tag, time, record) ⇒ Object



112
113
114
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 112

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

#refresh_producerObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 57

def refresh_producer()
  if @zookeeper
    @seed_brokers = []
    z = Zookeeper.new(@zookeeper)
    z.get_children(:path => @zookeeper_path)[:children].each do |id|
      broker = Yajl.load(z.get(:path => @zookeeper_path + "/#{id}")[:data])
      @seed_brokers.push("#{broker['host']}:#{broker['port']}")
    end
    z.close
    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

#setup_formatter(conf) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 116

def setup_formatter(conf)
  if @output_data_type == 'json'
    require 'yajl'
    Proc.new { |tag, time, record| Yajl::Encoder.encode(record) }
  elsif @output_data_type == 'ltsv'
    require 'ltsv'
    Proc.new { |tag, time, record| LTSV.dump(record) }
  elsif @output_data_type == 'msgpack'
    require 'msgpack'
    Proc.new { |tag, time, record| record.to_msgpack }
  elsif @output_data_type =~ /^attr:(.*)$/
    @custom_attributes = $1.split(',').map(&:strip).reject(&:empty?)
    @custom_attributes.unshift('time') if @output_include_time
    @custom_attributes.unshift('tag') if @output_include_tag
    Proc.new { |tag, time, record|
      @custom_attributes.map { |attr|
        record[attr].nil? ? '' : record[attr].to_s
      }.join(@f_separator)
    }
  else
    @formatter = Fluent::Plugin.new_formatter(@output_data_type)
    @formatter.configure(conf)
    Proc.new { |tag, time, record|
      @formatter.format(tag, time, record)
    }
  end
end

#shutdownObject



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

def shutdown
  super
end

#startObject



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

def start
  super
  refresh_producer()
end

#write(chunk) ⇒ Object



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
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fluent/plugin/out_kafka_buffered.rb', line 144

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_proc.call(tag, time, record)
      record_buf_bytes = record_buf.bytesize
      if messages.length > 0 and messages_bytes + record_buf_bytes > @kafka_agg_max_bytes
        log.on_trace { log.trace("#{messages.length} messages send.") }
        @producer.send_messages(messages)
        messages = []
        messages_bytes = 0
      end
      log.on_trace { 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}"
  @producer.close if @producer
  refresh_producer()
  # Raise exception to retry sendind messages
  raise e
end