summaryrefslogtreecommitdiff
path: root/lib/prometheus/pid_provider.rb
blob: 32beeb0d31ebda1cd983efb4a10144afbe165e05 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

module Prometheus
  module PidProvider
    extend self

    def worker_id
      if Gitlab::Runtime.sidekiq?
        sidekiq_worker_id
      elsif Gitlab::Runtime.unicorn?
        unicorn_worker_id
      elsif Gitlab::Runtime.puma?
        puma_worker_id
      else
        unknown_process_id
      end
    end

    private

    def sidekiq_worker_id
      if worker = ENV['SIDEKIQ_WORKER_ID']
        "sidekiq_#{worker}"
      else
        'sidekiq'
      end
    end

    def unicorn_worker_id
      if matches = process_name.match(/unicorn.*worker\[([0-9]+)\]/)
        "unicorn_#{matches[1]}"
      elsif process_name =~ /unicorn/
        "unicorn_master"
      else
        unknown_process_id
      end
    end

    def puma_worker_id
      if matches = process_name.match(/puma.*cluster worker ([0-9]+):/)
        "puma_#{matches[1]}"
      elsif process_name =~ /puma/
        "puma_master"
      else
        unknown_process_id
      end
    end

    def unknown_process_id
      "process_#{Process.pid}"
    end

    def process_name
      $0
    end
  end
end