Module: Airbrake::Rack::Instrumentable

Defined in:
lib/airbrake/rack/instrumentable.rb

Overview

Instrumentable holds methods that simplify instrumenting Rack apps.

Examples:

class UsersController
  extend Airbrake::Rack::Instrumentable

  def index
    # ...
  end
  airbrake_capture_timing :index
end

Since:

  • v9.2.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.chain_capture_timing(klass, method_name, label) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • v9.2.0



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object

Since:

  • v9.2.0



128
129
130
131
# 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_signatureObject

Since:

  • v9.2.0



117
118
119
# File 'lib/airbrake/rack/instrumentable.rb', line 117

def self.method_signature
  "*args, **kw_args, &block"
end

.method_visibility(klass, method_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • v9.2.0



102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • v9.2.0



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • v9.2.0



42
43
44
45
46
47
48
49
# 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

Instance Method Details

#airbrake_capture_timing(method_name, label: method_name.to_s) ⇒ Object

Since:

  • v9.2.0



19
20
21
22
23
24
25
26
27
# 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