Class: Fluent::Plugin::HttpClient

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

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_url, verify_ssl, headers, statuses, open_timeout, read_timeout, log) ⇒ HttpClient

Returns a new instance of HttpClient.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fluent/plugin/http_client.rb', line 6

def initialize(endpoint_url, verify_ssl, 
  headers, statuses, open_timeout, read_timeout, log)
  @log = log
  @statuses = statuses
  @options = {}

  if !verify_ssl
    @log.warn('SSL verification of the remote VMware Log Intelligence service is turned off. This is serious security risk. Please turn on SSL verification and restart the Fluentd/td-agent process.')
    ctx = OpenSSL::SSL::SSLContext.new
    ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @options = {:ssl_context => ctx}
  end

  timeout_options = {
    :connect_timeout => open_timeout,
    :read_timeout => read_timeout
  }
    
  @conn = HTTP.persistent(endpoint_url)
    .headers(headers)
    .timeout(timeout_options)

  @endpoint_path = HTTP::URI.parse(endpoint_url).path
  @last_429_time = nil
end

Instance Method Details

#check_quotaObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/fluent/plugin/http_client.rb', line 32

def check_quota
  if @last_429_time
    if (Time.new - @last_429_time) < 600
      return false
    end

    @last_429_time = nil
  end
  return true
end

#closeObject



68
69
70
# File 'lib/fluent/plugin/http_client.rb', line 68

def close
  @conn.close
end

#post(data) ⇒ 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
# File 'lib/fluent/plugin/http_client.rb', line 43

def post(data)
  if !check_quota
    return
  end

  begin
    response = @conn.post(@endpoint_path, @options.merge(:body => data))
    response.body.to_s
    if (response.code == 429)
      @log.warn('1GB quota of free account has been reached. Will stop sending data for 1 hour.')
      @last_429_time = Time.new
    else
      @last_429_time = nil
    end
    
    if @statuses.include? response.code.to_i
      # Raise an exception so that fluent will retry based on the configurations.
      fail "Server returned bad status: #{response.code}. #{response.to_s}"
    end

  rescue EOFError, SystemCallError, OpenSSL::SSL::SSLError => e
    @log.warn "http post raises exception: #{e.class}, '#{e.message}'"
  end
end