-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Jimdo/client_metrics
Client metrics
- Loading branch information
Showing
9 changed files
with
247 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
language: ruby | ||
rvm: | ||
- 2.0.0 | ||
- 2.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
source 'https://rubygems.org' | ||
gemspec | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ Gems: | |
Compatibility (tested): | ||
|
||
* Ruby 2.0.0 | ||
* Ruby 2.1.0 | ||
|
||
(if you can confirm another version of Ruby, email me at [email protected]) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require 'librato-sidekiq/middleware' | ||
require 'librato-sidekiq/client_middleware' | ||
|
||
Librato::Sidekiq::Middleware.configure | ||
Librato::Sidekiq::ClientMiddleware.configure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Librato | ||
module Sidekiq | ||
class ClientMiddleware < Middleware | ||
def reconfigure | ||
# puts "Reconfiguring with: #{options}" | ||
::Sidekiq.configure_client do |config| | ||
config.client_middleware do |chain| | ||
chain.remove self.class | ||
chain.add self.class, options | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def track(tracking_group, stats, worker_instance, msg, queue, elapsed) | ||
tracking_group.increment 'queued' | ||
return unless allowed_to_submit queue, worker_instance | ||
# puts "doing Librato insert" | ||
tracking_group.group queue.to_s do |q| | ||
q.increment 'queued' | ||
|
||
# using something like User.delay.send_email invokes | ||
# a class name with slashes. remove them in favor of underscores | ||
q.group msg['class'].underscore.gsub('/', '_') do |w| | ||
w.increment 'queued' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'librato-sidekiq/middleware' | ||
require 'librato-sidekiq/client_middleware' | ||
require 'timecop' | ||
|
||
# Fix time | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
require 'spec_helper' | ||
|
||
describe Librato::Sidekiq::ClientMiddleware do | ||
|
||
before(:each) do | ||
stub_const "Librato::Rails", Class.new | ||
stub_const "Sidekiq", Module.new | ||
stub_const "Sidekiq::Stats", Class.new | ||
end | ||
|
||
let(:middleware) do | ||
allow(Sidekiq).to receive(:configure_client) | ||
Librato::Sidekiq::ClientMiddleware.new | ||
end | ||
|
||
describe '#intialize' do | ||
it 'should call reconfigure' do | ||
expect(Sidekiq).to receive(:configure_client) | ||
Librato::Sidekiq::ClientMiddleware.new | ||
end | ||
end | ||
|
||
describe '#configure' do | ||
|
||
before(:each) { Sidekiq.should_receive(:configure_client) } | ||
|
||
it 'should yield with it self as argument' do | ||
expect { |b| Librato::Sidekiq::ClientMiddleware.configure &b }.to yield_with_args(Librato::Sidekiq::ClientMiddleware) | ||
end | ||
|
||
it 'should return a new instance' do | ||
expect(Librato::Sidekiq::ClientMiddleware.configure).to be_an_instance_of Librato::Sidekiq::ClientMiddleware | ||
end | ||
|
||
end | ||
|
||
describe '#reconfigure' do | ||
|
||
let(:chain) { double() } | ||
let(:config) { double() } | ||
|
||
it 'should add itself to the server middleware chain' do | ||
expect(chain).to receive(:remove).with Librato::Sidekiq::ClientMiddleware | ||
expect(chain).to receive(:add).with Librato::Sidekiq::ClientMiddleware, middleware.options | ||
|
||
expect(config).to receive(:client_middleware).once.and_yield(chain) | ||
expect(Sidekiq).to receive(:configure_client).once.and_yield(config) | ||
|
||
middleware.reconfigure | ||
end | ||
end | ||
|
||
describe '#call' do | ||
|
||
let(:meter) { double(measure: nil, increment: nil, group: nil) } | ||
|
||
let(:queue_name) { 'some_awesome_queue' } | ||
let(:some_worker_instance) { nil } | ||
let(:some_message) { Hash['class', double(underscore: queue_name)] } | ||
|
||
let(:sidekiq_stats_instance_double) do | ||
double("Sidekiq::Stats", :enqueued => 1, :failed => 2, :scheduled_size => 3) | ||
end | ||
|
||
context 'when middleware is not enabled' do | ||
|
||
before(:each) { middleware.enabled = false } | ||
|
||
it { expect { |b| middleware.call(1,2,3,&b) }.to yield_with_no_args } | ||
|
||
it 'should not send any metrics' do | ||
Librato.should_not_receive(:group) | ||
end | ||
|
||
end | ||
|
||
context 'when middleware is enabled but queue is blacklisted' do | ||
|
||
before(:each) do | ||
allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_instance_double) | ||
allow(Librato).to receive(:group).with('sidekiq').and_yield meter | ||
end | ||
|
||
before(:each) do | ||
middleware.enabled = true | ||
middleware.blacklist_queues = [] | ||
middleware.blacklist_queues << queue_name | ||
end | ||
|
||
it { expect { |b| middleware.call(some_worker_instance, some_message, queue_name, &b) }.to yield_with_no_args } | ||
|
||
it 'should measure increment queued metric' do | ||
expect(meter).to receive(:increment).with 'queued' | ||
middleware.call(some_worker_instance, some_message, queue_name) {} | ||
end | ||
|
||
end | ||
|
||
context 'when middleware is enabled and everything is whitlisted' do | ||
|
||
let(:sidekiq_group) { double(measure: nil, increment: nil, group: nil) } | ||
let(:queue_group) { double(measure: nil, increment: nil, timing: nil, group: nil) } | ||
let(:class_group) { double(measure: nil, increment: nil, timing: nil, group: nil) } | ||
|
||
before(:each) do | ||
middleware.enabled = true | ||
middleware.blacklist_queues = [] | ||
end | ||
|
||
before(:each) do | ||
allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_instance_double) | ||
allow(Librato).to receive(:group).with('sidekiq').and_yield(sidekiq_group) | ||
allow(sidekiq_stats_instance_double).to receive(:queues) | ||
end | ||
|
||
it 'should measure queue metrics' do | ||
expect(sidekiq_group).to receive(:group).and_yield(queue_group) | ||
|
||
expect(queue_group).to receive(:increment).with "queued" | ||
|
||
middleware.call(some_worker_instance, some_message, queue_name) {} | ||
end | ||
|
||
it 'should measure class metrics' do | ||
expect(sidekiq_group).to receive(:group).and_yield(queue_group) | ||
expect(queue_group).to receive(:group).with(queue_name).and_yield(class_group) | ||
|
||
expect(class_group).to receive(:increment).with "queued" | ||
|
||
middleware.call(some_worker_instance, some_message, queue_name) {} | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters