diff --git a/Gemfile.lock b/Gemfile.lock index ab29650..370163f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -34,7 +34,7 @@ GEM archive-tar-minitar (>= 0.5.2) sinatra (1.2.7) rack (~> 1.1) - tilt (< 2.0, >= 1.2.2) + tilt (>= 1.2.2, < 2.0) tilt (1.3.3) vegas (0.1.8) rack (>= 1.0.0) diff --git a/lib/resque/metrics.rb b/lib/resque/metrics.rb index a2f8080..88b52bb 100644 --- a/lib/resque/metrics.rb +++ b/lib/resque/metrics.rb @@ -82,12 +82,22 @@ def self.record_job_fork(job, time) run_callback(:on_job_fork, job_class, queue, time) end - def self.record_job_enqueue(job_class) + def self.record_job_enqueue(job_class, *args) queue = Resque.queue_from_class(job_class) increment_metric "enqueue_count" increment_metric "enqueue_count:job:#{job_class}" increment_metric "enqueue_count:queue:#{queue}" - run_callback(:on_job_enqueue, job_class, queue) + + size = Resque.encode(args).length + redis.multi do + increment_metric "payload_size", size + increment_metric "payload_size:queue:#{queue}", size + increment_metric "payload_size:job:#{job_class}", size + end + set_metric "avg_payload_size", total_payload_size / total_enqueue_count + set_metric "avg_payload_size:queue:#{queue}", total_payload_size_by_queue(queue) / total_enqueue_count_by_queue(queue) + set_metric "avg_payload_size:job:#{job_class}", total_payload_size_by_job(job_class) / total_enqueue_count_by_job(job_class) + run_callback(:on_job_enqueue, job_class, queue, size) true end @@ -167,6 +177,30 @@ def self.total_job_count_by_job(job) get_metric "job_count:job:#{job}" end + def self.total_payload_size + get_metric "payload_size" + end + + def self.total_payload_size_by_queue(queue) + get_metric "payload_size:queue:#{queue}" + end + + def self.total_payload_size_by_job(job) + get_metric "payload_size:job:#{job}" + end + + def self.avg_payload_size + get_metric "avg_payload_size" + end + + def self.avg_payload_size_by_queue(queue) + get_metric "avg_payload_size:queue:#{queue}" + end + + def self.avg_payload_size_by_job(job) + get_metric "avg_payload_size:job:#{job}" + end + def self.avg_fork_time get_metric "avg_fork_time" end @@ -206,7 +240,7 @@ def self.total_fork_count_by_job(job) module Hooks def after_enqueue_metrics(*args) - Resque::Metrics.record_job_enqueue(self) + Resque::Metrics.record_job_enqueue(self, *args) end def around_perform_metrics(*args) diff --git a/test/test_resque-metrics.rb b/test/test_resque-metrics.rb index 3bf99eb..1bcc699 100644 --- a/test/test_resque-metrics.rb +++ b/test/test_resque-metrics.rb @@ -41,6 +41,12 @@ def test_should_record_job_count assert Resque::Metrics.total_job_count_by_job(SomeJob) > 0 end + def test_should_record_payload_size + assert Resque::Metrics.total_payload_size > 0 + assert Resque::Metrics.total_payload_size_by_queue(:jobs) > 0 + assert Resque::Metrics.total_payload_size_by_job(SomeJob) > 0 + end + def test_should_record_avg_job_time assert Resque::Metrics.avg_job_time > 0 assert Resque::Metrics.avg_job_time_by_queue(:jobs) > 0