Add back ruby_memory_bytes metric, limit duplication, clean up

This adds back ruby_memory_bytes for backwards compatibility, limits
code duplication, cleans up unused methods, and limits the unicorn
worker sampling scope.
This commit is contained in:
Ryan Cobb 2019-04-29 12:13:02 -06:00
parent 2f6a1e77bc
commit 17986d91a5
3 changed files with 21 additions and 11 deletions

View file

@ -58,10 +58,11 @@ Some basic Ruby runtime metrics are available:
| ruby_gc_duration_seconds_total | Counter | 11.1 | Time spent by Ruby in GC |
| ruby_gc_stat_... | Gauge | 11.1 | Various metrics from [GC.stat] |
| ruby_file_descriptors | Gauge | 11.1 | File descriptors per process |
| ruby_process_resident_memory_bytes | Gauge | 11.1 | Memory usage by process |
| ruby_memory_bytes | Gauge | 11.1 | Memory usage by process |
| ruby_sampler_duration_seconds_total | Counter | 11.1 | Time spent collecting stats |
| ruby_process_cpu_seconds_total | Gauge | 11.11 | Total amount of cpu time per process |
| ruby_process_cpu_seconds_total | Gauge | 11.11 | Total amount of CPU time per process |
| ruby_process_max_fds | Gauge | 11.11 | Maximum number of open file descriptors per process |
| ruby_process_resident_memory_bytes | Gauge | 11.11 | Memory usage by process |
| ruby_process_start_time_seconds | Gauge | 11.11 | The time the process started after system boot in seconds |
[GC.stat]: https://ruby-doc.org/core-2.3.0/GC.html#method-c-stat

View file

@ -25,6 +25,7 @@ module Gitlab
def init_metrics
metrics = {
file_descriptors: ::Gitlab::Metrics.gauge(with_prefix(:file, :descriptors), 'File descriptors used', labels, :livesum),
memory_bytes: ::Gitlab::Metrics.gauge(with_prefix(:memory, :bytes), 'Memory used', labels, :livesum),
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),
@ -46,8 +47,8 @@ module Gitlab
metrics[:file_descriptors].set(labels.merge(worker_label), System.file_descriptor_count)
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)
metrics[:process_resident_memory_bytes].set(labels.merge(worker_label), System.memory_usage)
metrics[:process_start_time_seconds].set(labels.merge(worker_label), ::Gitlab::Metrics::System.process_start_time)
set_memory_usage_metrics
sample_gc
metrics[:sampler_duration].increment(labels, System.monotonic_time - start_time)
@ -67,6 +68,14 @@ module Gitlab
metrics[:total_time].increment(labels, GC::Profiler.total_time)
end
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
def worker_label
return {} unless defined?(Unicorn::Worker)

View file

@ -27,13 +27,10 @@ module Gitlab
def sample
Raindrops::Linux.tcp_listener_stats(tcp_listeners).each do |addr, stats|
metrics[:unicorn_active_connections].set({ socket_type: 'tcp', socket_address: addr }, stats.active)
metrics[:unicorn_queued_connections].set({ socket_type: 'tcp', socket_address: addr }, stats.queued)
set_unicorn_connection_metrics('tcp', addr, stats)
end
Raindrops::Linux.unix_listener_stats(unix_listeners).each do |addr, stats|
metrics[:unicorn_active_connections].set({ socket_type: 'unix', socket_address: addr }, stats.active)
metrics[:unicorn_queued_connections].set({ socket_type: 'unix', socket_address: addr }, stats.queued)
set_unicorn_connection_metrics('unix', addr, stats)
end
metrics[:unicorn_workers].set({}, unicorn_workers_count)
@ -45,8 +42,11 @@ module Gitlab
@tcp_listeners ||= Unicorn.listener_names.grep(%r{\A[^/]+:\d+\z})
end
def pid
@pid ||= Process.pid
def set_unicorn_connection_metrics(type, addr, stats)
labels = { socket_type: type, socket_address: addr }
metrics[:unicorn_active_connections].set(labels, stats.active)
metrics[:unicorn_queued_connections].set(labels, stats.queued)
end
def unix_listeners
@ -58,7 +58,7 @@ module Gitlab
end
def unicorn_workers_count
Sys::ProcTable.ps.select {|p| p.cmdline.match(/unicorn_rails worker/)}.count
Sys::ProcTable.ps.select {|p| p.cmdline.match(/unicorn_rails worker.+ #{Rails.root.to_s}/)}.count
end
end
end