Module: Gitlab::Metrics

Extended by:
CurrentSettings
Defined in:
lib/gitlab/metrics.rb,
lib/gitlab/metrics/delta.rb,
lib/gitlab/metrics/metric.rb,
lib/gitlab/metrics/system.rb,
lib/gitlab/metrics/sampler.rb,
lib/gitlab/metrics/transaction.rb,
lib/gitlab/metrics/rack_middleware.rb,
lib/gitlab/metrics/instrumentation.rb,
lib/gitlab/metrics/sidekiq_middleware.rb,
lib/gitlab/metrics/subscribers/action_view.rb,
lib/gitlab/metrics/subscribers/rails_cache.rb,
lib/gitlab/metrics/subscribers/active_record.rb

Defined Under Namespace

Modules: Instrumentation, Subscribers, System Classes: Delta, Metric, RackMiddleware, Sampler, SidekiqMiddleware, Transaction

Constant Summary collapse

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

Class Method Summary collapse

Methods included from CurrentSettings

current_application_settings, fake_application_settings

Class Method Details

.action=(action) ⇒ Object

Sets the action of the current transaction (if any)

action - The name of the action.


121
122
123
124
125
# File 'lib/gitlab/metrics.rb', line 121

def self.action=(action)
  trans = current_transaction

  trans.action = action if trans
end

.enabled?Boolean


22
23
24
# File 'lib/gitlab/metrics.rb', line 22

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

.escape_value(value) ⇒ Object


70
71
72
# File 'lib/gitlab/metrics.rb', line 70

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

.measure(name) ⇒ Object

Measures the execution time of a block.

Example:

Gitlab::Metrics.measure(:find_by_username_duration) do
  User.find_by_username(some_username)
end

name - The name of the field to store the execution time in.

Returns the value yielded by the supplied block.


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gitlab/metrics.rb', line 85

def self.measure(name)
  trans = current_transaction

  return yield unless trans

  real_start = Time.now.to_f
  cpu_start = System.cpu_time

  retval = yield

  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

  trans.increment("#{name}_real_time", real_time)
  trans.increment("#{name}_cpu_time", cpu_time)
  trans.increment("#{name}_call_count", 1)

  retval
end

.method_call_thresholdObject


30
31
32
33
34
35
# File 'lib/gitlab/metrics.rb', line 30

def self.method_call_threshold
  # 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.
  @method_call_threshold ||= settings[:method_call_threshold]
end

.mri?Boolean


26
27
28
# File 'lib/gitlab/metrics.rb', line 26

def self.mri?
  RUBY_ENGINE == 'ruby'
end

.poolObject


37
38
39
# File 'lib/gitlab/metrics.rb', line 37

def self.pool
  @pool
end

.prepare_metrics(metrics) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gitlab/metrics.rb', line 54

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

.settingsObject


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gitlab/metrics.rb', line 9

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],
    port:                  current_application_settings[:metrics_port],
    sample_interval:       current_application_settings[:metrics_sample_interval] || 15,
    packet_size:           current_application_settings[:metrics_packet_size] || 1
  }
end

.submit_metrics(metrics) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gitlab/metrics.rb', line 41

def self.submit_metrics(metrics)
  prepared = prepare_metrics(metrics)

  pool.with do |connection|
    prepared.each_slice(settings[:packet_size]) do |slice|
      begin
        connection.write_points(slice)
      rescue StandardError
      end
    end
  end
end

.tag_transaction(name, value) ⇒ Object

Adds a tag to the current transaction (if any)

name - The name of the tag to add. value - The value of the tag.


112
113
114
115
116
# File 'lib/gitlab/metrics.rb', line 112

def self.tag_transaction(name, value)
  trans = current_transaction

  trans.add_tag(name, value) if trans
end