Class: HTTP::Features::Instrumentation
- Inherits:
-
HTTP::Feature
- Object
- HTTP::Feature
- HTTP::Features::Instrumentation
- 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
-
#error_name ⇒ String
readonly
The event name for errors.
-
#instrumenter ⇒ #instrument
readonly
The instrumenter instance.
-
#name ⇒ String
readonly
The event name for requests.
Instance Method Summary collapse
-
#around_request(request) { ... } ⇒ HTTP::Response
Wraps the HTTP exchange with instrumentation.
-
#initialize(instrumenter: NullInstrumenter.new, namespace: "http") ⇒ Instrumentation
constructor
Initializes the Instrumentation feature.
-
#on_error(request, error) ⇒ Object
Instruments a request error.
Methods inherited from HTTP::Feature
#on_request, #wrap_request, #wrap_response
Constructor Details
#initialize(instrumenter: NullInstrumenter.new, namespace: "http") ⇒ Instrumentation
Initializes the Instrumentation feature
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_name ⇒ String (readonly)
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
29 30 31 |
# File 'lib/http/features/instrumentation.rb', line 29 def instrumenter @instrumenter end |
#name ⇒ String (readonly)
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).
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
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 |