Class: Fluent::KafkaOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_kafka.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKafkaOutput

Returns a new instance of KafkaOutput.



4
5
6
7
# File 'lib/fluent/plugin/out_kafka.rb', line 4

def initialize
  super
  require 'poseidon'
end

Instance Attribute Details

#field_separatorObject

Returns the value of attribute field_separator.



25
26
27
# File 'lib/fluent/plugin/out_kafka.rb', line 25

def field_separator
  @field_separator
end

#output_data_typeObject

Returns the value of attribute output_data_type.



24
25
26
# File 'lib/fluent/plugin/out_kafka.rb', line 24

def output_data_type
  @output_data_type
end

Instance Method Details

#configure(conf) ⇒ Object



55
56
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
# File 'lib/fluent/plugin/out_kafka.rb', line 55

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

#emit(tag, es, chain) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/fluent/plugin/out_kafka.rb', line 129

def emit(tag, es, chain)
  begin
    chain.next
    es.each do |time,record|
      record['time'] = time if @output_include_time
      record['tag'] = tag if @output_include_tag
      topic = record['topic'] || self.default_topic || tag
      partition_key = record['partition_key'] || @default_partition_key
      value = @formatter.nil? ? parse_record(record) : @formatter.format(tag, time, record)
      log.trace("message send to #{topic} with key: #{partition_key} and value: #{value}.")
      message = Poseidon::MessageToSend.new(topic, value, partition_key)
      @producer.send_messages([message])
    end
  rescue Exception => e
    log.warn("Send exception occurred: #{e}")
    refresh_producer()
    raise e
  end
end

#parse_record(record) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fluent/plugin/out_kafka.rb', line 108

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



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

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



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

def shutdown
  super
end

#startObject



99
100
101
102
# File 'lib/fluent/plugin/out_kafka.rb', line 99

def start
  super
  refresh_producer()
end