Skip to content

Commit

Permalink
Record payload size
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed Nov 11, 2011
1 parent f61c7f9 commit 7246ee5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 37 additions & 3 deletions lib/resque/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/test_resque-metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7246ee5

Please sign in to comment.