Sha256: 7a72172d7dfbca8d2b1977d3cc684c64723e5a9f3edffa34a2c0e312f596b39d

Contents?: true

Size: 1.23 KB

Versions: 5

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_total' => '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

5 entries across 5 versions & 1 rubygems

Version Path
prometheus_exporter-0.8.1 lib/prometheus_exporter/server/sidekiq_queue_collector.rb
prometheus_exporter-0.8.0 lib/prometheus_exporter/server/sidekiq_queue_collector.rb
prometheus_exporter-0.7.0 lib/prometheus_exporter/server/sidekiq_queue_collector.rb
prometheus_exporter-0.6.0 lib/prometheus_exporter/server/sidekiq_queue_collector.rb
prometheus_exporter-0.5.3 lib/prometheus_exporter/server/sidekiq_queue_collector.rb