Skip to content

Commit 21f0737

Browse files
committed
collect_by_queue added as optional argument to GoodJob#Start
1 parent 9aec28f commit 21f0737

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lib/prometheus_exporter/instrumentation/good_job.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@
33
# collects stats from GoodJob
44
module PrometheusExporter::Instrumentation
55
class GoodJob < PeriodicStats
6-
def self.start(client: nil, frequency: 30)
6+
COUNT_BY_QUEUE = ->(collection) { collection.group(:queue_name).size }
7+
COUNT_ALL = ->(collection) { collection.size }
8+
9+
def self.start(client: nil, frequency: 30, collect_by_queue: false)
710
good_job_collector = new
811
client ||= PrometheusExporter::Client.default
912

1013
worker_loop do
11-
client.send_json(good_job_collector.collect)
14+
client.send_json(good_job_collector.collect(collect_by_queue))
1215
end
1316

1417
super
1518
end
1619

17-
def collect
20+
def collect(by_queue = false)
21+
count_method = by_queue ? COUNT_BY_QUEUE : COUNT_ALL
1822
{
1923
type: "good_job",
20-
scheduled: ::GoodJob::Job.scheduled.size,
21-
retried: ::GoodJob::Job.retried.size,
22-
queued: ::GoodJob::Job.queued.size,
23-
running: ::GoodJob::Job.running.size,
24-
finished: ::GoodJob::Job.finished.size,
25-
succeeded: ::GoodJob::Job.succeeded.size,
26-
discarded: ::GoodJob::Job.discarded.size
24+
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)
2732
}
2833
end
2934
end

lib/prometheus_exporter/server/good_job_collector.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ def metrics
3333

3434
if value
3535
gauge = gauges[name] ||= PrometheusExporter::Metric::Gauge.new("good_job_#{name}", help)
36-
gauge.observe(value, labels)
36+
37+
if metric["by_queue"]
38+
value.each do |queue_name, count|
39+
gauge.observe(count, labels.merge(queue_name: queue_name))
40+
end
41+
else
42+
gauge.observe(value, labels)
43+
end
3744
end
3845
end
3946
end

0 commit comments

Comments
 (0)