Sha256: 7d343b31479934610fc5ca98d71282b5bba140ce2ad826c5f32647b1a5d9a5a1

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'observed/http/version'
require 'observed/observer_helpers/timer'
require 'timeout'
require 'net/http'

module Observed
  module Plugins
    class HTTP < Observed::Observer

      include Observed::ObserverHelpers::Timer

      attribute :timeout_in_milliseconds, default: 5000

      attribute :method
      attribute :url

      def observe
        logger.debug "method: #{method}, url: #{url}"

        uri = URI.parse(url)

        logger.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

          logger.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

          logger.debug "Response body: #{body}"

          "#{http_method} #{uri}"
        end

      end

      def logger
        @logger ||= Logger.new(STDOUT)
      end

      plugin_name 'http'

    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
observed-0.1.1 plugins/observed-http/lib/observed/http.rb
observed-http-0.1.0 lib/observed/http.rb