|
3 | 3 | # collects stats from GoodJob |
4 | 4 | module PrometheusExporter::Instrumentation |
5 | 5 | 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) |
7 | 10 | good_job_collector = new |
8 | 11 | client ||= PrometheusExporter::Client.default |
9 | 12 |
|
10 | 13 | worker_loop do |
11 | | - client.send_json(good_job_collector.collect) |
| 14 | + client.send_json(good_job_collector.collect(collect_by_queue)) |
12 | 15 | end |
13 | 16 |
|
14 | 17 | super |
15 | 18 | end |
16 | 19 |
|
17 | | - def collect |
| 20 | + def collect(by_queue = false) |
| 21 | + count_method = by_queue ? COUNT_BY_QUEUE : COUNT_ALL |
18 | 22 | { |
19 | 23 | 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) |
27 | 32 | } |
28 | 33 | end |
29 | 34 | end |
|
0 commit comments