2021-12-02 22:14:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-12-08 10:13:43 -05:00
|
|
|
require_relative '../config/boot'
|
2021-12-02 22:14:42 -05:00
|
|
|
|
|
|
|
class MetricsServer # rubocop:disable Gitlab/NamespacedClass
|
2022-03-15 08:07:44 -04:00
|
|
|
# The singleton instance used to supervise the Puma metrics server.
|
|
|
|
PumaProcessSupervisor = Class.new(Gitlab::ProcessSupervisor)
|
|
|
|
|
2021-12-02 22:14:42 -05:00
|
|
|
class << self
|
2022-04-25 17:09:46 -04:00
|
|
|
def version
|
|
|
|
Rails.root.join('GITLAB_METRICS_EXPORTER_VERSION').read.chomp
|
|
|
|
end
|
|
|
|
|
2022-03-15 08:07:44 -04:00
|
|
|
def start_for_puma
|
|
|
|
metrics_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
|
|
|
|
|
|
|
|
start_server = proc do
|
|
|
|
MetricsServer.spawn('puma', metrics_dir: metrics_dir).tap do |pid|
|
|
|
|
Gitlab::AppLogger.info("Starting Puma metrics server with pid #{pid}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
supervisor = PumaProcessSupervisor.instance
|
|
|
|
supervisor.supervise(start_server.call) do
|
|
|
|
Gitlab::AppLogger.info('Puma metrics server terminated, restarting...')
|
|
|
|
start_server.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-25 17:09:46 -04:00
|
|
|
def start_for_sidekiq(**options)
|
|
|
|
if new_metrics_server?
|
|
|
|
self.spawn('sidekiq', **options)
|
|
|
|
else
|
|
|
|
self.fork('sidekiq', **options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def spawn(target, metrics_dir:, **options)
|
|
|
|
return spawn_ruby_server(target, metrics_dir: metrics_dir, **options) unless new_metrics_server?
|
|
|
|
|
2022-05-09 14:08:30 -04:00
|
|
|
name = settings_key(target)
|
|
|
|
settings = ::Settings.monitoring[name]
|
2022-04-25 17:09:46 -04:00
|
|
|
path = options[:path]&.then { |p| Pathname.new(p) } || Pathname.new('')
|
|
|
|
cmd = path.join('gitlab-metrics-exporter').to_path
|
|
|
|
env = {
|
|
|
|
'GME_MMAP_METRICS_DIR' => metrics_dir.to_s,
|
|
|
|
'GME_PROBES' => 'self,mmap',
|
|
|
|
'GME_SERVER_HOST' => settings['address'],
|
|
|
|
'GME_SERVER_PORT' => settings['port'].to_s
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:08:30 -04:00
|
|
|
if settings['log_enabled']
|
|
|
|
env['GME_LOG_FILE'] = File.join(Rails.root, 'log', "#{name}.log")
|
|
|
|
env['GME_LOG_LEVEL'] = 'info'
|
|
|
|
else
|
|
|
|
env['GME_LOG_LEVEL'] = 'quiet'
|
|
|
|
end
|
|
|
|
|
2022-06-29 17:09:23 -04:00
|
|
|
if settings['tls_enabled']
|
|
|
|
env['GME_CERT_FILE'] = settings['tls_cert_path']
|
|
|
|
env['GME_CERT_KEY'] = settings['tls_key_path']
|
|
|
|
end
|
|
|
|
|
2022-04-25 17:09:46 -04:00
|
|
|
Process.spawn(env, cmd, err: $stderr, out: $stdout, pgroup: true).tap do |pid|
|
|
|
|
Process.detach(pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def spawn_ruby_server(target, metrics_dir:, wipe_metrics_dir: false, **options)
|
2022-02-15 04:17:01 -05:00
|
|
|
ensure_valid_target!(target)
|
|
|
|
|
|
|
|
cmd = "#{Rails.root}/bin/metrics-server"
|
|
|
|
env = {
|
|
|
|
'METRICS_SERVER_TARGET' => target,
|
2022-04-25 17:09:46 -04:00
|
|
|
'WIPE_METRICS_DIR' => wipe_metrics_dir ? '1' : '0',
|
|
|
|
'GITLAB_CONFIG' => ENV['GITLAB_CONFIG']
|
2022-02-15 04:17:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Process.spawn(env, cmd, err: $stderr, out: $stdout, pgroup: true).tap do |pid|
|
|
|
|
Process.detach(pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fork(target, metrics_dir:, wipe_metrics_dir: false, reset_signals: [])
|
|
|
|
ensure_valid_target!(target)
|
2021-12-15 19:15:50 -05:00
|
|
|
|
|
|
|
pid = Process.fork
|
|
|
|
|
|
|
|
if pid.nil? # nil means we're inside the fork
|
|
|
|
# Remove any custom signal handlers the parent process had registered, since we do
|
|
|
|
# not want to inherit them, and Ruby forks with a `clone` that has the `CLONE_SIGHAND`
|
|
|
|
# flag set.
|
2022-02-15 04:17:01 -05:00
|
|
|
Gitlab::ProcessManagement.modify_signals(reset_signals, 'DEFAULT')
|
2021-12-15 19:15:50 -05:00
|
|
|
|
|
|
|
server = MetricsServer.new(target, metrics_dir, wipe_metrics_dir)
|
|
|
|
# This rewrites /proc/cmdline, since otherwise tools like `top` will show the
|
|
|
|
# parent process `cmdline` which is really confusing.
|
|
|
|
$0 = server.name
|
|
|
|
|
|
|
|
server.start
|
|
|
|
else
|
2021-12-02 22:14:42 -05:00
|
|
|
Process.detach(pid)
|
|
|
|
end
|
2021-12-15 19:15:50 -05:00
|
|
|
|
|
|
|
pid
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
2022-02-15 04:17:01 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-04-25 17:09:46 -04:00
|
|
|
def new_metrics_server?
|
|
|
|
Gitlab::Utils.to_boolean(ENV['GITLAB_GOLANG_METRICS_SERVER'])
|
|
|
|
end
|
|
|
|
|
2022-02-15 04:17:01 -05:00
|
|
|
def ensure_valid_target!(target)
|
|
|
|
raise "Target must be one of [puma,sidekiq]" unless %w(puma sidekiq).include?(target)
|
|
|
|
end
|
2022-04-25 17:09:46 -04:00
|
|
|
|
2022-05-09 14:08:30 -04:00
|
|
|
def settings_key(target)
|
|
|
|
case target
|
|
|
|
when 'puma' then 'web_exporter'
|
|
|
|
when 'sidekiq' then 'sidekiq_exporter'
|
|
|
|
else ensure_valid_target!(target)
|
|
|
|
end
|
2022-04-25 17:09:46 -04:00
|
|
|
end
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
|
|
|
|
2021-12-08 10:13:43 -05:00
|
|
|
def initialize(target, metrics_dir, wipe_metrics_dir)
|
2021-12-02 22:14:42 -05:00
|
|
|
@target = target
|
|
|
|
@metrics_dir = metrics_dir
|
2021-12-08 10:13:43 -05:00
|
|
|
@wipe_metrics_dir = wipe_metrics_dir
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
::Prometheus::Client.configure do |config|
|
|
|
|
config.multiprocess_files_dir = @metrics_dir
|
2022-02-15 04:17:01 -05:00
|
|
|
config.pid_provider = proc { name }
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(@metrics_dir, mode: 0700)
|
2022-03-25 08:07:52 -04:00
|
|
|
::Prometheus::CleanupMultiprocDirService.new(@metrics_dir).execute if @wipe_metrics_dir
|
2021-12-02 22:14:42 -05:00
|
|
|
|
2021-12-20 10:12:25 -05:00
|
|
|
# We need to `warmup: true` since otherwise the sampler and exporter threads enter
|
|
|
|
# a race where not all Prometheus db files will be visible to the exporter, resulting
|
|
|
|
# in missing metrics.
|
|
|
|
# Warming up ensures that these files exist prior to the exporter starting up.
|
2022-01-24 04:16:59 -05:00
|
|
|
Gitlab::Metrics::Samplers::RubySampler.initialize_instance(prefix: name, warmup: true).start
|
2021-12-15 19:15:50 -05:00
|
|
|
|
2022-02-10 16:15:20 -05:00
|
|
|
default_opts = { gc_requests: true, synchronous: true }
|
|
|
|
exporter =
|
|
|
|
case @target
|
|
|
|
when 'puma'
|
|
|
|
Gitlab::Metrics::Exporter::WebExporter.instance(**default_opts)
|
2022-02-15 04:17:01 -05:00
|
|
|
when 'sidekiq'
|
2022-02-10 16:15:20 -05:00
|
|
|
settings = Settings.new(Settings.monitoring[name])
|
2022-02-15 04:17:01 -05:00
|
|
|
Gitlab::Metrics::Exporter::SidekiqExporter.instance(settings, **default_opts)
|
2022-02-10 16:15:20 -05:00
|
|
|
end
|
2021-12-02 22:14:42 -05:00
|
|
|
|
2022-02-10 16:15:20 -05:00
|
|
|
exporter.start
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
2021-12-15 19:15:50 -05:00
|
|
|
|
|
|
|
def name
|
2022-02-15 04:17:01 -05:00
|
|
|
case @target
|
|
|
|
when 'puma' then 'web_exporter'
|
|
|
|
when 'sidekiq' then 'sidekiq_exporter'
|
|
|
|
end
|
2021-12-15 19:15:50 -05:00
|
|
|
end
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|