BigW Consortium Gitlab

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

  # These are manually require'd so the classes are registered properly with
  # ActiveSupport.
  require 'gitlab/metrics/subscribers/action_view'
9
  require 'gitlab/metrics/subscribers/active_record'
10 11 12 13 14 15 16 17 18 19 20

  Gitlab::Application.configure do |config|
    config.middleware.use(Gitlab::Metrics::RackMiddleware)
  end

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

21 22 23 24 25 26 27 28 29 30 31 32 33
  # 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.
34 35 36 37 38 39 40 41
      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
42

43 44
          loc && loc[0].start_with?(models) && method.source =~ regex
        end
45 46 47
      end
  end

48 49 50 51 52 53 54 55 56 57
  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)

      config.instrument_methods(const) if const.is_a?(Module)
    end
58 59 60 61 62 63

    Dir[Rails.root.join('app', 'finders', '*.rb')].each do |path|
      const = File.basename(path, '.rb').camelize.constantize

      config.instrument_instance_methods(const)
    end
64 65
  end

66 67 68 69
  GC::Profiler.enable

  Gitlab::Metrics::Sampler.new.start
end