Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DelayedJob to be extended and JOB_CLASS_REGEXP redefined #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/prometheus_exporter/instrumentation/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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(self.class::JOB_CLASS_REGEXP).to_a[1].to_s,
queue_name: job.queue,
success: success,
duration: duration,
Expand Down
43 changes: 43 additions & 0 deletions test/server/collector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,49 @@ def test_it_can_collect_delayed_job_metrics_in_histogram_mode
job.verify
end

def test_it_can_be_subclassed_to_override_the_regex
# Create a new class that extends DelayedJob and redefines JOB_CLASS_REGEXP
class MyDelayedJob < PrometheusExporter::Instrumentation::DelayedJob
JOB_CLASS_REGEXP = %r{ruby/(?:object|struct):\s*(\w+)}.freeze
end

collector = PrometheusExporter::Server::Collector.new
client = PipedClient.new(collector, custom_labels: { service: 'service1' })

instrument = MyDelayedJob.new(client: client)

job = Minitest::Mock.new
job.expect(:handler, "---\n!ruby/struct:MyStruct\nendpoint_id:\n")
job.expect(:queue, "my_queue")
job.expect(:attempts, 0)

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

failed_job = Minitest::Mock.new
failed_job.expect(:handler, "---\n!ruby/struct:MyOtherStruct\nendpoint_id:\n")
failed_job.expect(:queue, "my_queue")
failed_job.expect(:attempts, 1)

begin
instrument.call(failed_job, 25, 10, 0, nil, "default") do
boom
end
rescue
end

result = collector.prometheus_metrics_text

assert(result.include?('delayed_failed_jobs_total{queue_name="my_queue",service="service1",job_name="MyOtherStruct"} 1'), "has failed job")
assert(result.include?('delayed_jobs_total{queue_name="my_queue",service="service1",job_name="MyStruct"} 1'), "has working job")
assert(result.include?('delayed_job_duration_seconds{queue_name="my_queue",service="service1",job_name="MyStruct"}'), "has duration")
assert(result.include?('delayed_jobs_enqueued{queue_name="my_queue",service="service1"} 10'), "has enqueued count")
assert(result.include?('delayed_jobs_pending{queue_name="my_queue",service="service1"} 0'), "has pending count")
job.verify
failed_job.verify
end

require 'minitest/stub_const'

def test_it_can_collect_puma_metrics
Expand Down