Class: PrometheusClientAddons::Prometheus::Client::Puma

Inherits:
MultiMetric
  • Object
show all
Defined in:
lib/prometheus_client_addons/prometheus/client/puma.rb

Constant Summary collapse

HANDLES =
{
  backlog: 'How many requests are waiting',
  running: 'Number of running threads',
  pool_capacity: 'Number of requests that can be processed right now',
  max_threads: 'Maximum number of worker threads',

  workers: 'Number of workers',
  phase: 'Phase of worker',
  booted_workers: 'Number of booted workers',
  old_workers: 'Number of old workers',

  worker_status_phase: 'Phase of worker',
  worker_status_booted: 'Booted or not',
  worker_status_last_checkin: 'Last update',

  worker_status_last_status_backlog: 'How many objects have yet to be processed by the pool',
  worker_status_last_status_running: 'Number of running threads',
  worker_status_last_status_pool_capacity: 'Number of requests that can be processed right now',
  worker_status_last_status_max_threads: 'Maximum number of worker threads'
}.freeze

Class Attribute Summary collapse

Attributes inherited from MultiMetric

#base_labels, #name, #prefix

Instance Method Summary collapse

Methods inherited from MultiMetric

#initialize

Constructor Details

This class inherits a constructor from PrometheusClientAddons::Prometheus::Client::MultiMetric

Class Attribute Details

.control_auth_tokenObject

Returns the value of attribute control_auth_token.



11
12
13
# File 'lib/prometheus_client_addons/prometheus/client/puma.rb', line 11

def control_auth_token
  @control_auth_token
end

.control_urlObject

Returns the value of attribute control_url.



11
12
13
# File 'lib/prometheus_client_addons/prometheus/client/puma.rb', line 11

def control_url
  @control_url
end

Instance Method Details

#extract(input, output, nested, label_set = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prometheus_client_addons/prometheus/client/puma.rb', line 44

def extract(input, output, nested, label_set = {})
  key = if input.key?(nested.last.to_s)
    nested.last.to_s
  elsif input.key?(nested.last.to_sym)
    nested.last.to_sym
  end
  return unless key

  output["#{prefix}#{nested.join('_')}".to_sym] ||= {}
  output["#{prefix}#{nested.join('_')}".to_sym][label_set] = input[key]
end

#multi_name_docstringObject



40
41
42
# File 'lib/prometheus_client_addons/prometheus/client/puma.rb', line 40

def multi_name_docstring
  Hash[HANDLES.map { |key, value| ["#{prefix}#{key}".to_sym, value] }]
end

#multi_name_typeObject



35
36
37
38
# File 'lib/prometheus_client_addons/prometheus/client/puma.rb', line 35

def multi_name_type
  full_handles = HANDLES.keys.map { |key| "#{prefix}#{key}" }.map(&:to_sym)
  Hash[full_handles.zip([:gauge] * HANDLES.size)]
end

#multi_valuesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/prometheus_client_addons/prometheus/client/puma.rb', line 56

def multi_values
  body = Socket.unix(self.class.control_url.gsub("unix://", '')) do |socket|
    socket << "GET /stats?token=#{self.class.control_auth_token} HTTP/1.0\r\n\r\n"
    socket.read
  end

  data = JSON.parse(body.split("\n").last, symbolize_names: true)

  result = {}

  extract(data, result, [:backlog])
  extract(data, result, [:running])
  extract(data, result, [:pool_capacity])
  extract(data, result, [:max_threads])

  extract(data, result, [:workers])
  extract(data, result, [:phase])
  extract(data, result, [:booted_workers])
  extract(data, result, [:old_workers])

  if data.key?(:worker_status) && !data[:worker_status].empty?
    data[:worker_status].each do |worker_status|
      worker_index = worker_status[:index]

      extract(worker_status, result, [:worker_status, :phase], { worker_index: worker_index })
      extract(worker_status, result, [:worker_status, :booted], { worker_index: worker_index })
      extract(worker_status, result, [:worker_status, :last_checkin], { worker_index: worker_index })

      last_status = worker_status[:last_status]
      extract(last_status, result, [:worker_status, :last_status, :backlog], { worker_index: worker_index })
      extract(last_status, result, [:worker_status, :last_status, :running], { worker_index: worker_index })
      extract(last_status, result, [:worker_status, :last_status, :pool_capacity], { worker_index: worker_index })
      extract(last_status, result, [:worker_status, :last_status, :max_threads], { worker_index: worker_index })
    end
  end

  result
end