diff options
Diffstat (limited to 'config/initializers/7_prometheus_metrics.rb')
-rw-r--r-- | config/initializers/7_prometheus_metrics.rb | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/config/initializers/7_prometheus_metrics.rb b/config/initializers/7_prometheus_metrics.rb index 68f8487d377..741c8ef1ca0 100644 --- a/config/initializers/7_prometheus_metrics.rb +++ b/config/initializers/7_prometheus_metrics.rb @@ -1,15 +1,27 @@ require 'prometheus/client' require 'prometheus/client/support/unicorn' +# 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 + Prometheus::Client.configure do |config| config.logger = Rails.logger config.initial_mmap_file_size = 4 * 1024 - config.multiprocess_files_dir = ENV['prometheus_multiproc_dir'] - if Rails.env.development? || Rails.env.test? - config.multiprocess_files_dir ||= Rails.root.join('tmp/prometheus_multiproc_dir') - end + config.multiprocess_files_dir = ENV['prometheus_multiproc_dir'] || prometheus_default_multiproc_dir config.pid_provider = Prometheus::Client::Support::Unicorn.method(:worker_pid_provider) end @@ -29,28 +41,33 @@ if !Rails.env.test? && Gitlab::Metrics.prometheus_metrics_enabled? Gitlab::Cluster::LifecycleEvents.on_worker_start do defined?(::Prometheus::Client.reinitialize_on_pid_change) && Prometheus::Client.reinitialize_on_pid_change - if defined?(::Unicorn) - Gitlab::Metrics::Samplers::UnicornSampler.initialize_instance(Settings.monitoring.unicorn_sampler_interval).start - end - Gitlab::Metrics::Samplers::RubySampler.initialize_instance(Settings.monitoring.ruby_sampler_interval).start end - if defined?(::Puma) - Gitlab::Cluster::LifecycleEvents.on_master_start do + 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) Gitlab::Metrics::Samplers::PumaSampler.initialize_instance(Settings.monitoring.puma_sampler_interval).start end end end -Gitlab::Cluster::LifecycleEvents.on_master_restart do +def cleanup_prometheus_multiproc_dir # 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. - prometheus_multiproc_dir = ENV['prometheus_multiproc_dir'] - if prometheus_multiproc_dir - old_metrics = Dir[File.join(prometheus_multiproc_dir, '*.db')] + if dir = ::Prometheus::Client.configuration.multiprocess_files_dir + old_metrics = Dir[File.join(dir, '*.db')] FileUtils.rm_rf(old_metrics) end end + +Gitlab::Cluster::LifecycleEvents.on_master_start do + cleanup_prometheus_multiproc_dir +end + +Gitlab::Cluster::LifecycleEvents.on_master_restart do + cleanup_prometheus_multiproc_dir +end |