Class: Fluent::KafkaRestOutput

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

Instance Method Summary collapse

Constructor Details

#initializeKafkaRestOutput

Returns a new instance of KafkaRestOutput.



4
5
6
7
8
9
10
11
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 4

def initialize
  super
  require 'net/https'
  require 'openssl'
  require 'uri'
  require 'yajl'
  require 'base64'
end

Instance Method Details

#configure(conf) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 43

def configure(conf)
  super

  @use_ssl = conf['use_ssl']
  @include_tag = conf['include_tag']
  @include_timestamp = conf['include_timestamp']

  serializers = [:json_bin]  # Should support :avro in the future
  @serializer = if serializers.include? @serializer.intern
                  @serializer.intern
                else
                  :json_bin
                end

  @content_type = conf['content_type']

  # Kafka REST Proxy accepts only POST method at the moment
  http_methods = [:post]
  @http_method = if http_methods.include? @http_method.intern
                  @http_method.intern
                else
                  :post
                end

  @auth = case @authentication
          when 'basic' then :basic
          else
            :none
          end
end

#create_request(tag, time, record) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 117

def create_request(tag, time, record)
  url = format_url(tag, time, record)
  uri = URI.parse(url)
  req = Net::HTTP.const_get(@http_method.to_s.capitalize).new(uri.path)
  set_body(req, tag, time, record)
  set_header(req, tag, time, record)
  return req, uri
end

#emit(tag, es, chain) ⇒ Object



164
165
166
167
168
169
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 164

def emit(tag, es, chain)
  es.each do |time, record|
    handle_record(tag, time, record)
  end
  chain.next
end

#format_url(tag, time, record) ⇒ Object



82
83
84
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 82

def format_url(tag, time, record)
  @endpoint_url
end

#handle_record(tag, time, record) ⇒ Object



159
160
161
162
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 159

def handle_record(tag, time, record)
  req, uri = create_request(tag, time, record)
  send_request(req, uri)
end

#send_request(req, uri) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 126

def send_request(req, uri)    
  is_rate_limited = (@rate_limit_msec != 0 and not @last_request_time.nil?)
  if is_rate_limited and ((Time.now.to_f - @last_request_time) * 1000.0 < @rate_limit_msec)
    $log.info('Dropped request due to rate limiting')
    return
  end
  
  res = nil
  begin
    if @auth and @auth == :basic
      req.basic_auth(@username, @password)
    end
    @last_request_time = Time.now.to_f
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = @use_ssl
    https.ca_file = OpenSSL::X509::DEFAULT_CERT_FILE 
#      https.verify_mode = OpenSSL::SSL::VERIFY_PEER
    https.verify_mode = OpenSSL::SSL::VERIFY_NONE
    res = https.start {|http| http.request(req) }
  rescue IOError, EOFError, SystemCallError
    # server didn't respond
    $log.warn "Net::HTTP.#{req.method.capitalize} raises exception: #{$!.class}, '#{$!.message}'"
  end
  unless res and res.is_a?(Net::HTTPSuccess)
    res_summary = if res
                    "#{res.code} #{res.message} #{res.body}"
                  else
                    "res=nil"
                  end
    $log.warn "failed to #{req.method} #{uri} (#{res_summary})"
  end
end

#set_avro_body(req, data) ⇒ Object



113
114
115
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 113

def set_avro_body(req, data)
  # TODO: Implement avro body parser
end

#set_body(req, tag, time, record) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 86

def set_body(req, tag, time, record)
  # TODO: Add avro support
  if @include_tag
    record['tag'] = tag
  end
  if @include_timestamp
    record['timestamp'] = Time.now.to_i
  end 
  if @serializer == :json_bin
    set_json_body(req, record)
  # elsif @serializer == :avro
  #   set_avro_body(req, record)
  end
  req
end

#set_header(req, tag, time, record) ⇒ Object



102
103
104
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 102

def set_header(req, tag, time, record)
  req
end

#set_json_body(req, data) ⇒ Object



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

def set_json_body(req, data)
  dumped_data = Yajl.dump(data)
  encoded_data = Base64.encode64(dumped_data)
  req.body = Yajl.dump({ "records" => [{ "value" => encoded_data }] })
  req['Content-Type'] = 'application/vnd.kafka.binary.v1+json'
end

#shutdownObject



78
79
80
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 78

def shutdown
  super
end

#startObject



74
75
76
# File 'lib/fluent/plugin/out_kafka_rest.rb', line 74

def start
  super
end