2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Samplers
|
|
|
|
class RubySampler < BaseSampler
|
2021-03-08 18:12:59 +05:30
|
|
|
DEFAULT_SAMPLING_INTERVAL_SECONDS = 60
|
2021-04-17 20:07:23 +05:30
|
|
|
GC_REPORT_BUCKETS = [0.01, 0.05, 0.1, 0.2, 0.3, 0.5, 1].freeze
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def initialize(prefix: nil, **options)
|
|
|
|
@prefix = prefix
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
GC::Profiler.clear
|
|
|
|
|
|
|
|
metrics[:process_start_time_seconds].set(labels, Time.now.to_i)
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
super(**options)
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def metrics
|
|
|
|
@metrics ||= init_metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_doc_string(name)
|
|
|
|
name.to_s.humanize
|
|
|
|
end
|
|
|
|
|
|
|
|
def labels
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def init_metrics
|
2019-09-04 21:01:54 +05:30
|
|
|
metrics = {
|
2022-10-11 01:57:18 +05:30
|
|
|
file_descriptors: ::Gitlab::Metrics.gauge(metric_name(:file, :descriptors), 'File descriptors used', labels),
|
|
|
|
process_cpu_seconds_total: ::Gitlab::Metrics.gauge(metric_name(:process, :cpu_seconds_total), 'Process CPU seconds total'),
|
|
|
|
process_max_fds: ::Gitlab::Metrics.gauge(metric_name(:process, :max_fds), 'Process max fds'),
|
|
|
|
process_resident_memory_bytes: ::Gitlab::Metrics.gauge(metric_name(:process, :resident_memory_bytes), 'Memory used (RSS)', labels),
|
2023-01-13 00:05:48 +05:30
|
|
|
process_resident_anon_memory_bytes: ::Gitlab::Metrics.gauge(metric_name(:process, :resident_anon_memory_bytes), 'Anonymous memory used (RSS)', labels),
|
|
|
|
process_resident_file_memory_bytes: ::Gitlab::Metrics.gauge(metric_name(:process, :resident_file_memory_bytes), 'File backed memory used (RSS)', labels),
|
2022-10-11 01:57:18 +05:30
|
|
|
process_unique_memory_bytes: ::Gitlab::Metrics.gauge(metric_name(:process, :unique_memory_bytes), 'Memory used (USS)', labels),
|
2022-04-04 11:22:00 +05:30
|
|
|
process_proportional_memory_bytes: ::Gitlab::Metrics.gauge(metric_name(:process, :proportional_memory_bytes), 'Memory used (PSS)', labels),
|
2022-10-11 01:57:18 +05:30
|
|
|
process_start_time_seconds: ::Gitlab::Metrics.gauge(metric_name(:process, :start_time_seconds), 'Process start time seconds'),
|
|
|
|
sampler_duration: ::Gitlab::Metrics.counter(metric_name(:sampler, :duration_seconds_total), 'Sampler time', labels),
|
|
|
|
gc_duration_seconds: ::Gitlab::Metrics.histogram(metric_name(:gc, :duration_seconds), 'GC time', labels, GC_REPORT_BUCKETS),
|
|
|
|
heap_fragmentation: ::Gitlab::Metrics.gauge(metric_name(:gc_stat_ext, :heap_fragmentation), 'Ruby heap fragmentation', labels)
|
2019-09-04 21:01:54 +05:30
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
GC.stat.keys.each do |key|
|
2022-04-04 11:22:00 +05:30
|
|
|
metrics[key] = ::Gitlab::Metrics.gauge(metric_name(:gc_stat, key), to_doc_string(key), labels)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
def sample
|
|
|
|
start_time = System.monotonic_time
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
metrics[:file_descriptors].set(labels, System.file_descriptor_count)
|
|
|
|
metrics[:process_cpu_seconds_total].set(labels, ::Gitlab::Metrics::System.cpu_time)
|
|
|
|
metrics[:process_max_fds].set(labels, ::Gitlab::Metrics::System.max_open_file_descriptors)
|
2019-09-04 21:01:54 +05:30
|
|
|
set_memory_usage_metrics
|
2018-11-08 19:23:39 +05:30
|
|
|
sample_gc
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
metrics[:sampler_duration].increment(labels, System.monotonic_time - start_time)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def metric_name(group, metric)
|
|
|
|
name = "ruby_#{group}_#{metric}"
|
|
|
|
name = "#{@prefix}_#{name}" if @prefix.present?
|
|
|
|
name.to_sym
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def sample_gc
|
2019-10-12 21:52:04 +05:30
|
|
|
# Observe all GC samples
|
|
|
|
sample_gc_reports.each do |report|
|
|
|
|
metrics[:gc_duration_seconds].observe(labels, report[:GC_TIME])
|
|
|
|
end
|
|
|
|
|
|
|
|
# Collect generic GC stats
|
2022-08-13 15:12:31 +05:30
|
|
|
GC.stat.then do |gc_stat|
|
|
|
|
gc_stat.each do |key, value|
|
|
|
|
metrics[key].set(labels, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Collect custom GC stats
|
|
|
|
metrics[:heap_fragmentation].set(labels, Memory.gc_heap_fragmentation(gc_stat))
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def sample_gc_reports
|
|
|
|
GC::Profiler.enable
|
|
|
|
GC::Profiler.raw_data
|
|
|
|
ensure
|
|
|
|
GC::Profiler.clear
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def set_memory_usage_metrics
|
2023-01-13 00:05:48 +05:30
|
|
|
rss = System.memory_usage_rss
|
|
|
|
metrics[:process_resident_memory_bytes].set(labels, rss[:total])
|
|
|
|
metrics[:process_resident_anon_memory_bytes].set(labels, rss[:anon])
|
|
|
|
metrics[:process_resident_file_memory_bytes].set(labels, rss[:file])
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
if Gitlab::Utils.to_boolean(ENV['enable_memory_uss_pss'] || '1')
|
|
|
|
memory_uss_pss = System.memory_usage_uss_pss
|
|
|
|
metrics[:process_unique_memory_bytes].set(labels, memory_uss_pss[:uss])
|
|
|
|
metrics[:process_proportional_memory_bytes].set(labels, memory_uss_pss[:pss])
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|