Class: GitLab::Monitor::GitProcessProber

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_monitor/git.rb

Overview

A special prober for git processes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, metrics: PrometheusMetrics.new) ⇒ GitProcessProber

Returns a new instance of GitProcessProber.


88
89
90
91
# File 'lib/gitlab_monitor/git.rb', line 88

def initialize(opts, metrics: PrometheusMetrics.new)
  @opts    = opts
  @metrics = metrics
end

Class Method Details

.extract_subcommand(cmd) ⇒ Object


132
133
134
135
136
137
138
139
140
141
# File 'lib/gitlab_monitor/git.rb', line 132

def self.extract_subcommand(cmd)
  return if cmd.empty?
  cmd_splitted = cmd.split("\u0000") # cmdline does not return it space-separated

  cmd_splitted.shift # Because it's "git"
  cmd_splitted.shift while cmd_splitted.first &&
                           (cmd_splitted.first.empty? || cmd_splitted.first !~ /^[^-][a-z\-]*$/)

  cmd_splitted[0]
end

Instance Method Details

#probe_gitObject

rubocop:disable Metrics/MethodLength


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gitlab_monitor/git.rb', line 93

def probe_git # rubocop:disable Metrics/MethodLength
  counts = Hash.new(0)

  Utils.pgrep("^git ").each do |pid|
    process_cmd = begin
                    File.read("/proc/#{pid}/cmdline")
                  rescue
                    "" # Process file is gone (race condition)
                  end
    subcommand  = self.class.extract_subcommand(process_cmd)
    next unless subcommand # Unlikely, but just to be safe

    name = "git #{subcommand}"
    counts[name] += 1

    prober = ProcessProber.new(
      {
        name: name,
        pid_or_pattern: pid,
        quantiles: @opts[:quantiles]
      },
      metrics: @metrics
    )

    prober
      .probe_stat
  end

  counts.each do |name, count|
    @metrics.add("process_count", count, name: name)
  end

  self
end

#write_to(target) ⇒ Object


128
129
130
# File 'lib/gitlab_monitor/git.rb', line 128

def write_to(target)
  target.write(@metrics.to_s)
end