Sha256: ed3521fbe5cccba4780acdd696b1333fc9604bc773d3ab70ee1e6bc597849706

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Server
  class PumaCollector < TypeCollector
    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.",
    }

    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

9 entries across 9 versions & 1 rubygems

Version Path
prometheus_exporter-2.0.6 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-2.0.5 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-2.0.4 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-2.0.3 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-2.0.2 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-2.0.1 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-2.0.0 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-1.0.1 lib/prometheus_exporter/server/puma_collector.rb
prometheus_exporter-1.0.0 lib/prometheus_exporter/server/puma_collector.rb