Module: Gitlab::Metrics

Includes:
Prometheus
Defined in:
lib/gitlab/metrics.rb,
lib/gitlab/metrics/sli.rb,
lib/gitlab/metrics/delta.rb,
lib/gitlab/metrics/system.rb,
lib/gitlab/metrics/methods.rb,
lib/gitlab/metrics/prometheus.rb,
lib/gitlab/metrics/rails_slis.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/rack_middleware.rb,
lib/gitlab/metrics/web_transaction.rb,
lib/gitlab/metrics/dashboard/errors.rb,
lib/gitlab/metrics/dashboard/finder.rb,
lib/gitlab/metrics/boot_time_tracker.rb,
lib/gitlab/metrics/dashboard/defaults.rb,
lib/gitlab/metrics/dashboard/importer.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/rack_attack.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/subscribers/action_cable.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/subscribers/external_http.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/subscribers/load_balancing.rb,
lib/gitlab/metrics/dashboard/stages/base_stage.rb,
lib/gitlab/metrics/exporter/metrics_middleware.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/exporter/gc_request_middleware.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,
lib/gitlab/metrics/dashboard/stages/custom_dashboard_metrics_inserter.rb

Defined Under Namespace

Modules: Dashboard, Exporter, Methods, Prometheus, RailsSlis, Samplers, Subscribers, System Classes: BackgroundTransaction, BootTimeTracker, Delta, ElasticsearchRackMiddleware, MethodCall, NullMetric, RackMiddleware, RequestsRackMiddleware, Sli, 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.


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

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

.current_transactionObject

Allow access from other metrics related middlewares


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

def self.current_transaction
  WebTransaction.current || BackgroundTransaction.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.


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
96
97
98
99
# File 'lib/gitlab/metrics.rb', line 71

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


52
53
54
55
56
57
58
# File 'lib/gitlab/metrics.rb', line 52

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

.record_duration_for_status?(status) ⇒ Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/gitlab/metrics.rb', line 19

def self.record_duration_for_status?(status)
  status.to_i.between?(200, 499)
end

.series_prefixObject

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


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

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

.settingsObject


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

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

    {

      method_call_threshold: current_settings[:metrics_method_call_threshold]

    }
  end
end