Commit 9c64662b authored by Ahmad Sherif's avatar Ahmad Sherif
Browse files

Merge branch 'bjk/process' into 'master'

Cleanup process collector

See merge request gitlab-org/gitlab-monitor!78
parents ccc81dcb c3b68263
......@@ -29,9 +29,10 @@ metrics.
`git_pull_time_milliseconds`, `git_push_time_milliseconds`
* git processes stats (see Process below)
1. [Process](lib/gitlab_monitor/process.rb)
* Age -- `process_age_seconds`
* CPU time -- `process_cpu_seconds_total`
* Start time -- `process_start_time_seconds`
* Count -- `process_count`
* Memory usage -- `process_memory_bytes`
* Memory usage -- `process_resident_memory_bytes`, `process_virtual_memory_bytes`
1. [Sidekiq](lib/gitlab_monitor/sidekiq.rb) -- `sidekiq_queue_size`, `sidekiq_queue_paused`,
`sidekiq_queue_latency_seconds`, `sidekiq_enqueued_jobs`, `sidekiq_dead_jobs`,
`sidekiq_running_jobs`, `sidekiq_to_be_retried_jobs`
......
......@@ -57,8 +57,7 @@ probes:
process: &process
methods:
- probe_memory
- probe_age
- probe_stat
- probe_count
opts:
- pid_or_pattern: "sidekiq .* \\[.*?\\]"
......
......@@ -236,8 +236,7 @@ module GitLab
def run
::GitLab::Monitor::ProcessProber.new(pid_or_pattern: @pid || @pattern, name: @name, quantiles: @quantiles)
.probe_memory
.probe_age
.probe_stat
.probe_count
.write_to(@target)
end
......
......@@ -115,8 +115,7 @@ module GitLab
)
prober
.probe_memory
.probe_age
.probe_stat
end
counts.each do |name, count|
......
......@@ -2,71 +2,59 @@ module GitLab
module Monitor
# A helper class to extract memory info from /proc/<pid>/status
#
# It takes a pid
class ProcessMemory
def initialize(pid)
@pid = pid
@info = {}
populate_info
end
def [](field)
@info[field]
end
private
def populate_info
status = File.read("/proc/#{@pid}/status")
status.each_line do |line|
line.strip!
next unless line.end_with?("kB") # We're only interested in sizes
match = line.match(/(\w+):\s*(\d+) kB/)
next unless match
@info[match[1]] = match[2].to_i * 1024
end
rescue Errno::ENOENT
nil
end
end
# A helper class to stats from /proc/<pid>/stat
#
# See: man 5 proc
#
# It takes a pid
class ProcessStats
def initialize(pid)
@pid = pid
@stats = nil
populate_info
@pid = pid
@user_hertz = retrieve_user_hertz
@stats = populate_info
end
def valid?
!@stats.nil?
end
def age
Utils.system_uptime - (@stats[21].to_i / Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz))
def cpu_time
(@stats[14].to_i + @stats[15].to_i) / @user_hertz
end
def start_time
@stats[22].to_i / @user_hertz
end
def vsize
# Virtual memory size in bytes.
@stats[23].to_i
end
def rss
# Resident Set Size: number of pages the process has in real memory.
@stats[24].to_i * 4096
end
private
def populate_info
@stats = File.read("/proc/#{@pid}/stat").split(" ")
# Pad the array by one element to make field numbers match the man page.
[""].concat(File.read("/proc/#{@pid}/stat").split(" "))
rescue Errno::ENOENT
nil
end
def retrieve_user_hertz
Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz)
rescue Errno::EINVAL
100.0
end
end
# Probes a process for info then writes metrics to a target
class ProcessProber
MEMORY_FIELDS = %w(RssFile RssAnon VmRSS VmData).freeze
def initialize(options, metrics: PrometheusMetrics.new)
@metrics = metrics
@name = options[:name]
......@@ -78,25 +66,7 @@ module GitLab
@use_quantiles = options.fetch(:quantiles, false)
end
def probe_memory
@pids.each do |pid|
memory = ProcessMemory.new(pid)
MEMORY_FIELDS.each do |field|
value = memory[field]
next unless value
labels = { name: @name.downcase, field: field }
labels[:pid] = pid unless @use_quantiles
@metrics.add("process_memory_bytes", value, @use_quantiles, **labels)
end
end
self
end
def probe_age
def probe_stat
@pids.each do |pid|
stats = ProcessStats.new(pid)
next unless stats.valid?
......@@ -104,7 +74,10 @@ module GitLab
labels = { name: @name.downcase }
labels[:pid] = pid unless @use_quantiles
@metrics.add("process_age_seconds", stats.age, @use_quantiles, **labels)
@metrics.add("process_cpu_seconds_total", stats.cpu_time, @use_quantiles, **labels)
@metrics.add("process_resident_memory_bytes", stats.rss, @use_quantiles, **labels)
@metrics.add("process_virtual_memory_bytes", stats.vsize, @use_quantiles, **labels)
@metrics.add("process_start_time_seconds", stats.start_time, @use_quantiles, **labels)
end
self
......
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