2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-16 07:44:21 -04:00
|
|
|
require 'prometheus/client/support/unicorn'
|
|
|
|
|
2017-08-10 10:18:37 -04:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Samplers
|
|
|
|
class RubySampler < BaseSampler
|
|
|
|
def metrics
|
|
|
|
@metrics ||= init_metrics
|
|
|
|
end
|
|
|
|
|
2017-08-11 16:00:06 -04:00
|
|
|
def with_prefix(prefix, name)
|
|
|
|
"ruby_#{prefix}_#{name}".to_sym
|
2017-08-10 10:18:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_doc_string(name)
|
|
|
|
name.to_s.humanize
|
|
|
|
end
|
|
|
|
|
|
|
|
def labels
|
2017-08-22 10:13:51 -04:00
|
|
|
{}
|
2017-08-10 10:18:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def init_metrics
|
2019-04-24 13:41:54 -04:00
|
|
|
metrics = {
|
2019-04-24 19:05:09 -04:00
|
|
|
file_descriptors: ::Gitlab::Metrics.gauge(with_prefix(:file, :descriptors), 'File descriptors used', labels, :livesum),
|
2019-04-29 14:13:02 -04:00
|
|
|
memory_bytes: ::Gitlab::Metrics.gauge(with_prefix(:memory, :bytes), 'Memory used', labels, :livesum),
|
2019-04-24 19:05:09 -04:00
|
|
|
process_cpu_seconds_total: ::Gitlab::Metrics.gauge(with_prefix(:process, :cpu_seconds_total), 'Process CPU seconds total'),
|
|
|
|
process_max_fds: ::Gitlab::Metrics.gauge(with_prefix(:process, :max_fds), 'Process max fds'),
|
|
|
|
process_resident_memory_bytes: ::Gitlab::Metrics.gauge(with_prefix(:process, :resident_memory_bytes), 'Memory used', labels, :livesum),
|
|
|
|
process_start_time_seconds: ::Gitlab::Metrics.gauge(with_prefix(:process, :start_time_seconds), 'Process start time seconds'),
|
|
|
|
sampler_duration: ::Gitlab::Metrics.counter(with_prefix(:sampler, :duration_seconds_total), 'Sampler time', labels),
|
|
|
|
total_time: ::Gitlab::Metrics.counter(with_prefix(:gc, :duration_seconds_total), 'Total GC time', labels)
|
2019-04-24 13:41:54 -04:00
|
|
|
}
|
|
|
|
|
2017-08-10 10:18:37 -04:00
|
|
|
GC.stat.keys.each do |key|
|
2019-02-14 13:05:35 -05:00
|
|
|
metrics[key] = ::Gitlab::Metrics.gauge(with_prefix(:gc_stat, key), to_doc_string(key), labels, :livesum)
|
2017-08-10 10:18:37 -04:00
|
|
|
end
|
2017-08-11 16:00:06 -04:00
|
|
|
|
2017-08-10 10:18:37 -04:00
|
|
|
metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
def sample
|
2017-08-16 07:44:21 -04:00
|
|
|
start_time = System.monotonic_time
|
2017-08-11 16:00:06 -04:00
|
|
|
|
2018-06-20 10:42:38 -04:00
|
|
|
metrics[:file_descriptors].set(labels.merge(worker_label), System.file_descriptor_count)
|
2019-04-24 13:41:54 -04:00
|
|
|
metrics[:process_cpu_seconds_total].set(labels.merge(worker_label), ::Gitlab::Metrics::System.cpu_time)
|
|
|
|
metrics[:process_max_fds].set(labels.merge(worker_label), ::Gitlab::Metrics::System.max_open_file_descriptors)
|
2019-04-24 19:05:09 -04:00
|
|
|
metrics[:process_start_time_seconds].set(labels.merge(worker_label), ::Gitlab::Metrics::System.process_start_time)
|
2019-04-29 14:13:02 -04:00
|
|
|
set_memory_usage_metrics
|
2018-06-20 10:42:38 -04:00
|
|
|
sample_gc
|
2017-08-16 07:44:21 -04:00
|
|
|
|
2018-06-20 10:42:38 -04:00
|
|
|
metrics[:sampler_duration].increment(labels, System.monotonic_time - start_time)
|
2017-08-16 07:44:21 -04:00
|
|
|
ensure
|
|
|
|
GC::Profiler.clear
|
2017-08-11 16:00:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def sample_gc
|
2018-06-20 10:42:38 -04:00
|
|
|
# Collect generic GC stats.
|
2017-08-10 10:18:37 -04:00
|
|
|
GC.stat.each do |key, value|
|
|
|
|
metrics[key].set(labels, value)
|
|
|
|
end
|
2018-06-20 10:42:38 -04:00
|
|
|
|
|
|
|
# Collect the GC time since last sample in float seconds.
|
|
|
|
metrics[:total_time].increment(labels, GC::Profiler.total_time)
|
2017-08-10 10:18:37 -04:00
|
|
|
end
|
|
|
|
|
2019-04-29 14:13:02 -04:00
|
|
|
def set_memory_usage_metrics
|
|
|
|
memory_usage = System.memory_usage
|
|
|
|
memory_labels = labels.merge(worker_label)
|
|
|
|
|
|
|
|
metrics[:memory_bytes].set(memory_labels, memory_usage)
|
|
|
|
metrics[:process_resident_memory_bytes].set(memory_labels, memory_usage)
|
|
|
|
end
|
|
|
|
|
2017-08-10 10:18:37 -04:00
|
|
|
def worker_label
|
|
|
|
return {} unless defined?(Unicorn::Worker)
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2017-08-16 07:44:21 -04:00
|
|
|
worker_no = ::Prometheus::Client::Support::Unicorn.worker_id
|
|
|
|
|
|
|
|
if worker_no
|
2017-12-07 11:47:23 -05:00
|
|
|
{ worker: worker_no }
|
2017-08-10 10:18:37 -04:00
|
|
|
else
|
2017-12-07 11:47:23 -05:00
|
|
|
{ worker: 'master' }
|
2017-08-10 10:18:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-16 07:44:21 -04:00
|
|
|
end
|