Class: Metrician::NetHttp

Inherits:
Reporter show all
Defined in:
lib/metrician/reporters/net_http.rb

Constant Summary collapse

REQUEST_METRIC =
"outgoing_request"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reporter

all, inherited

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/metrician/reporters/net_http.rb', line 8

def self.enabled?
  !!defined?(Net::HTTP) &&
    Metrician.configuration[:external_service][:enabled]
end

Instance Method Details

#instrumentObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/metrician/reporters/net_http.rb', line 13

def instrument
  return if ::Net::HTTP.method_defined?(:request_with_metrician_time)
  ::Net::HTTP.class_eval do
    def request_with_metrician_time(req, body = nil, &block)
      start_time = Time.now
      begin
        request_without_metrician_time(req, body, &block)
      ensure
        Metrician.gauge(REQUEST_METRIC, (Time.now - start_time).to_f) if Metrician.configuration[:external_service][:request][:enabled]
      end
    end
    alias_method :request_without_metrician_time, :request
    alias_method :request, :request_with_metrician_time
  end
end