Class: PrometheusExporter::Instrumentation::Puma

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus_exporter/instrumentation/puma.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric_labels = {}) ⇒ Puma

Returns a new instance of Puma.



25
26
27
# File 'lib/prometheus_exporter/instrumentation/puma.rb', line 25

def initialize(metric_labels = {})
  @metric_labels = metric_labels
end

Class Method Details

.start(client: nil, frequency: 30, labels: {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/prometheus_exporter/instrumentation/puma.rb', line 8

def self.start(client: nil, frequency: 30, labels: {})
  puma_collector = new(labels)
  client ||= PrometheusExporter::Client.default
  Thread.new do
    while true
      begin
        metric = puma_collector.collect
        client.send_json metric
      rescue => e
        client.logger.error("Prometheus Exporter Failed To Collect Puma Stats #{e}")
      ensure
        sleep frequency
      end
    end
  end
end

Instance Method Details

#collectObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/prometheus_exporter/instrumentation/puma.rb', line 29

def collect
  metric = {
    pid: pid,
    type: "puma",
    hostname: ::PrometheusExporter.hostname,
    metric_labels: @metric_labels
  }
  collect_puma_stats(metric)
  metric
end

#collect_puma_stats(metric) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/prometheus_exporter/instrumentation/puma.rb', line 44

def collect_puma_stats(metric)
  stats = JSON.parse(::Puma.stats)

  if stats.key?("workers")
    metric[:phase] = stats["phase"]
    metric[:workers] = stats["workers"]
    metric[:booted_workers] = stats["booted_workers"]
    metric[:old_workers] = stats["old_workers"]

    stats["worker_status"].each do |worker|
      next if worker["last_status"].empty?
      collect_worker_status(metric, worker["last_status"])
    end
  else
    collect_worker_status(metric, stats)
  end
end

#pidObject



40
41
42
# File 'lib/prometheus_exporter/instrumentation/puma.rb', line 40

def pid
  @pid = ::Process.pid
end