instrumentation.rb 4.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
module Gitlab
  module Metrics
    # Module for instrumenting methods.
    #
    # This module allows instrumenting of methods without having to actually
    # alter the target code (e.g. by including modules).
    #
    # Example usage:
    #
    #     Gitlab::Metrics::Instrumentation.instrument_method(User, :by_login)
    module Instrumentation
12 13
      SERIES = 'method_calls'

14 15 16 17
      def self.configure
        yield self
      end

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
      # Instruments a class method.
      #
      # mod  - The module to instrument as a Module/Class.
      # name - The name of the method to instrument.
      def self.instrument_method(mod, name)
        instrument(:class, mod, name)
      end

      # Instruments an instance method.
      #
      # mod  - The module to instrument as a Module/Class.
      # name - The name of the method to instrument.
      def self.instrument_instance_method(mod, name)
        instrument(:instance, mod, name)
      end

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
      # Recursively instruments all subclasses of the given root module.
      #
      # This can be used to for example instrument all ActiveRecord models (as
      # these all inherit from ActiveRecord::Base).
      #
      # This method can optionally take a block to pass to `instrument_methods`
      # and `instrument_instance_methods`.
      #
      # root - The root module for which to instrument subclasses. The root
      #        module itself is not instrumented.
      def self.instrument_class_hierarchy(root, &block)
        visit = root.subclasses

        until visit.empty?
          klass = visit.pop

          instrument_methods(klass, &block)
          instrument_instance_methods(klass, &block)

          klass.subclasses.each { |c| visit << c }
        end
      end

57 58
      # Instruments all public methods of a module.
      #
59 60 61 62 63
      # This method optionally takes a block that can be used to determine if a
      # method should be instrumented or not. The block is passed the receiving
      # module and an UnboundMethod. If the block returns a non truthy value the
      # method is not instrumented.
      #
64 65 66
      # mod - The module to instrument.
      def self.instrument_methods(mod)
        mod.public_methods(false).each do |name|
67 68
          method = mod.method(name)

69 70 71 72 73
          if method.owner == mod.singleton_class
            if !block_given? || block_given? && yield(mod, method)
              instrument_method(mod, name)
            end
          end
74 75 76 77 78
        end
      end

      # Instruments all public instance methods of a module.
      #
79 80
      # See `instrument_methods` for more information.
      #
81 82 83
      # mod - The module to instrument.
      def self.instrument_instance_methods(mod)
        mod.public_instance_methods(false).each do |name|
84 85
          method = mod.instance_method(name)

86 87 88 89 90
          if method.owner == mod
            if !block_given? || block_given? && yield(mod, method)
              instrument_instance_method(mod, name)
            end
          end
91 92 93 94 95 96 97 98
        end
      end

      # Instruments a method.
      #
      # type - The type (:class or :instance) of method to instrument.
      # mod  - The module containing the method.
      # name - The name of the method to instrument.
99 100 101
      def self.instrument(type, mod, name)
        return unless Metrics.enabled?

102 103
        name       = name.to_sym
        alias_name = :"_original_#{name}"
104 105
        target     = type == :instance ? mod : mod.singleton_class

106 107 108 109 110 111 112 113
        if type == :instance
          target = mod
          label  = "#{mod.name}##{name}"
        else
          target = mod.singleton_class
          label  = "#{mod.name}.#{name}"
        end

114
        target.class_eval <<-EOF, __FILE__, __LINE__ + 1
115
          alias_method #{alias_name.inspect}, #{name.inspect}
116

117
          def #{name}(*args, &block)
118 119 120 121
            trans = Gitlab::Metrics::Instrumentation.transaction

            if trans
              start    = Time.now
122
              retval   = __send__(#{alias_name.inspect}, *args, &block)
123 124
              duration = (Time.now - start) * 1000.0

125 126 127 128 129
              if duration >= Gitlab::Metrics.method_call_threshold
                trans.add_metric(Gitlab::Metrics::Instrumentation::SERIES,
                                 { duration: duration },
                                 method: #{label.inspect})
              end
130 131 132

              retval
            else
133
              __send__(#{alias_name.inspect}, *args, &block)
134
            end
135
          end
136
        EOF
137
      end
138 139 140 141 142 143

      # Small layer of indirection to make it easier to stub out the current
      # transaction.
      def self.transaction
        Transaction.current
      end
144 145 146
    end
  end
end