Class: PrometheusExporter::Server::SidekiqQueueCollector

Inherits:
TypeCollector
  • Object
show all
Defined in:
lib/prometheus_exporter/server/sidekiq_queue_collector.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSidekiqQueueCollector

Returns a new instance of SidekiqQueueCollector.



13
14
15
16
# File 'lib/prometheus_exporter/server/sidekiq_queue_collector.rb', line 13

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

Instance Attribute Details

#gaugesObject (readonly)

Returns the value of attribute gauges.



11
12
13
# File 'lib/prometheus_exporter/server/sidekiq_queue_collector.rb', line 11

def gauges
  @gauges
end

#sidekiq_metricsObject (readonly)

Returns the value of attribute sidekiq_metrics.



11
12
13
# File 'lib/prometheus_exporter/server/sidekiq_queue_collector.rb', line 11

def sidekiq_metrics
  @sidekiq_metrics
end

Instance Method Details

#collect(object) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/prometheus_exporter/server/sidekiq_queue_collector.rb', line 36

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

#metricsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/prometheus_exporter/server/sidekiq_queue_collector.rb', line 22

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

#typeObject



18
19
20
# File 'lib/prometheus_exporter/server/sidekiq_queue_collector.rb', line 18

def type
  'sidekiq_queue'
end