Skip to content

Commit

Permalink
Replace queue workers stats with a single method (#48)
Browse files Browse the repository at this point in the history
* Replace queue workers stats with a single method
* Adjust rubocop Metrics/AbcSize
* Update queue workers stats help description
* Update README
  • Loading branch information
Strech authored Dec 14, 2021
1 parent 4260c6b commit 0a0eda7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ Layout/MultilineOperationIndentation:
EnforcedStyle: indented
IndentationWidth: ~

Metrics/AbcSize:
Max: 25
CountRepeatedAttributes: false

Metrics/MethodLength:
Max: 15

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ _(starting Sidekiq `v3.3.1`)_
| sidekiq_queue_latency_seconds | gauge | The number of seconds between oldest job being pushed to the queue and current time (labels: `name`) |
| sidekiq_queue_max_processing_time_seconds | gauge | The number of seconds between oldest job of the queue being executed and current time (labels: `name`) |
| sidekiq_queue_enqueued_jobs | gauge | The number of enqueued jobs in the queue (labels: `name`) |
| sidekiq_queue_workers | gauge | The number of workers serving the queue (labels: `name`) |
| sidekiq_queue_processes | gauge | The number of processes serving the queue (labels: `name`) |
| sidekiq_queue_busy_workers | gauge | The number of workers performing the job for the queue (labels: `name`) |

<details>
<summary>Click to expand for all available contribs</summary>
Expand Down
42 changes: 19 additions & 23 deletions lib/sidekiq/prometheus/exporter/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Standard
TEMPLATE = ERB.new(File.read(File.expand_path('templates/standard.erb', __dir__)))

QueueStats = Struct.new(:name, :size, :latency)
QueueWorkersStats = Struct.new(:total_workers, :busy_workers, :processes)
WorkersStats = Struct.new(:total_workers, :by_queue)

def self.available?
true
Expand All @@ -18,9 +20,8 @@ def self.available?
def initialize
@overview_stats = Sidekiq::Stats.new
@queues_stats = queues_stats
@workers_stats = workers_stats
@max_processing_times = max_processing_times
@total_workers = total_workers
@workers_stats_by_queue = workers_stats_by_queue
end

def to_s
Expand All @@ -35,8 +36,24 @@ def queues_stats
end
end

def workers_stats
workers_stats = WorkersStats.new(0, {})

Sidekiq::ProcessSet.new.each_with_object(workers_stats) do |process, stats|
stats.total_workers += process['concurrency'].to_i

process['queues'].each do |queue|
stats.by_queue[queue] ||= QueueWorkersStats.new(0, 0, 0)
stats.by_queue[queue].processes += 1
stats.by_queue[queue].busy_workers += process['busy'].to_i
stats.by_queue[queue].total_workers += process['concurrency'].to_i
end
end
end

def max_processing_times
now = Time.now.to_i

Sidekiq::Workers.new
.map { |_, _, execution| execution }
.group_by { |execution| execution['queue'] }
Expand All @@ -45,27 +62,6 @@ def max_processing_times
memo[queue] = now - oldest_execution['run_at']
end
end

def total_workers
sidekiq_processes.sum { |process| process['concurrency'].to_i }
end

def workers_stats_by_queue
sidekiq_processes.each_with_object({}) do |process, by_queue|
process['queues'].each do |queue|
by_queue[queue] ||= { 'workers' => 0, 'processes' => 0, 'busy_workers' => 0 }
by_queue[queue]['workers'] += process['concurrency']
by_queue[queue]['busy_workers'] += process['busy']
by_queue[queue]['processes'] += 1
end

by_queue
end
end

def sidekiq_processes
@sidekiq_processes ||= Sidekiq::ProcessSet.new
end
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/sidekiq/prometheus/exporter/templates/standard.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sidekiq_failed_jobs_total <%= format('%d', @overview_stats.failed) %>

# HELP sidekiq_workers The number of workers across all the processes.
# TYPE sidekiq_workers gauge
sidekiq_workers <%= format('%d', @total_workers) %>
sidekiq_workers <%= format('%d', @workers_stats.total_workers) %>

# HELP sidekiq_processes The number of processes.
# TYPE sidekiq_processes gauge
Expand Down Expand Up @@ -46,15 +46,15 @@ sidekiq_dead_jobs <%= format('%d', @overview_stats.dead_size) %>
# TYPE sidekiq_queue_max_processing_time_seconds gauge
<% @max_processing_times.each do |queue, max_processing_time| %>sidekiq_queue_max_processing_time_seconds{name="<%= queue %>"} <%= format('%i', max_processing_time) %>
<% end %>
# HELP sidekiq_queue_workers The number of workers per queue.
# HELP sidekiq_queue_workers The number of workers serving the queue.
# TYPE sidekiq_queue_workers gauge
<% @workers_stats_by_queue.each do |queue, workers_stats| %>sidekiq_queue_workers{name="<%= queue %>"} <%= format('%i', workers_stats['workers']) %>
<% @workers_stats.by_queue.each do |queue, stats| %>sidekiq_queue_workers{name="<%= queue %>"} <%= format('%i', stats.total_workers) %>
<% end %>
# HELP sidekiq_queue_processes The number of processes per queue.
# HELP sidekiq_queue_processes The number of processes serving the queue.
# TYPE sidekiq_queue_processes gauge
<% @workers_stats_by_queue.each do |queue, workers_stats| %>sidekiq_queue_processes{name="<%= queue %>"} <%= format('%i', workers_stats['processes']) %>
<% @workers_stats.by_queue.each do |queue, stats| %>sidekiq_queue_processes{name="<%= queue %>"} <%= format('%i', stats.processes) %>
<% end %>
# HELP sidekiq_queue_busy_workers The number of busy_workers per queue.
# HELP sidekiq_queue_busy_workers The number of workers performing the job for the queue.
# TYPE sidekiq_queue_busy_workers gauge
<% @workers_stats_by_queue.each do |queue, workers_stats| %>sidekiq_queue_busy_workers{name="<%= queue %>"} <%= format('%i', workers_stats['busy_workers']) %>
<% @workers_stats.by_queue.each do |queue, stats| %>sidekiq_queue_busy_workers{name="<%= queue %>"} <%= format('%i', stats.busy_workers) %>
<% end %>
6 changes: 3 additions & 3 deletions spec/sidekiq/prometheus/exporter/standard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,17 @@
sidekiq_queue_max_processing_time_seconds{name="default"} 20
sidekiq_queue_max_processing_time_seconds{name="additional"} 40
# HELP sidekiq_queue_workers The number of workers per queue.
# HELP sidekiq_queue_workers The number of workers serving the queue.
# TYPE sidekiq_queue_workers gauge
sidekiq_queue_workers{name="default"} 96
sidekiq_queue_workers{name="additional"} 42
# HELP sidekiq_queue_processes The number of processes per queue.
# HELP sidekiq_queue_processes The number of processes serving the queue.
# TYPE sidekiq_queue_processes gauge
sidekiq_queue_processes{name="default"} 3
sidekiq_queue_processes{name="additional"} 2
# HELP sidekiq_queue_busy_workers The number of busy_workers per queue.
# HELP sidekiq_queue_busy_workers The number of workers performing the job for the queue.
# TYPE sidekiq_queue_busy_workers gauge
sidekiq_queue_busy_workers{name="default"} 16
sidekiq_queue_busy_workers{name="additional"} 10
Expand Down

0 comments on commit 0a0eda7

Please sign in to comment.