Skip to content

Commit 7ce7b1a

Browse files
committed
collect_by_queue added as optional argument to GoodJob#Start
don't forget the documentation! RuntimeMetric unused struct removed GoodJobCollector refactor empty? changed to length.zero? to respect expiry metrics_container changes reverted since I didn't use any enumerable methods post refactor
1 parent 5886421 commit 7ce7b1a

File tree

5 files changed

+45
-30
lines changed

5 files changed

+45
-30
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@ installation, you'll need to start the instrumentation:
654654
# e.g. config/initializers/good_job.rb
655655
require 'prometheus_exporter/instrumentation'
656656
PrometheusExporter::Instrumentation::GoodJob.start
657+
658+
# or, to collect metrics labelled by their queue name
659+
PrometheusExporter::Instrumentation::GoodJob.start(collect_by_queue: true)
657660
```
658661

659662
#### Metrics collected by GoodJob Instrumentation

lib/prometheus_exporter/instrumentation/delayed_job.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ module PrometheusExporter::Instrumentation
44
class DelayedJob
55
JOB_CLASS_REGEXP = /job_class: ((\w+:{0,2})+)/.freeze
66

7-
RuntimeMetric = Struct.new(:max_attempts, :enqueued_count, :pending_count, :ready_count)
8-
97
class << self
108
def register_plugin(client: nil, include_module_name: false)
119
instrumenter = self.new(client: client)

lib/prometheus_exporter/instrumentation/good_job.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +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

10-
worker_loop { client.send_json(good_job_collector.collect) }
13+
worker_loop do
14+
client.send_json(good_job_collector.collect(collect_by_queue))
15+
end
1116

1217
super
1318
end
1419

15-
def collect
20+
def collect(by_queue = false)
21+
count_method = by_queue ? COUNT_BY_QUEUE : COUNT_ALL
1622
{
1723
type: "good_job",
18-
scheduled: ::GoodJob::Job.scheduled.size,
19-
retried: ::GoodJob::Job.retried.size,
20-
queued: ::GoodJob::Job.queued.size,
21-
running: ::GoodJob::Job.running.size,
22-
finished: ::GoodJob::Job.finished.size,
23-
succeeded: ::GoodJob::Job.succeeded.size,
24-
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)
2532
}
2633
end
2734
end

lib/prometheus_exporter/server/good_job_collector.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,9 @@ def type
2323
end
2424

2525
def metrics
26-
return [] if good_job_metrics.length == 0
27-
28-
good_job_metrics.map do |metric|
29-
labels = metric.fetch("custom_labels", {})
30-
31-
GOOD_JOB_GAUGES.map do |name, help|
32-
value = metric[name.to_s]
33-
34-
if value
35-
gauge = gauges[name] ||= PrometheusExporter::Metric::Gauge.new("good_job_#{name}", help)
36-
gauge.observe(value, labels)
37-
end
38-
end
39-
end
26+
return [] if good_job_metrics.length.zero?
4027

28+
good_job_metrics.each(&method(:process_metric))
4129
gauges.values
4230
end
4331

@@ -48,5 +36,24 @@ def collect(object)
4836
private
4937

5038
attr_reader :good_job_metrics, :gauges
39+
40+
def process_metric(metric)
41+
labels = metric.fetch("custom_labels", {})
42+
43+
GOOD_JOB_GAUGES.each do |name, help|
44+
next unless (value = metric[name.to_s])
45+
46+
gauge = gauges[name] ||= PrometheusExporter::Metric::Gauge.new("good_job_#{name}", help)
47+
observe_metric(gauge, metric, labels, value)
48+
end
49+
end
50+
51+
def observe_metric(gauge, metric, labels, value)
52+
if metric["by_queue"]
53+
value.each { |queue_name, count| gauge.observe(count, labels.merge(queue_name: queue_name)) }
54+
else
55+
gauge.observe(value, labels)
56+
end
57+
end
5158
end
5259
end

lib/prometheus_exporter/server/metrics_container.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ def size(&blk)
3434
end
3535
alias_method :length, :size
3636

37-
def map(&blk)
38-
wrap_expire(:map, &blk)
39-
end
40-
4137
def each(&blk)
4238
wrap_expire(:each, &blk)
4339
end
4440

41+
def map(&blk)
42+
wrap_expire(:map, &blk)
43+
end
44+
4545
def expire(time: nil, new_metric: nil)
4646
time ||= get_time
4747

0 commit comments

Comments
 (0)