Sha256: 7f20c0aabfbb23ec8b224f9ab6c6614078d753dec81fbdc67be5cc6980378003

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Server
  class SidekiqProcessCollector < PrometheusExporter::Server::TypeCollector
    MAX_SIDEKIQ_METRIC_AGE = 60

    SIDEKIQ_PROCESS_GAUGES = {
      'busy' => 'Number of running jobs',
      'concurrency' => 'Maximum concurrency',
    }.freeze

    attr_reader :sidekiq_metrics, :gauges

    def initialize
      @sidekiq_metrics = MetricsContainer.new(ttl: MAX_SIDEKIQ_METRIC_AGE)
      @gauges = {}
    end

    def type
      'sidekiq_process'
    end

    def metrics
      SIDEKIQ_PROCESS_GAUGES.each_key { |name| gauges[name]&.reset! }

      sidekiq_metrics.map do |metric|
        labels = metric.fetch('labels', {})
        SIDEKIQ_PROCESS_GAUGES.map do |name, help|
          if (value = metric[name])
            gauge = gauges[name] ||= PrometheusExporter::Metric::Gauge.new("sidekiq_process_#{name}", help)
            gauge.observe(value, labels)
          end
        end
      end

      gauges.values
    end

    def collect(object)
      @sidekiq_metrics << object["process"]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prometheus_exporter-2.0.8 lib/prometheus_exporter/server/sidekiq_process_collector.rb
prometheus_exporter-2.0.7 lib/prometheus_exporter/server/sidekiq_process_collector.rb