metrics.rb 5.06 KB
Newer Older
1
if Gitlab::Metrics.enabled?
2
  require 'pathname'
3 4
  require 'influxdb'
  require 'connection_pool'
5
  require 'method_source'
6 7 8 9

  # These are manually require'd so the classes are registered properly with
  # ActiveSupport.
  require 'gitlab/metrics/subscribers/action_view'
10
  require 'gitlab/metrics/subscribers/active_record'
11
  require 'gitlab/metrics/subscribers/rails_cache'
12 13 14

  Gitlab::Application.configure do |config|
    config.middleware.use(Gitlab::Metrics::RackMiddleware)
15
    config.middleware.use(Gitlab::Middleware::RailsQueueDuration)
16 17 18 19 20 21 22 23
  end

  Sidekiq.configure_server do |config|
    config.server_middleware do |chain|
      chain.add Gitlab::Metrics::SidekiqMiddleware
    end
  end

24 25 26 27 28 29 30 31 32 33 34 35 36
  # This instruments all methods residing in app/models that (appear to) use any
  # of the ActiveRecord methods. This has to take place _after_ initializing as
  # for some unknown reason calling eager_load! earlier breaks Devise.
  Gitlab::Application.config.after_initialize do
    Rails.application.eager_load!

    models = Rails.root.join('app', 'models').to_s

    regex = Regexp.union(
      ActiveRecord::Querying.public_instance_methods(false).map(&:to_s)
    )

    Gitlab::Metrics::Instrumentation.
37 38 39 40 41 42 43 44
      instrument_class_hierarchy(ActiveRecord::Base) do |klass, method|
        # Instrumenting the ApplicationSetting class can lead to an infinite
        # loop. Since the data is cached any way we don't really need to
        # instrument it.
        if klass == ApplicationSetting
          false
        else
          loc = method.source_location
45

46 47
          loc && loc[0].start_with?(models) && method.source =~ regex
        end
48 49 50
      end
  end

51 52 53 54 55 56 57 58
  Gitlab::Metrics::Instrumentation.configure do |config|
    config.instrument_instance_methods(Gitlab::Shell)

    config.instrument_methods(Gitlab::Git)

    Gitlab::Git.constants.each do |name|
      const = Gitlab::Git.const_get(name)

59 60 61 62
      next unless const.is_a?(Module)

      config.instrument_methods(const)
      config.instrument_instance_methods(const)
63
    end
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    # Path to search => prefix to strip from constant
    paths_to_instrument = {
      ['app', 'finders']                    => ['app', 'finders'],
      ['app', 'mailers', 'emails']          => ['app', 'mailers'],
      ['app', 'services', '**']             => ['app', 'services'],
      ['lib', 'gitlab', 'diff']             => ['lib'],
      ['lib', 'gitlab', 'email', 'message'] => ['lib']
    }

    paths_to_instrument.each do |(path, prefix)|
      prefix = Rails.root.join(*prefix)

      Dir[Rails.root.join(*path + ['*.rb'])].each do |file_path|
        path = Pathname.new(file_path).relative_path_from(prefix)
        const = path.to_s.sub('.rb', '').camelize.constantize

        config.instrument_methods(const)
        config.instrument_instance_methods(const)
      end
84
    end
85

86 87 88
    config.instrument_methods(Premailer::Adapter::Nokogiri)
    config.instrument_instance_methods(Premailer::Adapter::Nokogiri)

89 90 91 92 93 94 95 96 97
    [
      :Blame, :Branch, :BranchCollection, :Blob, :Commit, :Diff, :Repository,
      :Tag, :TagCollection, :Tree
    ].each do |name|
      const = Rugged.const_get(name)

      config.instrument_methods(const)
      config.instrument_instance_methods(const)
    end
Yorick Peterse's avatar
Yorick Peterse committed
98

99 100 101 102 103 104 105 106
    # Instruments all Banzai filters and reference parsers
    {
      Filter: Rails.root.join('lib', 'banzai', 'filter', '*.rb'),
      ReferenceParser: Rails.root.join('lib', 'banzai', 'reference_parser', '*.rb')
    }.each do |const_name, path|
      Dir[path].each do |file|
        klass = File.basename(file, File.extname(file)).camelize
        const = Banzai.const_get(const_name).const_get(klass)
Yorick Peterse's avatar
Yorick Peterse committed
107

108 109 110
        config.instrument_methods(const)
        config.instrument_instance_methods(const)
      end
Yorick Peterse's avatar
Yorick Peterse committed
111 112 113 114 115
    end

    config.instrument_methods(Banzai::Renderer)
    config.instrument_methods(Banzai::Querying)

116 117 118 119
    config.instrument_instance_methods(Banzai::ObjectRenderer)
    config.instrument_instance_methods(Banzai::Redactor)
    config.instrument_methods(Banzai::NoteRenderer)

Yorick Peterse's avatar
Yorick Peterse committed
120 121 122 123 124 125 126
    [Issuable, Mentionable, Participable].each do |klass|
      config.instrument_instance_methods(klass)
      config.instrument_instance_methods(klass::ClassMethods)
    end

    config.instrument_methods(Gitlab::ReferenceExtractor)
    config.instrument_instance_methods(Gitlab::ReferenceExtractor)
127

128 129 130
    # Instrument the classes used for checking if somebody has push access.
    config.instrument_instance_methods(Gitlab::GitAccess)
    config.instrument_instance_methods(Gitlab::GitAccessWiki)
131 132

    config.instrument_instance_methods(API::Helpers)
133 134

    config.instrument_instance_methods(RepositoryCheck::SingleRepositoryWorker)
135 136 137

    config.instrument_instance_methods(Rouge::Plugins::Redcarpet)
    config.instrument_instance_methods(Rouge::Formatters::HTMLGitlab)
138 139
  end

140 141 142
  GC::Profiler.enable

  Gitlab::Metrics::Sampler.new.start
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

  module TrackNewRedisConnections
    def connect(*args)
      val = super

      if current_transaction = Gitlab::Metrics::Transaction.current
        current_transaction.increment(:new_redis_connections, 1)
      end

      val
    end
  end

  class ::Redis::Client
    prepend TrackNewRedisConnections
  end
159
end