summaryrefslogtreecommitdiff
path: root/metrics_server/metrics_server.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-12-20 13:37:47 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-12-20 13:37:47 +0000
commitaee0a117a889461ce8ced6fcf73207fe017f1d99 (patch)
tree891d9ef189227a8445d83f35c1b0fc99573f4380 /metrics_server/metrics_server.rb
parent8d46af3258650d305f53b819eabf7ab18d22f59e (diff)
downloadgitlab-ce-aee0a117a889461ce8ced6fcf73207fe017f1d99.tar.gz
Add latest changes from gitlab-org/gitlab@14-6-stable-eev14.6.0-rc42
Diffstat (limited to 'metrics_server/metrics_server.rb')
-rw-r--r--metrics_server/metrics_server.rb59
1 files changed, 59 insertions, 0 deletions
diff --git a/metrics_server/metrics_server.rb b/metrics_server/metrics_server.rb
new file mode 100644
index 00000000000..56fc20dcc9d
--- /dev/null
+++ b/metrics_server/metrics_server.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+require_relative '../config/boot'
+
+require_relative 'dependencies'
+
+class MetricsServer # rubocop:disable Gitlab/NamespacedClass
+ class << self
+ def spawn(target, metrics_dir:, wipe_metrics_dir: false, trapped_signals: [])
+ raise "The only valid target is 'sidekiq' currently" unless target == 'sidekiq'
+
+ 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.
+ Gitlab::ProcessManagement.modify_signals(trapped_signals, 'DEFAULT')
+
+ 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
+ Process.detach(pid)
+ end
+
+ pid
+ end
+ end
+
+ def initialize(target, metrics_dir, wipe_metrics_dir)
+ @target = target
+ @metrics_dir = metrics_dir
+ @wipe_metrics_dir = wipe_metrics_dir
+ end
+
+ def start
+ ::Prometheus::Client.configure do |config|
+ config.multiprocess_files_dir = @metrics_dir
+ end
+
+ FileUtils.mkdir_p(@metrics_dir, mode: 0700)
+ ::Prometheus::CleanupMultiprocDirService.new.execute if @wipe_metrics_dir
+
+ settings = Settings.new(Settings.monitoring[name])
+
+ exporter_class = "Gitlab::Metrics::Exporter::#{@target.camelize}Exporter".constantize
+ server = exporter_class.instance(settings, synchronous: true)
+
+ server.start
+ end
+
+ def name
+ "#{@target}_exporter"
+ end
+end