Class: Observed::Plugins::HTTP

Inherits:
Observer
  • Object
show all
Includes:
Logging, ObserverHelpers::Timer
Defined in:
lib/observed/http.rb

Instance Method Summary collapse

Instance Method Details

#observeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/observed/http.rb', line 21

def observe
  method = get_attribute_value(:method)

  log_debug "method: #{method}, url: #{url}"

  uri = URI.parse(url)

  log_debug "uri: #{uri}, uri.host: #{uri.host}, uri.port:#{uri.port}, uri.path: #{uri.path}"

  http_method = method.capitalize
  path = if uri.path.size == 0
           '/'
         else
           uri.path
         end
  req = Net::HTTP::const_get(http_method.intern).new(path)

  timeout_in_seconds = timeout_in_milliseconds / 1000.0
  if timeout_in_seconds.nan?
    fail "Invalid configuration on timeout: `timeout` must be a number but it was not(=#{timeout_in_seconds})"
  end

  time_and_report(tag: self.tag, timeout_in_seconds: timeout_in_seconds) do

    log_debug "Sending a HTTP request with the timeout of #{timeout_in_seconds} seconds"

    body = Net::HTTP.start(uri.host, uri.port) {|http|
      http.request(req)
    }.body

    log_debug "Response body: #{body}"

    "#{http_method} #{uri}"
  end

end