Sha256: e43a52812e351e1b3b836f383b78eebf25e1473189a5af274c791389193e84a2

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Server
  class PumaCollector < TypeCollector
    MAX_PUMA_METRIC_AGE = 30
    PUMA_GAUGES = {
      workers_total: "Number of puma workers.",
      booted_workers_total: "Number of puma workers booted.",
      old_workers_total: "Number of old puma workers.",
      running_threads_total: "Number of puma threads currently running.",
      request_backlog_total: "Number of requests waiting to be processed by a puma thread.",
      thread_pool_capacity_total: "Number of puma threads available at current scale.",
      max_threads_total: "Number of puma threads at available at max scale.",
    }

    def initialize
      @puma_metrics = []
    end

    def type
      "puma"
    end

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

      metrics = {}

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

        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

    def collect(obj)
      now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)

      obj["created_at"] = now

      @puma_metrics.delete_if do |current|
        (obj["pid"] == current["pid"] && obj["hostname"] == current["hostname"]) ||
          (current["created_at"] + MAX_PUMA_METRIC_AGE < now)
      end

      @puma_metrics << obj
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prometheus_exporter-0.8.1 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-0.8.0 lib/prometheus_exporter/server/puma_collector.rb