2017-07-19 04:54:39 -04:00
|
|
|
require 'prometheus/client'
|
|
|
|
|
2019-07-02 03:36:33 -04:00
|
|
|
# Keep separate directories for separate processes
|
|
|
|
def prometheus_default_multiproc_dir
|
|
|
|
return unless Rails.env.development? || Rails.env.test?
|
|
|
|
|
|
|
|
if Sidekiq.server?
|
|
|
|
Rails.root.join('tmp/prometheus_multiproc_dir/sidekiq')
|
|
|
|
elsif defined?(Unicorn::Worker)
|
|
|
|
Rails.root.join('tmp/prometheus_multiproc_dir/unicorn')
|
|
|
|
elsif defined?(::Puma)
|
|
|
|
Rails.root.join('tmp/prometheus_multiproc_dir/puma')
|
|
|
|
else
|
|
|
|
Rails.root.join('tmp/prometheus_multiproc_dir')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-19 04:54:39 -04:00
|
|
|
Prometheus::Client.configure do |config|
|
2019-07-10 15:26:47 -04:00
|
|
|
config.logger = Rails.logger # rubocop:disable Gitlab/RailsLogger
|
2017-07-19 04:54:39 -04:00
|
|
|
|
|
|
|
config.initial_mmap_file_size = 4 * 1024
|
|
|
|
|
2019-07-02 03:36:33 -04:00
|
|
|
config.multiprocess_files_dir = ENV['prometheus_multiproc_dir'] || prometheus_default_multiproc_dir
|
2017-08-29 09:45:19 -04:00
|
|
|
|
2019-07-18 09:54:11 -04:00
|
|
|
config.pid_provider = Prometheus::PidProvider.method(:worker_id)
|
2017-07-19 04:54:39 -04:00
|
|
|
end
|
2017-08-07 13:13:02 -04:00
|
|
|
|
2017-11-22 18:26:50 -05:00
|
|
|
Gitlab::Application.configure do |config|
|
|
|
|
# 0 should be Sentry to catch errors in this middleware
|
|
|
|
config.middleware.insert(1, Gitlab::Metrics::RequestsRackMiddleware)
|
|
|
|
end
|
|
|
|
|
2019-06-13 21:41:29 -04:00
|
|
|
Sidekiq.configure_server do |config|
|
|
|
|
config.on(:startup) do
|
|
|
|
Gitlab::Metrics::SidekiqMetricsExporter.instance.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-24 09:02:39 -04:00
|
|
|
if !Rails.env.test? && Gitlab::Metrics.prometheus_metrics_enabled?
|
2018-10-20 14:00:19 -04:00
|
|
|
Gitlab::Cluster::LifecycleEvents.on_worker_start do
|
|
|
|
defined?(::Prometheus::Client.reinitialize_on_pid_change) && Prometheus::Client.reinitialize_on_pid_change
|
|
|
|
|
|
|
|
Gitlab::Metrics::Samplers::RubySampler.initialize_instance(Settings.monitoring.ruby_sampler_interval).start
|
2017-11-02 16:23:23 -04:00
|
|
|
end
|
2019-05-29 08:27:44 -04:00
|
|
|
|
2019-07-02 13:05:56 -04:00
|
|
|
Gitlab::Cluster::LifecycleEvents.on_master_start do
|
|
|
|
if defined?(::Unicorn)
|
|
|
|
Gitlab::Metrics::Samplers::UnicornSampler.initialize_instance(Settings.monitoring.unicorn_sampler_interval).start
|
|
|
|
elsif defined?(::Puma)
|
2019-05-29 08:27:44 -04:00
|
|
|
Gitlab::Metrics::Samplers::PumaSampler.initialize_instance(Settings.monitoring.puma_sampler_interval).start
|
|
|
|
end
|
|
|
|
end
|
2018-10-20 14:00:19 -04:00
|
|
|
end
|
2017-11-02 16:23:23 -04:00
|
|
|
|
2019-07-01 11:32:19 -04:00
|
|
|
def cleanup_prometheus_multiproc_dir
|
2018-10-20 14:00:19 -04:00
|
|
|
# The following is necessary to ensure stale Prometheus metrics don't
|
|
|
|
# accumulate over time. It needs to be done in this hook as opposed to
|
|
|
|
# inside an init script to ensure metrics files aren't deleted after new
|
|
|
|
# unicorn workers start after a SIGUSR2 is received.
|
2019-07-01 11:32:19 -04:00
|
|
|
if dir = ::Prometheus::Client.configuration.multiprocess_files_dir
|
|
|
|
old_metrics = Dir[File.join(dir, '*.db')]
|
2018-10-20 14:00:19 -04:00
|
|
|
FileUtils.rm_rf(old_metrics)
|
|
|
|
end
|
2017-09-05 19:26:31 -04:00
|
|
|
end
|
2019-07-01 11:32:19 -04:00
|
|
|
|
|
|
|
Gitlab::Cluster::LifecycleEvents.on_master_start do
|
|
|
|
cleanup_prometheus_multiproc_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Cluster::LifecycleEvents.on_master_restart do
|
|
|
|
cleanup_prometheus_multiproc_dir
|
|
|
|
end
|