summaryrefslogtreecommitdiff
path: root/metrics_server/metrics_server.rb
blob: 09171d8220b02a03d7728804fd2d9e83c9a4f891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

require_relative '../config/bundler_setup'

require_relative 'dependencies'

class MetricsServer # rubocop:disable Gitlab/NamespacedClass
  class << self
    def spawn(target, gitlab_config: nil)
      cmd = "#{Rails.root}/bin/metrics-server"
      env = {
        'METRICS_SERVER_TARGET' => target,
        'GITLAB_CONFIG' => gitlab_config
      }

      Process.spawn(env, cmd, err: $stderr, out: $stdout).tap do |pid|
        Process.detach(pid)
      end
    end
  end

  def initialize(target, metrics_dir)
    @target = target
    @metrics_dir = 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

    settings = Settings.monitoring.sidekiq_exporter
    exporter_class = "Gitlab::Metrics::Exporter::#{@target.camelize}Exporter".constantize
    server = exporter_class.instance(settings, synchronous: true)

    server.start
  end
end