Class: OkComputer::HttpCheck
- 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
Constant Summary collapse
- ConnectionFailed =
Class.new(StandardError)
Constants inherited from Check
Instance Attribute Summary collapse
-
#basic_auth_password ⇒ Object
Returns the value of attribute basic_auth_password.
-
#basic_auth_username ⇒ Object
Returns the value of attribute basic_auth_username.
-
#request_timeout ⇒ Object
Returns the value of attribute request_timeout.
-
#url ⇒ Object
Returns the value of attribute url.
Attributes inherited from Check
#failure_occurred, #message, #registrant_name, #time
Instance Method Summary collapse
- #basic_auth_options ⇒ Object
-
#check ⇒ Object
Public: Return the status of the HTTP check.
-
#initialize(url, request_timeout = 5) ⇒ HttpCheck
constructor
Public: Initialize a new HTTP check.
- #parse_url(url) ⇒ Object
-
#perform_request ⇒ Object
Public: Actually performs the request against the URL.
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_password ⇒ Object
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_username ⇒ Object
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_timeout ⇒ Object
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 |
#url ⇒ Object
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_options ⇒ Object
58 59 60 |
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 58 def [self.basic_auth_username, self.basic_auth_password] end |
#check ⇒ Object
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 "HTTP check successful" end rescue => e "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_request ⇒ Object
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 = { read_timeout: request_timeout } if .any? [:http_basic_authentication] = end url.read() end rescue => e raise ConnectionFailed, e end |