BigW Consortium Gitlab

metrics.rb 4.42 KB
Newer Older
1 2
module Gitlab
  module Metrics
3 4
    extend Gitlab::CurrentSettings

5 6 7 8
    RAILS_ROOT   = Rails.root.to_s
    METRICS_ROOT = Rails.root.join('lib', 'gitlab', 'metrics').to_s
    PATH_REGEX   = /^#{RAILS_ROOT}\/?/

9 10 11 12 13 14 15
    def self.settings
      @settings ||= {
        enabled:               current_application_settings[:metrics_enabled],
        pool_size:             current_application_settings[:metrics_pool_size],
        timeout:               current_application_settings[:metrics_timeout],
        method_call_threshold: current_application_settings[:metrics_method_call_threshold],
        host:                  current_application_settings[:metrics_host],
16
        port:                  current_application_settings[:metrics_port],
17 18
        sample_interval:       current_application_settings[:metrics_sample_interval] || 15,
        packet_size:           current_application_settings[:metrics_packet_size] || 1
19
      }
20 21 22
    end

    def self.enabled?
23
      settings[:enabled] || false
24 25
    end

26 27 28 29
    def self.mri?
      RUBY_ENGINE == 'ruby'
    end

30
    def self.method_call_threshold
31 32 33
      # This is memoized since this method is called for every instrumented
      # method. Loading data from an external cache on every method call slows
      # things down too much.
34
      @method_call_threshold ||= settings[:method_call_threshold]
35 36
    end

37 38 39 40
    def self.pool
      @pool
    end

41 42 43 44
    def self.submit_metrics(metrics)
      prepared = prepare_metrics(metrics)

      pool.with do |connection|
45
        prepared.each_slice(settings[:packet_size]) do |slice|
46
          begin
47
            connection.write_points(slice)
48 49 50 51
          rescue StandardError
          end
        end
      end
52 53 54
    rescue Errno::EADDRNOTAVAIL, SocketError => ex
      Gitlab::EnvironmentLogger.error('Cannot resolve InfluxDB address. GitLab Performance Monitoring will not work.')
      Gitlab::EnvironmentLogger.error(ex)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    end

    def self.prepare_metrics(metrics)
      metrics.map do |hash|
        new_hash = hash.symbolize_keys

        new_hash[:tags].each do |key, value|
          if value.blank?
            new_hash[:tags].delete(key)
          else
            new_hash[:tags][key] = escape_value(value)
          end
        end

        new_hash
      end
    end

    def self.escape_value(value)
      value.to_s.gsub('=', '\\=')
    end

77 78 79 80
    # Measures the execution time of a block.
    #
    # Example:
    #
81
    #     Gitlab::Metrics.measure(:find_by_username_duration) do
82 83 84
    #       User.find_by_username(some_username)
    #     end
    #
85
    # name - The name of the field to store the execution time in.
86 87
    #
    # Returns the value yielded by the supplied block.
88
    def self.measure(name)
89 90 91
      trans = current_transaction

      return yield unless trans
92

93 94 95
      real_start = Time.now.to_f
      cpu_start = System.cpu_time

96 97
      retval = yield

98 99 100 101 102 103
      cpu_stop = System.cpu_time
      real_stop = Time.now.to_f

      real_time = (real_stop - real_start) * 1000.0
      cpu_time = cpu_stop - cpu_start

104 105 106
      trans.increment("#{name}_real_time", real_time)
      trans.increment("#{name}_cpu_time", cpu_time)
      trans.increment("#{name}_call_count", 1)
107 108 109 110

      retval
    end

111 112 113 114 115 116 117
    # Adds a tag to the current transaction (if any)
    #
    # name - The name of the tag to add.
    # value - The value of the tag.
    def self.tag_transaction(name, value)
      trans = current_transaction

118
      trans&.add_tag(name, value)
119 120
    end

121 122 123 124 125 126
    # Sets the action of the current transaction (if any)
    #
    # action - The name of the action.
    def self.action=(action)
      trans = current_transaction

127
      trans&.action = action
128 129
    end

130 131 132 133 134 135
    # Tracks an event.
    #
    # See `Gitlab::Metrics::Transaction#add_event` for more details.
    def self.add_event(*args)
      trans = current_transaction

136
      trans&.add_event(*args)
137 138
    end

139 140 141 142 143
    # Returns the prefix to use for the name of a series.
    def self.series_prefix
      @series_prefix ||= Sidekiq.server? ? 'sidekiq_' : 'rails_'
    end

144 145 146 147 148
    # Allow access from other metrics related middlewares
    def self.current_transaction
      Transaction.current
    end

149 150 151
    # When enabled this should be set before being used as the usual pattern
    # "@foo ||= bar" is _not_ thread-safe.
    if enabled?
152 153 154
      @pool = ConnectionPool.new(size: settings[:pool_size], timeout: settings[:timeout]) do
        host = settings[:host]
        port = settings[:port]
155

156
        InfluxDB::Client.
157
          new(udp: { host: host, port: port })
158 159 160 161
      end
    end
  end
end