metrics.rb 1.5 KB
Newer Older
1 2
module Gitlab
  module Metrics
3 4 5 6
    RAILS_ROOT   = Rails.root.to_s
    METRICS_ROOT = Rails.root.join('lib', 'gitlab', 'metrics').to_s
    PATH_REGEX   = /^#{RAILS_ROOT}\/?/

7 8 9 10 11 12 13 14 15 16 17 18
    def self.pool_size
      Settings.metrics['pool_size'] || 16
    end

    def self.timeout
      Settings.metrics['timeout'] || 10
    end

    def self.enabled?
      !!Settings.metrics['enabled']
    end

19 20 21 22
    def self.mri?
      RUBY_ENGINE == 'ruby'
    end

23 24 25 26
    def self.method_call_threshold
      Settings.metrics['method_call_threshold'] || 10
    end

27 28 29 30 31 32 33 34
    def self.pool
      @pool
    end

    def self.hostname
      @hostname
    end

35 36
    # Returns a relative path and line number based on the last application call
    # frame.
37 38
    def self.last_relative_application_frame
      frame = caller_locations.find do |l|
39
        l.path.start_with?(RAILS_ROOT) && !l.path.start_with?(METRICS_ROOT)
40 41 42
      end

      if frame
43
        return frame.path.sub(PATH_REGEX, ''), frame.lineno
44 45 46 47 48 49 50 51 52 53 54
      else
        return nil, nil
      end
    end

    @hostname = Socket.gethostname

    # When enabled this should be set before being used as the usual pattern
    # "@foo ||= bar" is _not_ thread-safe.
    if enabled?
      @pool = ConnectionPool.new(size: pool_size, timeout: timeout) do
55
        host = Settings.metrics['host']
56 57 58 59
        db   = Settings.metrics['database']
        user = Settings.metrics['username']
        pw   = Settings.metrics['password']

60
        InfluxDB::Client.new(db, host: host, username: user, password: pw)
61 62 63 64
      end
    end
  end
end