instrumentation.rb 2.9 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'

Y
Yorick Peterse 已提交
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
      # Instruments all public methods of a module.
      #
      # mod - The module to instrument.
      def self.instrument_methods(mod)
        mod.public_methods(false).each do |name|
          instrument_method(mod, name)
        end
      end

      # Instruments all public instance methods of a module.
      #
      # mod - The module to instrument.
      def self.instrument_instance_methods(mod)
        mod.public_instance_methods(false).each do |name|
          instrument_instance_method(mod, name)
        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.
57 58 59
      def self.instrument(type, mod, name)
        return unless Metrics.enabled?

60 61
        name       = name.to_sym
        alias_name = :"_original_#{name}"
62 63
        target     = type == :instance ? mod : mod.singleton_class

64 65 66 67 68 69 70 71
        if type == :instance
          target = mod
          label  = "#{mod.name}##{name}"
        else
          target = mod.singleton_class
          label  = "#{mod.name}.#{name}"
        end

72
        target.class_eval <<-EOF, __FILE__, __LINE__ + 1
73
          alias_method #{alias_name.inspect}, #{name.inspect}
74

75
          def #{name}(*args, &block)
76 77 78 79
            trans = Gitlab::Metrics::Instrumentation.transaction

            if trans
              start    = Time.now
80
              retval   =
81 82 83 84 85 86 87 88
              duration = (Time.now - start) * 1000.0

              trans.add_metric(Gitlab::Metrics::Instrumentation::SERIES,
                               { duration: duration },
                               method: #{label.inspect})

              retval
            else
89
              __send__(#{alias_name.inspect}, *args, &block)
90
            end
91
          end
92
        EOF
93
      end
94 95 96 97 98 99

      # Small layer of indirection to make it easier to stub out the current
      # transaction.
      def self.transaction
        Transaction.current
      end
100 101 102
    end
  end
end