class OkComputer::HttpCheck

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.

Constants

ConnectionFailed

Attributes

basic_auth_password[RW]
basic_auth_username[RW]
request_timeout[RW]
url[RW]

Public Class Methods

new(url, request_timeout = 5) click to toggle source

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.

# 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

Public Instance Methods

basic_auth_options() click to toggle source
# 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
check() click to toggle source

Public: Return the status of the HTTP check

# 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) click to toggle source
# 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() click to toggle source

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

# 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