Skip to content

Commit b29f9fe

Browse files
omarshammasbenngarcia
authored andcommitted
fix bug, handle when queue is empty
When we do `group(:queue_name).size` it returns the count by queue => {"queue_a"=>3, "queue_d"=>1} The problem is when a queue is empty it will simply be excluded from the results instead of returning count of 0. So the result we want should be => {"queue_a"=>3, "queue_b"=>0, "queue_c"=>0, "queue_d"=>1} Without returning 0, the queue count in prometheus metrics will be the last non-zero value meaning we can't auto-scale down the workers. Bug Fix Refactor
1 parent 7ce7b1a commit b29f9fe

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

lib/prometheus_exporter/instrumentation/good_job.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,21 @@
33
# collects stats from GoodJob
44
module PrometheusExporter::Instrumentation
55
class GoodJob < PeriodicStats
6-
COUNT_BY_QUEUE = ->(collection) { collection.group(:queue_name).size }
7-
COUNT_ALL = ->(collection) { collection.size }
6+
TotalCounter = Struct.new do
7+
def count(relation)
8+
relation.size
9+
end
10+
end
11+
12+
QueueCounter = Struct.new(:queue_names) do
13+
def initialize(queue_names)
14+
@empty_queues = queue_names.to_h { |name| [name, 0] }
15+
end
16+
17+
def count(relation)
18+
@empty_queues.merge(relation.group(:queue_name).size)
19+
end
20+
end
821

922
def self.start(client: nil, frequency: 30, collect_by_queue: false)
1023
good_job_collector = new
@@ -18,17 +31,18 @@ def self.start(client: nil, frequency: 30, collect_by_queue: false)
1831
end
1932

2033
def collect(by_queue = false)
21-
count_method = by_queue ? COUNT_BY_QUEUE : COUNT_ALL
34+
counter = by_queue ? QueueCounter.new(::GoodJob::Job.distinct.pluck(:queue_name)) : TotalCounter.new
35+
2236
{
2337
type: "good_job",
2438
by_queue: by_queue,
25-
scheduled: ::GoodJob::Job.scheduled.yield_self(&count_method),
26-
retried: ::GoodJob::Job.retried.yield_self(&count_method),
27-
queued: ::GoodJob::Job.queued.yield_self(&count_method),
28-
running: ::GoodJob::Job.running.yield_self(&count_method),
29-
finished: ::GoodJob::Job.finished.yield_self(&count_method),
30-
succeeded: ::GoodJob::Job.succeeded.yield_self(&count_method),
31-
discarded: ::GoodJob::Job.discarded.yield_self(&count_method)
39+
scheduled: counter.count(::GoodJob::Job.scheduled),
40+
retried: counter.count(::GoodJob::Job.retried),
41+
queued: counter.count(::GoodJob::Job.queued),
42+
running: counter.count(::GoodJob::Job.running),
43+
finished: counter.count(::GoodJob::Job.finished),
44+
succeeded: counter.count(::GoodJob::Job.succeeded),
45+
discarded: counter.count(::GoodJob::Job.discarded)
3246
}
3347
end
3448
end

0 commit comments

Comments
 (0)