Class: PrometheusExporter::Server::PumaCollector

Inherits:
TypeCollector show all
Defined in:
lib/prometheus_exporter/server/puma_collector.rb

Constant Summary collapse

MAX_PUMA_METRIC_AGE =
30
PUMA_GAUGES =
{
  workers: "Number of puma workers.",
  booted_workers: "Number of puma workers booted.",
  old_workers: "Number of old puma workers.",
  running_threads: "Number of puma threads currently running.",
  request_backlog: "Number of requests waiting to be processed by a puma thread.",
  thread_pool_capacity: "Number of puma threads available at current scale.",
  max_threads: "Number of puma threads at available at max scale.",
}

Instance Method Summary collapse

Constructor Details

#initializePumaCollector

Returns a new instance of PumaCollector.



16
17
18
19
20
21
# File 'lib/prometheus_exporter/server/puma_collector.rb', line 16

def initialize
  @puma_metrics = MetricsContainer.new(ttl: MAX_PUMA_METRIC_AGE)
  @puma_metrics.filter = ->(new_metric, old_metric) do
    new_metric["pid"] == old_metric["pid"] && new_metric["hostname"] == old_metric["hostname"]
  end
end

Instance Method Details

#collect(obj) ⇒ Object



50
51
52
# File 'lib/prometheus_exporter/server/puma_collector.rb', line 50

def collect(obj)
  @puma_metrics << obj
end

#metricsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/prometheus_exporter/server/puma_collector.rb', line 27

def metrics
  return [] if @puma_metrics.length == 0

  metrics = {}

  @puma_metrics.map do |m|
    labels = {}
    labels.merge!(phase: m["phase"]) if m["phase"]
    labels.merge!(m["custom_labels"]) if m["custom_labels"]
    labels.merge!(m["metric_labels"]) if m["metric_labels"]

    PUMA_GAUGES.map do |k, help|
      k = k.to_s
      if v = m[k]
        g = metrics[k] ||= PrometheusExporter::Metric::Gauge.new("puma_#{k}", help)
        g.observe(v, labels)
      end
    end
  end

  metrics.values
end

#typeObject



23
24
25
# File 'lib/prometheus_exporter/server/puma_collector.rb', line 23

def type
  "puma"
end