Class: HTTP::Features::Instrumentation

Inherits:
HTTP::Feature show all
Defined in:
lib/http/features/instrumentation.rb

Overview

Instrument requests and responses. Expects an ActiveSupport::Notifications-compatible instrumenter. Defaults to use a namespace of 'http' which may be overridden with a :namespace param. Emits a single event like "request.{namespace}", eg "request.http". Be sure to specify the instrumenter when enabling the feature:

HTTP .use(instrumentation: ActiveSupport::Notifications.instrumenter) .get("https://example.com/")

Emits two events on every request:

  • start_request.http before the request is made, so you can log the reqest being started
  • request.http after the response is recieved, and contains start and finish so the duration of the request can be calculated.

Defined Under Namespace

Classes: NullInstrumenter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instrumenter: NullInstrumenter.new, namespace: "http") ⇒ Instrumentation

Returns a new instance of Instrumentation.



24
25
26
27
# File 'lib/http/features/instrumentation.rb', line 24

def initialize(instrumenter: NullInstrumenter.new, namespace: "http")
  @instrumenter = instrumenter
  @name = "request.#{namespace}"
end

Instance Attribute Details

#instrumenterObject (readonly)

Returns the value of attribute instrumenter.



22
23
24
# File 'lib/http/features/instrumentation.rb', line 22

def instrumenter
  @instrumenter
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/http/features/instrumentation.rb', line 22

def name
  @name
end

Instance Method Details

#wrap_request(request) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/http/features/instrumentation.rb', line 29

def wrap_request(request)
  # Emit a separate "start" event, so a logger can print the request
  # being run without waiting for a response
  instrumenter.instrument("start_#{name}", :request => request) {}
  instrumenter.start(name, :request => request)
  request
end

#wrap_response(response) ⇒ Object



37
38
39
40
# File 'lib/http/features/instrumentation.rb', line 37

def wrap_response(response)
  instrumenter.finish(name, :response => response)
  response
end