Sha256: ae1a9dfa55b51169e0310690cf2384643d233fc5f2e6845d57d138324d7aeece

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true
module PrometheusExporter::Server
  class SidekiqQueueCollector < TypeCollector
    MAX_SIDEKIQ_METRIC_AGE = 60

    SIDEKIQ_QUEUE_GAUGES = {
      'backlog' => 'Size of the sidekiq queue.',
      'latency_seconds' => 'Latency of the sidekiq queue.',
    }.freeze

    attr_reader :sidekiq_metrics, :gauges

    def initialize
      @sidekiq_metrics = []
      @gauges = {}
    end

    def type
      'sidekiq_queue'
    end

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

      gauges.values
    end

    def collect(object)
      now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
      object['queues'].each do |queue|
        queue["created_at"] = now
        queue["labels"].merge!(object['custom_labels']) if object['custom_labels']
        sidekiq_metrics.delete_if { |metric| metric['created_at'] + MAX_SIDEKIQ_METRIC_AGE < now }
        sidekiq_metrics << queue
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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