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
|
|
|
|
metrics = {}
|
2017-12-07 11:47:23 -05:00
|
|
|
metrics[:sampler_duration] = Metrics.histogram(with_prefix(:sampler_duration, :seconds), 'Sampler time', { worker: nil })
|
2017-10-17 10:23:33 -04:00
|
|
|
metrics[:total_time] = Metrics.gauge(with_prefix(:gc, :time_total), 'Total GC time', labels, :livesum)
|
2017-08-10 10:18:37 -04:00
|
|
|
GC.stat.keys.each do |key|
|
2017-10-17 10:23:33 -04:00
|
|
|
metrics[key] = Metrics.gauge(with_prefix(:gc, 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-10-17 10:23:33 -04:00
|
|
|
metrics[:objects_total] = Metrics.gauge(with_prefix(:objects, :total), 'Objects total', labels.merge(class: nil), :livesum)
|
|
|
|
metrics[:memory_usage] = Metrics.gauge(with_prefix(:memory, :usage_total), 'Memory used total', labels, :livesum)
|
|
|
|
metrics[:file_descriptors] = Metrics.gauge(with_prefix(:file, :descriptors_total), 'File descriptors total', labels, :livesum)
|
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
|
|
|
sample_gc
|
|
|
|
|
2017-08-16 07:44:21 -04:00
|
|
|
metrics[:memory_usage].set(labels, System.memory_usage)
|
|
|
|
metrics[:file_descriptors].set(labels, System.file_descriptor_count)
|
|
|
|
|
2017-12-12 12:12:49 -05:00
|
|
|
metrics[:sampler_duration].observe(labels.merge(worker_label), 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
|
2017-08-10 10:18:37 -04:00
|
|
|
metrics[:total_time].set(labels, GC::Profiler.total_time * 1000)
|
|
|
|
|
|
|
|
GC.stat.each do |key, value|
|
|
|
|
metrics[key].set(labels, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|