summaryrefslogtreecommitdiff
path: root/lib/prometheus/pid_provider.rb
blob: c92522c73c53792c6364b8093ed84ca1c52cbe55 (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
42
# frozen_string_literal: true

require 'prometheus/client/support/unicorn'

module Prometheus
  module PidProvider
    extend self

    def worker_id
      if Sidekiq.server?
        'sidekiq'
      elsif defined?(Unicorn::Worker)
        "unicorn_#{unicorn_worker_id}"
      elsif defined?(::Puma)
        "puma_#{puma_worker_id}"
      else
        "process_#{Process.pid}"
      end
    end

    private

    # This is not fully accurate as we don't really know if the nil returned
    # is actually means we're on master or not.
    # Follow up issue was created to address this problem and
    # to introduce more structrured approach to a current process discovery:
    # https://gitlab.com/gitlab-org/gitlab-ce/issues/64740
    def unicorn_worker_id
      ::Prometheus::Client::Support::Unicorn.worker_id || 'master'
    end

    # See the comment for #unicorn_worker_id
    def puma_worker_id
      match = process_name.match(/cluster worker ([0-9]+):/)
      match ? match[1] : 'master'
    end

    def process_name
      $0
    end
  end
end