require 'open-uri' require 'benchmark' module Remon module Metrics class Http def initialize(url) @uri = URI.parse(url) end def status(read_timeout: 1, open_timeout: 1) status = nil time = Benchmark.realtime do status = get_status(read_timeout, open_timeout) end return time, status end private def get_status(read_timeout, open_timeout) @uri.open(read_timeout: read_timeout, open_timeout: open_timeout, redirect: false) do |f| f.status[0].to_i end rescue EOFError return 444 rescue Errno::ECONNREFUSED return 502 rescue Net::OpenTimeout return 504 rescue Net::ReadTimeout return 504 rescue OpenURI::HTTPRedirect => e return 301 end end end end