module Airbrake::Rack::Instrumentable

Instrumentable holds methods that simplify instrumenting Rack apps. @example

class UsersController
  extend Airbrake::Rack::Instrumentable

  def index
    # ...
  end
  airbrake_capture_timing :index
end

@api public @since v9.2.0

Public Class Methods

chain_capture_timing(klass, method_name, label) click to toggle source

@api private

# File lib/airbrake/rack/instrumentable.rb, line 76
      def self.chain_capture_timing(klass, method_name, label)
        args = method_signature
        visibility = method_visibility(klass, method_name)

        # Generate the wrapper method.
        aliased = method_name.to_s.sub(/([?!=])$/, '')
        punctuation = Regexp.last_match(1)
        wrapped_method_name = "#{aliased}_without_airbrake#{punctuation}"
        needs_removal = method_needs_removal(klass, method_name)
        klass.module_exec do
          alias_method wrapped_method_name, method_name
          remove_method method_name if needs_removal
          # rubocop:disable Style/DocumentDynamicEvalDefinition
          module_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{method_name}(#{args})
              Airbrake::Rack.capture_timing(#{label.to_s.inspect}) do
                __send__("#{aliased}_without_airbrake#{punctuation}", #{args})
              end
            end
            #{visibility} :#{method_name}
          RUBY
          # rubocop:enable Style/DocumentDynamicEvalDefinition
        end
      end
method_needs_removal(klass, method_name) click to toggle source
# File lib/airbrake/rack/instrumentable.rb, line 128
def self.method_needs_removal(klass, method_name)
  klass.method_defined?(method_name, false) ||
    klass.private_method_defined?(method_name, false)
end
method_signature() click to toggle source
# File lib/airbrake/rack/instrumentable.rb, line 117
def self.method_signature
  "*args, **kw_args, &block"
end
method_visibility(klass, method_name) click to toggle source

@api private

# File lib/airbrake/rack/instrumentable.rb, line 102
def self.method_visibility(klass, method_name)
  klass.module_exec do
    if protected_method_defined?(method_name)
      "protected"
    elsif private_method_defined?(method_name)
      "private"
    else
      "public"
    end
  end
end
prepend_capture_timing(klass, method_name, label) click to toggle source

@api private

# File lib/airbrake/rack/instrumentable.rb, line 52
      def self.prepend_capture_timing(klass, method_name, label)
        args = method_signature
        visibility = method_visibility(klass, method_name)

        # Generate the wrapper method.
        klass.module_exec do
          mod = __airbrake_capture_timing_module__
          mod.module_exec do
            # rubocop:disable Style/DocumentDynamicEvalDefinition
            module_eval <<-RUBY, __FILE__, __LINE__ + 1
              def #{method_name}(#{args})
                Airbrake::Rack.capture_timing(#{label.to_s.inspect}) do
                  super
                end
              end
              #{visibility} :#{method_name}
            RUBY
            # rubocop:enable Style/DocumentDynamicEvalDefinition
          end
          prepend mod
        end
      end
should_prepend?(klass, method_name) click to toggle source

@api private

# File lib/airbrake/rack/instrumentable.rb, line 42
def self.should_prepend?(klass, method_name)
  # Don't chain already-prepended or operator methods.
  klass.module_exec do
    self_class_idx = ancestors.index(self)
    method_owner_idx = ancestors.index(instance_method(method_name).owner)
    method_owner_idx < self_class_idx || !(/\A\W/ =~ method_name).nil?
  end
end

Public Instance Methods

airbrake_capture_timing(method_name, label: method_name.to_s) click to toggle source
# File lib/airbrake/rack/instrumentable.rb, line 19
def airbrake_capture_timing(method_name, label: method_name.to_s)
  instrumentable = ::Airbrake::Rack::Instrumentable
  if instrumentable.should_prepend?(self, method_name)
    instrumentable.prepend_capture_timing(self, method_name, label)
  else
    instrumentable.chain_capture_timing(self, method_name, label)
  end
  method_name
end

Private Instance Methods

__airbrake_capture_timing_module__() click to toggle source

@api private

# File lib/airbrake/rack/instrumentable.rb, line 30
def __airbrake_capture_timing_module__
  # Module used to store prepended wrapper methods, saved as an instance
  # variable so each target class/module gets its own module. This just
  # a convenience to avoid prepending a lot of anonymous modules.
  @__airbrake_capture_timing_module__ ||= ::Module.new
end