9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/ruby_script_exporter/observers/http.rb', line 9
def observe_http(url, method: :get, expected_response_code: 200, expected_body: nil, timeout: 0.9)
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
response = HTTP.timeout(timeout).public_send(method, url)
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
observe :http_response_time, end_time - start_time
if expected_response_code && expected_response_code != response.code
observe :http_status, 6
return
end
if expected_body && !response.body.to_s.match?(expected_body)
observe :http_status, 7
return
end
observe :http_status, 0
rescue HTTP::ConnectionError => e
case e.message
when /Name or service not known/
observe :http_status, 3 when /Connection refused/
observe :http_status, 1 else
observe :http_status, 4 end
rescue HTTP::ConnectTimeoutError, HTTP::TimeoutError => e
observe :http_status, 2 rescue OpenSSL::SSL::SSLError
observe :http_status, 5 end
|