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: {instrumenter: 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

Methods inherited from HTTP::Feature

#on_request, #wrap_request, #wrap_response

Constructor Details

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

Initializes the Instrumentation feature

Examples:

Instrumentation.new(instrumenter: ActiveSupport::Notifications.instrumenter)

Parameters:

  • instrumenter (#instrument) (defaults to: NullInstrumenter.new)

    instrumenter instance

  • namespace (String) (defaults to: "http")

    event namespace



58
59
60
61
62
63
# File 'lib/http/features/instrumentation.rb', line 58

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

Instance Attribute Details

#error_nameString (readonly)

The event name for errors

Examples:

feature.error_name # => "error.http"

Returns:

  • (String)

    the event name for errors



47
48
49
# File 'lib/http/features/instrumentation.rb', line 47

def error_name
  @error_name
end

#instrumenter#instrument (readonly)

The instrumenter instance

Examples:

feature.instrumenter

Returns:

  • (#instrument)

    the instrumenter instance



29
30
31
# File 'lib/http/features/instrumentation.rb', line 29

def instrumenter
  @instrumenter
end

#nameString (readonly)

The event name for requests

Examples:

feature.name # => "request.http"

Returns:

  • (String)

    the event name for requests



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

def name
  @name
end

Instance Method Details

#around_request(request) { ... } ⇒ HTTP::Response

Wraps the HTTP exchange with instrumentation

Emits a ‘“start_request.http”` event before the request, then wraps the exchange in a `“request.http”` span that is guaranteed to close on both success and failure (via the instrumenter’s ensure block).

Examples:

feature.around_request(request) { perform_io }

Parameters:

Yields:

  • Executes the HTTP exchange

Yield Returns:

Returns:



79
80
81
82
83
84
85
86
87
88
# File 'lib/http/features/instrumentation.rb', line 79

def around_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) {} # rubocop:disable Lint/EmptyBlock
  instrumenter.instrument(name, request: request) do |payload|
    response = yield request
    payload[:response] = response if payload
    response
  end
end

#on_error(request, error) ⇒ Object

Instruments a request error

Examples:

feature.on_error(request, error)

Parameters:

Returns:

  • (Object)


99
100
101
# File 'lib/http/features/instrumentation.rb', line 99

def on_error(request, error)
  instrumenter.instrument(error_name, request: request, error: error) {} # rubocop:disable Lint/EmptyBlock
end