From 1d8e6b106dad10e12afc53be6536f73292fb32d2 Mon Sep 17 00:00:00 2001 From: martinramirez7 <89474234+martinramirez7@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:31:02 -0400 Subject: [PATCH] feat(delayed job): allow to select the modules as part of the jobs name (#314) --- .../instrumentation/delayed_job.rb | 11 +++++----- test/server/collector_test.rb | 20 +++++++++---------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/prometheus_exporter/instrumentation/delayed_job.rb b/lib/prometheus_exporter/instrumentation/delayed_job.rb index 119e5bb..254fb0d 100644 --- a/lib/prometheus_exporter/instrumentation/delayed_job.rb +++ b/lib/prometheus_exporter/instrumentation/delayed_job.rb @@ -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) @@ -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 @@ -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 @@ -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, diff --git a/test/server/collector_test.rb b/test/server/collector_test.rb index 45dc4f7..f46873f 100644 --- a/test/server/collector_test.rb +++ b/test/server/collector_test.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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