Skip to content

Commit

Permalink
feat(delayed job): allow to select the modules as part of the jobs name
Browse files Browse the repository at this point in the history
  • Loading branch information
martinramirez7 committed Apr 9, 2024
1 parent d172482 commit 0e59eb4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
11 changes: 6 additions & 5 deletions lib/prometheus_exporter/instrumentation/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module PrometheusExporter::Instrumentation
class DelayedJob
JOB_CLASS_REGEXP = %r{job_class: (\w+:{0,2})+}.freeze
JOB_CLASS_REGEXP = %r{job_class: ((\w+:{0,2})+)}.freeze

class << self
def register_plugin(client: nil)
def register_plugin(client: nil, include_module_name: false)
instrumenter = self.new(client: client)
return unless defined?(Delayed::Plugin)

Expand All @@ -15,7 +15,8 @@ def register_plugin(client: nil)
max_attempts = Delayed::Worker.max_attempts
enqueued_count = Delayed::Job.where(queue: job.queue).count
pending_count = Delayed::Job.where(attempts: 0, locked_at: nil, queue: job.queue).count
instrumenter.call(job, max_attempts, enqueued_count, pending_count, *args, &block)
instrumenter.call(job, max_attempts, enqueued_count, pending_count, include_module_name,
*args, &block)
end
end
end
Expand All @@ -28,7 +29,7 @@ def initialize(client: nil)
@client = client || PrometheusExporter::Client.default
end

def call(job, max_attempts, enqueued_count, pending_count, *args, &block)
def call(job, max_attempts, enqueued_count, pending_count, include_module_name, *args, &block)
success = false
start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
latency = Time.current - job.run_at
Expand All @@ -41,7 +42,7 @@ def call(job, max_attempts, enqueued_count, pending_count, *args, &block)

@client.send_json(
type: "delayed_job",
name: job.handler.to_s.match(JOB_CLASS_REGEXP).to_a[1].to_s,
name: job.handler.to_s.match(JOB_CLASS_REGEXP).to_a[include_module_name ? 1 : 2].to_s,
queue_name: job.queue,
success: success,
duration: duration,
Expand Down
20 changes: 10 additions & 10 deletions test/server/collector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -741,25 +741,25 @@ def test_it_can_collect_delayed_job_metrics

current_time = Time.current
job = Minitest::Mock.new
job.expect(:handler, "job_class: Class")
job.expect(:handler, "job_class: SomeModule::Class")
job.expect(:queue, "my_queue")
job.expect(:attempts, 0)
job.expect(:run_at, current_time - 10.seconds)

Time.stub(:current, current_time) do
instrument.call(job, 20, 10, 0, nil, "default") do
instrument.call(job, 20, 10, 0, true, nil, "default") do
# nothing
end
end

failed_job = Minitest::Mock.new
failed_job.expect(:handler, "job_class: Object")
failed_job.expect(:handler, "job_class: Module::Object")
failed_job.expect(:queue, "my_queue")
failed_job.expect(:attempts, 1)
failed_job.expect(:run_at, 30.seconds.ago)

begin
instrument.call(failed_job, 25, 10, 0, nil, "default") do
instrument.call(failed_job, 25, 10, 0, false, nil, "default") do
boom
end
rescue
Expand All @@ -768,9 +768,9 @@ def test_it_can_collect_delayed_job_metrics
result = collector.prometheus_metrics_text

assert(result.include?("delayed_failed_jobs_total{queue_name=\"my_queue\",job_name=\"Object\"} 1"), "has failed job")
assert(result.include?("delayed_jobs_total{queue_name=\"my_queue\",job_name=\"Class\"} 1"), "has working job")
assert(result.include?("delayed_job_duration_seconds{queue_name=\"my_queue\",job_name=\"Class\"}"), "has duration")
assert(result.include?("delayed_job_latency_seconds_total{queue_name=\"my_queue\",job_name=\"Class\"}"), "has latency")
assert(result.include?("delayed_jobs_total{queue_name=\"my_queue\",job_name=\"SomeModule::Class\"} 1"), "has working job")
assert(result.include?("delayed_job_duration_seconds{queue_name=\"my_queue\",job_name=\"SomeModule::Class\"}"), "has duration")
assert(result.include?("delayed_job_latency_seconds_total{queue_name=\"my_queue\",job_name=\"SomeModule::Class\"}"), "has latency")
assert(result.include?("delayed_jobs_enqueued{queue_name=\"my_queue\"} 10"), "has enqueued count")
assert(result.include?("delayed_jobs_pending{queue_name=\"my_queue\"} 0"), "has pending count")
job.verify
Expand All @@ -791,7 +791,7 @@ def test_it_can_collect_delayed_job_metrics_with_custom_labels
job.expect(:run_at, current_time - 10.seconds)

Time.stub(:current, current_time) do
instrument.call(job, 25, 10, 0, nil, "default") do
instrument.call(job, 25, 10, 0, false, nil, "default") do
# nothing
end
end
Expand All @@ -802,7 +802,7 @@ def test_it_can_collect_delayed_job_metrics_with_custom_labels
failed_job.expect(:run_at, 30.seconds.ago)

begin
instrument.call(failed_job, 25, 10, 0, nil, "default") do
instrument.call(failed_job, 25, 10, 0, false, nil, "default") do
boom
end
rescue
Expand Down Expand Up @@ -833,7 +833,7 @@ def test_it_can_collect_delayed_job_metrics_in_histogram_mode
job.expect(:attempts, 0)
job.expect(:run_at, 10.seconds.ago)

instrument.call(job, 20, 10, 0, nil, "default") do
instrument.call(job, 20, 10, 0, false, nil, "default") do
# nothing
end

Expand Down

0 comments on commit 0e59eb4

Please sign in to comment.