Module: Gitlab::Metrics

Includes:
Prometheus
Defined in:
lib/gitlab/metrics.rb,
lib/gitlab/metrics/delta.rb,
lib/gitlab/metrics/system.rb,
lib/gitlab/metrics/methods.rb,
lib/gitlab/metrics/prometheus.rb,
lib/gitlab/metrics/method_call.rb,
lib/gitlab/metrics/null_metric.rb,
lib/gitlab/metrics/transaction.rb,
lib/gitlab/metrics/dashboard/url.rb,
lib/gitlab/metrics/dashboard/cache.rb,
lib/gitlab/metrics/instrumentation.rb,
lib/gitlab/metrics/rack_middleware.rb,
lib/gitlab/metrics/web_transaction.rb,
lib/gitlab/metrics/dashboard/errors.rb,
lib/gitlab/metrics/dashboard/finder.rb,
lib/gitlab/metrics/dashboard/defaults.rb,
lib/gitlab/metrics/dashboard/importer.rb,
lib/gitlab/metrics/sidekiq_middleware.rb,
lib/gitlab/metrics/dashboard/processor.rb,
lib/gitlab/metrics/dashboard/validator.rb,
lib/gitlab/metrics/exporter/web_exporter.rb,
lib/gitlab/metrics/samplers/base_sampler.rb,
lib/gitlab/metrics/samplers/puma_sampler.rb,
lib/gitlab/metrics/samplers/ruby_sampler.rb,
lib/gitlab/metrics/background_transaction.rb,
lib/gitlab/metrics/exporter/base_exporter.rb,
lib/gitlab/metrics/methods/metric_options.rb,
lib/gitlab/metrics/subscribers/action_view.rb,
lib/gitlab/metrics/subscribers/rails_cache.rb,
lib/gitlab/metrics/requests_rack_middleware.rb,
lib/gitlab/metrics/samplers/threads_sampler.rb,
lib/gitlab/metrics/samplers/unicorn_sampler.rb,
lib/gitlab/metrics/exporter/sidekiq_exporter.rb,
lib/gitlab/metrics/samplers/database_sampler.rb,
lib/gitlab/metrics/subscribers/active_record.rb,
lib/gitlab/metrics/dashboard/service_selector.rb,
lib/gitlab/metrics/dashboard/validator/client.rb,
lib/gitlab/metrics/dashboard/validator/errors.rb,
lib/gitlab/metrics/dashboard/stages/base_stage.rb,
lib/gitlab/metrics/dashboard/transformers/errors.rb,
lib/gitlab/metrics/elasticsearch_rack_middleware.rb,
lib/gitlab/metrics/samplers/action_cable_sampler.rb,
lib/gitlab/metrics/dashboard/stages/url_validator.rb,
lib/gitlab/metrics/dashboard/repo_dashboard_finder.rb,
lib/gitlab/metrics/dashboard/stages/alerts_inserter.rb,
lib/gitlab/metrics/dashboard/stages/track_panel_type.rb,
lib/gitlab/metrics/dashboard/stages/grafana_formatter.rb,
lib/gitlab/metrics/dashboard/validator/custom_formats.rb,
lib/gitlab/metrics/dashboard/stages/panel_ids_inserter.rb,
lib/gitlab/metrics/dashboard/importers/prometheus_metrics.rb,
lib/gitlab/metrics/dashboard/stages/common_metrics_inserter.rb,
lib/gitlab/metrics/dashboard/stages/custom_metrics_inserter.rb,
lib/gitlab/metrics/dashboard/stages/metric_endpoint_inserter.rb,
lib/gitlab/metrics/dashboard/validator/post_schema_validator.rb,
lib/gitlab/metrics/dashboard/stages/cluster_endpoint_inserter.rb,
lib/gitlab/metrics/dashboard/stages/variable_endpoint_inserter.rb,
lib/gitlab/metrics/dashboard/stages/custom_metrics_details_inserter.rb,
lib/gitlab/metrics/dashboard/transformers/yml/v1/prometheus_metrics.rb

Defined Under Namespace

Modules: Dashboard, Exporter, Instrumentation, Methods, Prometheus, Samplers, Subscribers, System Classes: BackgroundTransaction, Delta, ElasticsearchRackMiddleware, MethodCall, NullMetric, RackMiddleware, RequestsRackMiddleware, SidekiqMiddleware, Transaction, WebTransaction

Constant Summary collapse

EXECUTION_MEASUREMENT_BUCKETS =
[0.001, 0.01, 0.1, 1].freeze

Constants included from Prometheus

Prometheus::PROVIDER_MUTEX, Prometheus::REGISTRY_MUTEX

Class Method Summary collapse

Class Method Details

.add_event(*args) ⇒ Object

Tracks an event.

See `Gitlab::Metrics::Transaction#add_event` for more details.


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

def self.add_event(*args)
  current_transaction&.add_event(*args)
end

.current_transactionObject

Allow access from other metrics related middlewares


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

def self.current_transaction
  Transaction.current
end

.enabled?Boolean

Returns:

  • (Boolean)

11
12
13
# File 'lib/gitlab/metrics.rb', line 11

def self.enabled?
  prometheus_metrics_enabled?
end

.error?Boolean

Returns:

  • (Boolean)

15
16
17
# File 'lib/gitlab/metrics.rb', line 15

def self.error?
  @error
end

.measure(name) ⇒ Object

Measures the execution time of a block.

Example:

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

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

Returns the value yielded by the supplied block.


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gitlab/metrics.rb', line 67

def self.measure(name)
  trans = current_transaction

  return yield unless trans

  real_start = System.monotonic_time
  cpu_start = System.cpu_time

  retval = yield

  cpu_stop = System.cpu_time
  real_stop = System.monotonic_time

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

  trans.observe("gitlab_#{name}_real_duration_seconds".to_sym, real_time) do
    docstring "Measure #{name}"
    buckets EXECUTION_MEASUREMENT_BUCKETS
  end

  trans.observe("gitlab_#{name}_cpu_duration_seconds".to_sym, cpu_time) do
    docstring "Measure #{name}"
    buckets EXECUTION_MEASUREMENT_BUCKETS
    with_feature "prometheus_metrics_measure_#{name}_cpu_duration"
  end

  retval
end

.method_call_thresholdObject


48
49
50
51
52
53
54
# File 'lib/gitlab/metrics.rb', line 48

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

.series_prefixObject

Returns the prefix to use for the name of a series.


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

def self.series_prefix
  @series_prefix ||= Gitlab::Runtime.sidekiq? ? 'sidekiq_' : 'rails_'
end

.settingsObject


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/metrics.rb', line 36

def self.settings
  @settings ||= begin
    current_settings = Gitlab::CurrentSettings.current_application_settings

    {

      method_call_threshold: current_settings[:metrics_method_call_threshold]

    }
  end
end