Class: HighLevel::Middleware::Instrumentation

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/high_level/middleware/instrumentation.rb

Overview

Emits an instrumentation event around every request when an instrumenter is configured. The instrumenter is any object responding to #instrument(name, payload, &block)ActiveSupport::Notifications satisfies this, but so does a small custom shim. When no instrumenter is configured the middleware is a transparent pass-through.

Event name: “request.high_level”. Payload: :method, :url, and :status (added after the response returns).

Constant Summary collapse

EVENT =

The instrumentation event name.

"request.high_level"

Instance Method Summary collapse

Constructor Details

#initialize(app, instrumenter:) ⇒ Instrumentation

Returns a new instance of Instrumentation.

Parameters:

  • app (#call)

    the next middleware in the stack

  • instrumenter (#instrument, nil)

    an instrumenter, or nil to make this middleware a pass-through



21
22
23
24
# File 'lib/high_level/middleware/instrumentation.rb', line 21

def initialize(app, instrumenter:)
  super(app)
  @instrumenter = instrumenter
end

Instance Method Details

#call(env) ⇒ Object

Wraps the downstream call in an instrumentation event when an instrumenter is configured.



28
29
30
31
32
33
34
35
36
37
# File 'lib/high_level/middleware/instrumentation.rb', line 28

def call(env)
  return @app.call(env) if @instrumenter.nil?

  payload = { method: env.method, url: env.url.to_s }
  @instrumenter.instrument(EVENT, payload) do
    response = @app.call(env)
    payload[:status] = response.status
    response
  end
end