Commit 4c04444e authored by Pawel Chojnacki's avatar Pawel Chojnacki

Finished Ruby Sampler

+ Cleanup sampler, use latest Prometheus gem
parent 0bfe7901
...@@ -21,5 +21,5 @@ Sidekiq.configure_server do |config| ...@@ -21,5 +21,5 @@ Sidekiq.configure_server do |config|
end end
# if Gitlab::Metrics.prometheus_metrics_enabled? # if Gitlab::Metrics.prometheus_metrics_enabled?
Gitlab::Metrics::Samplers::RubySampler.initialize_instance(1.second).start Gitlab::Metrics::Samplers::RubySampler.initialize_instance(60.second).start
# end # end
require 'prometheus/client/support/unicorn'
module Gitlab module Gitlab
module Metrics module Metrics
module Samplers module Samplers
class RubySampler < BaseSampler class RubySampler < BaseSampler
COUNTS = [:count, :minor_gc_count, :major_gc_count]
def metrics def metrics
@metrics ||= init_metrics @metrics ||= init_metrics
end end
...@@ -32,25 +32,30 @@ module Gitlab ...@@ -32,25 +32,30 @@ module Gitlab
def init_metrics def init_metrics
metrics = {} metrics = {}
metrics[:samples_total] = Gitlab::Metrics.counter(with_prefix(:sampler, :total), 'Total count of samples') metrics[:sampler_duration] = Gitlab::Metrics.histogram(with_prefix(:sampler_duration, :seconds), 'Sampler time', source_label)
metrics[:total_time] = Gitlab::Metrics.gauge(with_prefix(:gc, :time_total), 'Total GC time', labels, :livesum) metrics[:total_time] = Gitlab::Metrics.gauge(with_prefix(:gc, :time_total), 'Total GC time', labels, :livesum)
GC.stat.keys.each do |key| GC.stat.keys.each do |key|
metrics[key] = Gitlab::Metrics.gauge(with_prefix(:gc, key), to_doc_string(key), labels, :livesum) metrics[key] = Gitlab::Metrics.gauge(with_prefix(:gc, key), to_doc_string(key), labels, :livesum)
end end
metrics[:objects_total] = Gitlab::Metrics.gauge(with_prefix(:objects, :total), 'Objects total', labels.merge(class: nil), :livesum) metrics[:objects_total] = Gitlab::Metrics.gauge(with_prefix(:objects, :total), 'Objects total', labels.merge(class: nil), :livesum)
metrics[:memory_usage] = Gitlab::Metrics.gauge(with_prefix(:memory, :usage_total), 'Memory used total', labels, :livesum)
metrics[:file_descriptors] = Gitlab::Metrics.gauge(with_prefix(:file, :descriptors_total), 'File descriptors total', labels, :livesum)
metrics metrics
end end
def sample def sample
start_time = System.monotonic_time
metrics[:samples_total].increment(labels)
sample_gc sample_gc
sample_objects sample_objects
rescue => ex
puts ex
metrics[:memory_usage].set(labels, System.memory_usage)
metrics[:file_descriptors].set(labels, System.file_descriptor_count)
metrics[:sampler_duration].observe(source_label, (System.monotonic_time - start_time) / 1000.0)
ensure
GC::Profiler.clear
end end
private private
...@@ -64,13 +69,13 @@ module Gitlab ...@@ -64,13 +69,13 @@ module Gitlab
end end
def sample_objects def sample_objects
ss_objects.each do |name, count| list_objects.each do |name, count|
metrics[:objects_total].set(labels.merge(class: name), count) metrics[:objects_total].set(labels.merge(class: name), count)
end end
end end
if Metrics.mri? if Metrics.mri?
def ss_objects def list_objects
sample = Allocations.to_hash sample = Allocations.to_hash
counts = sample.each_with_object({}) do |(klass, count), hash| counts = sample.each_with_object({}) do |(klass, count), hash|
name = klass.name name = klass.name
...@@ -85,12 +90,10 @@ module Gitlab ...@@ -85,12 +90,10 @@ module Gitlab
counts counts
end end
else else
def ss_objects def list_objects
end end
end end
def source_label def source_label
if Sidekiq.server? if Sidekiq.server?
{ source: 'sidekiq' } { source: 'sidekiq' }
...@@ -101,11 +104,10 @@ module Gitlab ...@@ -101,11 +104,10 @@ module Gitlab
def worker_label def worker_label
return {} unless defined?(Unicorn::Worker) return {} unless defined?(Unicorn::Worker)
worker = if defined?(Unicorn::Worker) worker_no = ::Prometheus::Client::Support::Unicorn.worker_id
ObjectSpace.each_object(Unicorn::Worker)&.first
end if worker_no
if worker { unicorn: worker_no }
{ unicorn: worker.nr }
else else
{ unicorn: 'master' } { unicorn: 'master' }
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment