Class: OkComputer::HttpCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/ok_computer/built_in_checks/http_check.rb

Overview

Performs a health check by reading a URL over HTTP. A successful response is considered passing. To implement your own pass/fail criteria, inherit from this class, override #check, and call #perform_request to get the response body.

Direct Known Subclasses

ElasticsearchCheck, SolrCheck

Constant Summary collapse

ConnectionFailed =
Class.new(StandardError)

Constants inherited from Check

Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from Check

#failure_occurred, #message, #registrant_name, #time

Instance Method Summary collapse

Methods inherited from Check

#<=>, #clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text, #with_benchmarking

Constructor Details

#initialize(url, request_timeout = 5) ⇒ HttpCheck

Public: Initialize a new HTTP check.

url - The URL to check request_timeout - How long to wait to connect before timing out. Defaults to 5 seconds.



18
19
20
21
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 18

def initialize(url, request_timeout = 5)
  parse_url(url)
  self.request_timeout = request_timeout.to_i
end

Instance Attribute Details

#basic_auth_passwordObject

Returns the value of attribute basic_auth_password.



12
13
14
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 12

def basic_auth_password
  @basic_auth_password
end

#basic_auth_usernameObject

Returns the value of attribute basic_auth_username.



12
13
14
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 12

def basic_auth_username
  @basic_auth_username
end

#request_timeoutObject

Returns the value of attribute request_timeout.



12
13
14
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 12

def request_timeout
  @request_timeout
end

#urlObject

Returns the value of attribute url.



12
13
14
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 12

def url
  @url
end

Instance Method Details

#basic_auth_optionsObject



58
59
60
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 58

def basic_auth_options
  [self.basic_auth_username, self.basic_auth_password]
end

#checkObject

Public: Return the status of the HTTP check



24
25
26
27
28
29
30
31
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 24

def check
  if perform_request
    mark_message "HTTP check successful"
  end
rescue => e
  mark_message "Error: '#{e}'"
  mark_failure
end

#parse_url(url) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 50

def parse_url(url)
  self.url = URI.parse(url)
  if self.url.userinfo
    self.basic_auth_username, self.basic_auth_password = self.url.userinfo.split(':')
    self.url.userinfo = ''
  end
end

#perform_requestObject

Public: Actually performs the request against the URL. Returns response body if the request was successful. Otherwise raises a HttpCheck::ConnectionFailed error.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 36

def perform_request
  Timeout.timeout(request_timeout) do
    options = { read_timeout: request_timeout }

    if basic_auth_options.any?
      options[:http_basic_authentication] = basic_auth_options
    end

    url.read(options)
  end
rescue => e
  raise ConnectionFailed, e
end