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
28
# File 'lib/http/features/instrumentation.rb', line 24

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

Instance Attribute Details

#error_nameObject (readonly)

Returns the value of attribute error_name.



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

def error_name
  @error_name
end

#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

#on_error(request, error) ⇒ Object



43
44
45
# File 'lib/http/features/instrumentation.rb', line 43

def on_error(request, error)
  instrumenter.instrument(error_name, :request => request, :error => error)
end

#wrap_request(request) ⇒ Object



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

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



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

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