Class: PrometheusExporter::Server::SidekiqProcessCollector

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSidekiqProcessCollector

Returns a new instance of SidekiqProcessCollector.



14
15
16
17
# File 'lib/prometheus_exporter/server/sidekiq_process_collector.rb', line 14

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

Instance Attribute Details

#gaugesObject (readonly)

Returns the value of attribute gauges.



12
13
14
# File 'lib/prometheus_exporter/server/sidekiq_process_collector.rb', line 12

def gauges
  @gauges
end

#sidekiq_metricsObject (readonly)

Returns the value of attribute sidekiq_metrics.



12
13
14
# File 'lib/prometheus_exporter/server/sidekiq_process_collector.rb', line 12

def sidekiq_metrics
  @sidekiq_metrics
end

Instance Method Details

#collect(object) ⇒ Object



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

def collect(object)
  now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  process = object['process']

  process["created_at"] = now
  sidekiq_metrics.delete_if { |metric| metric['created_at'] + MAX_SIDEKIQ_METRIC_AGE < now }
  sidekiq_metrics << process
end

#metricsObject



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

def metrics
  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)
        gauges[name].observe(value, labels)
      end
    end
  end

  gauges.values
end

#typeObject



19
20
21
# File 'lib/prometheus_exporter/server/sidekiq_process_collector.rb', line 19

def type
  'sidekiq_process'
end