thibaudgg-librato-sidekiq 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/History.md +12 -0
- data/LICENSE +20 -0
- data/README.md +87 -0
- data/lib/librato-sidekiq.rb +5 -0
- data/lib/librato-sidekiq/client_middleware.rb +32 -0
- data/lib/librato-sidekiq/middleware.rb +130 -0
- data/lib/librato-sidekiq/version.rb +5 -0
- data/librato-sidekiq.gemspec +30 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/unit/client_middleware_spec.rb +145 -0
- data/spec/unit/middleware_spec.rb +172 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 253bccc1acbb18d11798d56ba441366c00f6b6a5
|
4
|
+
data.tar.gz: 3df964a20c121dad04d61cf9317ff3639e71c8ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08c2ef029e6cc7afee6497410e817777ab4bf3d5a26b8ac59d144388ff7c49c7b777230987b224beeb7958f918cd70a1144fc61ab0f18fad7cd11a21528c3f61
|
7
|
+
data.tar.gz: 0e9152b5aece12b4da522fd6634a7c533ac2c74257ce0b883db8eaca244523159d36b4c49565a30de35710239832e02b6373360fe85e746c5366f77b9087709f
|
data/Gemfile
ADDED
data/History.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
librato-sidekiq changelog
|
2
|
+
=====================
|
3
|
+
|
4
|
+
HEAD
|
5
|
+
=======
|
6
|
+
- Drop librato-rails dependency since librato-rack is API compliant. this is now an implicit dependency and not managed by gemspec
|
7
|
+
|
8
|
+
0.1.0
|
9
|
+
=======
|
10
|
+
- Initial commit
|
11
|
+
- Each completed job measures current stats, timing, and increments processed for queue and worker name
|
12
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Scott Klein <scott@statuspage.io>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
librato-sidekiq
|
2
|
+
=====
|
3
|
+
|
4
|
+
librato-sidekiq is a simple gem to stick Sidekiq stats and granular processing counts/times into [Librato Metrics](http://metrics.librato.com/)
|
5
|
+
|
6
|
+
|
7
|
+
Requirements and Compatibility
|
8
|
+
------------
|
9
|
+
|
10
|
+
Gems:
|
11
|
+
|
12
|
+
* sidekiq
|
13
|
+
* librato-rails OR librato-rack
|
14
|
+
|
15
|
+
Compatibility (tested):
|
16
|
+
|
17
|
+
* Ruby 2.0.0
|
18
|
+
* Ruby 2.1.0
|
19
|
+
|
20
|
+
(if you can confirm another version of Ruby, email me at scott@statuspage.io)
|
21
|
+
|
22
|
+
|
23
|
+
Usage with Rails 3.x
|
24
|
+
---------------------------
|
25
|
+
|
26
|
+
In your Gemfile:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'librato-sidekiq'
|
30
|
+
```
|
31
|
+
|
32
|
+
In `config/environments/librato_sidekiq.rb`:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# only needed for fine-tuning, gem will enable all metrics by default
|
36
|
+
Librato::Sidekiq::Middleware.configure do |c|
|
37
|
+
# only enable for production
|
38
|
+
c.enabled = Rails.env.production?
|
39
|
+
|
40
|
+
# only allow these 3 queues
|
41
|
+
c.whitelist_queues = %w(default cron notifications)
|
42
|
+
|
43
|
+
# ignore these worker classes
|
44
|
+
c.blacklist_classes = %w(CronSchedulerWorker NotificationCheckerWorker)
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
|
49
|
+
Configuration
|
50
|
+
------------------------
|
51
|
+
Librato::Sidekiq accepts the following options.
|
52
|
+
|
53
|
+
**enabled**: Boolean, true by default
|
54
|
+
|
55
|
+
**whitelist_queues**: Array, list of queue names that will be the only ones sent to Librato (optional)
|
56
|
+
|
57
|
+
**blacklist_queues**: Array, list of queue names that will not be sent to Librato (optional)
|
58
|
+
|
59
|
+
**whitelist_classes**: Array, list of worker classes that will be the only ones sent to Librato (optional)
|
60
|
+
|
61
|
+
**blacklist_classes**: Array, list of worker classes that will not be sent to Librato (optional)
|
62
|
+
|
63
|
+
|
64
|
+
Contributing
|
65
|
+
-------------
|
66
|
+
|
67
|
+
If you have a fix you wish to provide, please fork the code, fix in your local project and then send a pull request on github. Please ensure that you include a test which verifies your fix and update History.md with a one sentence description of your fix so you get credit as a contributor.
|
68
|
+
|
69
|
+
|
70
|
+
Thanks
|
71
|
+
------------
|
72
|
+
|
73
|
+
Mike Perham - for creating [Sidekiq](http://github.com/mperham/sidekiq), a fantastic contribution to the ruby world
|
74
|
+
|
75
|
+
Librato - for a great [metrics service](http://metrics.librato.com)
|
76
|
+
|
77
|
+
|
78
|
+
Author
|
79
|
+
----------
|
80
|
+
|
81
|
+
Scott Klein, scott@statuspage.io, [statuspage.io](https://www.statuspage.io), If you like and use this project, please check out the [StatusPage.io service](https://www.statuspage.io/tour) for your project or company
|
82
|
+
|
83
|
+
|
84
|
+
Copyright
|
85
|
+
-----------
|
86
|
+
|
87
|
+
Copyright (c) 2013 Scott Klein. See LICENSE for details.
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Librato
|
2
|
+
module Sidekiq
|
3
|
+
class ClientMiddleware < Middleware
|
4
|
+
def self.reconfigure
|
5
|
+
# puts "Reconfiguring with: #{options}"
|
6
|
+
::Sidekiq.configure_client do |config|
|
7
|
+
config.client_middleware do |chain|
|
8
|
+
chain.remove self
|
9
|
+
chain.add self, options
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def track(tracking_group, stats, worker_instance, msg, queue, elapsed)
|
17
|
+
tracking_group.increment 'queued'
|
18
|
+
return unless allowed_to_submit queue, worker_instance
|
19
|
+
# puts "doing Librato insert"
|
20
|
+
tracking_group.group queue.to_s do |q|
|
21
|
+
q.increment 'queued'
|
22
|
+
|
23
|
+
# using something like User.delay.send_email invokes
|
24
|
+
# a class name with slashes. remove them in favor of underscores
|
25
|
+
q.group msg['class'].underscore.gsub('/', '_') do |w|
|
26
|
+
w.increment 'queued'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
2
|
+
|
3
|
+
module Librato
|
4
|
+
module Sidekiq
|
5
|
+
class Middleware
|
6
|
+
cattr_accessor :enabled do
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
cattr_accessor :whitelist_queues, :blacklist_queues, :whitelist_classes, :blacklist_classes do
|
11
|
+
[]
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(options = {})
|
15
|
+
# hard dependency on one or the other being present
|
16
|
+
rails = !!defined?(Librato::Rails)
|
17
|
+
rack = !!defined?(Librato::Rack)
|
18
|
+
fail 'librato-sidekiq depends on having one of librato-rails or librato-rack installed' unless rails || rack
|
19
|
+
|
20
|
+
# librato-rails >= 0.10 changes behavior of reporting agent
|
21
|
+
if File.basename($PROGRAM_NAME) == 'sidekiq' && rails && Librato::Rails::VERSION.split('.')[1].to_i >= 10 && ENV['LIBRATO_AUTORUN'].nil?
|
22
|
+
puts 'NOTICE: --------------------------------------------------------------------'
|
23
|
+
puts 'NOTICE: THE REPORTING AGENT HAS NOT STARTED, AND NO METRICS WILL BE SENT'
|
24
|
+
puts 'NOTICE: librato-rails >= 0.10 requires LIBRATO_AUTORUN=1 in your environment'
|
25
|
+
puts 'NOTICE: --------------------------------------------------------------------'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.configure
|
30
|
+
yield(self) if block_given?
|
31
|
+
reconfigure
|
32
|
+
new
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.reconfigure
|
36
|
+
# puts "Reconfiguring with: #{options}"
|
37
|
+
::Sidekiq.configure_server do |config|
|
38
|
+
config.client_middleware do |chain|
|
39
|
+
chain.remove ClientMiddleware
|
40
|
+
chain.add ClientMiddleware, options
|
41
|
+
end
|
42
|
+
config.server_middleware do |chain|
|
43
|
+
chain.remove self
|
44
|
+
chain.add self, options
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.options
|
50
|
+
{
|
51
|
+
enabled: enabled,
|
52
|
+
whitelist_queues: whitelist_queues,
|
53
|
+
blacklist_queues: blacklist_queues,
|
54
|
+
whitelist_classes: whitelist_classes,
|
55
|
+
blacklist_classes: blacklist_classes
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
# redis_pool is needed for the sidekiq 3 upgrade
|
60
|
+
# https://github.com/mperham/sidekiq/blob/master/3.0-Upgrade.md
|
61
|
+
def call(worker_instance, msg, queue, redis_pool = nil)
|
62
|
+
start_time = Time.now
|
63
|
+
result = yield
|
64
|
+
elapsed = (Time.now - start_time).to_f
|
65
|
+
|
66
|
+
return result unless enabled
|
67
|
+
# puts "#{worker_instance} #{queue}"
|
68
|
+
|
69
|
+
stats = ::Sidekiq::Stats.new
|
70
|
+
|
71
|
+
Librato.group 'sidekiq' do |sidekiq|
|
72
|
+
track sidekiq, stats, worker_instance, msg, queue, elapsed
|
73
|
+
end
|
74
|
+
|
75
|
+
result
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def track(tracking_group, stats, worker_instance, msg, queue, elapsed)
|
81
|
+
submit_general_stats tracking_group, stats
|
82
|
+
return unless allowed_to_submit queue, worker_instance
|
83
|
+
# puts "doing Librato insert"
|
84
|
+
tracking_group.group queue.to_s do |q|
|
85
|
+
q.increment 'processed'
|
86
|
+
q.timing 'time', elapsed
|
87
|
+
q.measure 'enqueued', stats.queues[queue].to_i
|
88
|
+
|
89
|
+
# using something like User.delay.send_email invokes
|
90
|
+
# a class name with slashes. remove them in favor of underscores
|
91
|
+
q.group msg['class'].underscore.gsub('/', '_') do |w|
|
92
|
+
w.increment 'processed'
|
93
|
+
w.timing 'time', elapsed
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def submit_general_stats(group, stats)
|
99
|
+
group.increment 'processed'
|
100
|
+
{
|
101
|
+
enqueued: nil,
|
102
|
+
failed: nil,
|
103
|
+
scheduled_size: 'scheduled'
|
104
|
+
}.each do |method, name|
|
105
|
+
group.measure((name || method).to_s, stats.send(method).to_i)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def queue_in_whitelist(queue)
|
110
|
+
whitelist_queues.nil? || whitelist_queues.empty? || whitelist_queues.include?(queue.to_s)
|
111
|
+
end
|
112
|
+
|
113
|
+
def queue_in_blacklist(queue)
|
114
|
+
blacklist_queues.include?(queue.to_s)
|
115
|
+
end
|
116
|
+
|
117
|
+
def class_in_whitelist(worker_instance)
|
118
|
+
whitelist_classes.nil? || whitelist_classes.empty? || whitelist_classes.include?(worker_instance.class.to_s)
|
119
|
+
end
|
120
|
+
|
121
|
+
def class_in_blacklist(worker_instance)
|
122
|
+
blacklist_classes.include?(worker_instance.class.to_s)
|
123
|
+
end
|
124
|
+
|
125
|
+
def allowed_to_submit(queue, worker_instance)
|
126
|
+
class_in_whitelist(worker_instance) && !class_in_blacklist(worker_instance) && queue_in_whitelist(queue) && !queue_in_blacklist(queue)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require './lib/librato-sidekiq/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{thibaudgg-librato-sidekiq}
|
5
|
+
s.version = Librato::Sidekiq::VERSION
|
6
|
+
s.license = "MIT"
|
7
|
+
|
8
|
+
s.authors = ["Scott Klein", "Ole Michaelis"]
|
9
|
+
s.description = %q{Sidekiq hooks to push stats into Librato}
|
10
|
+
s.email = %q{scott@statuspage.io}
|
11
|
+
s.files = Dir.glob("lib/**/*") + [
|
12
|
+
"LICENSE",
|
13
|
+
"README.md",
|
14
|
+
"History.md",
|
15
|
+
"Gemfile",
|
16
|
+
"librato-sidekiq.gemspec",
|
17
|
+
]
|
18
|
+
s.homepage = %q{http://github.com/thibaudgg/librato-sidekiq}
|
19
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.summary = %q{Sidekiq hooks to push stats into Librato}
|
22
|
+
s.test_files = Dir.glob("spec/**/*")
|
23
|
+
|
24
|
+
s.add_dependency(%q<sidekiq>, [">= 0"])
|
25
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
26
|
+
|
27
|
+
s.add_development_dependency(%q<rspec>)
|
28
|
+
s.add_development_dependency(%q<timecop>)
|
29
|
+
s.add_development_dependency(%q<rake>)
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'librato-sidekiq/middleware'
|
2
|
+
require 'librato-sidekiq/client_middleware'
|
3
|
+
require 'timecop'
|
4
|
+
|
5
|
+
# Fix time
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.before(:suite) do
|
8
|
+
Timecop.freeze(Date.today + 30)
|
9
|
+
end
|
10
|
+
config.after(:suite) do
|
11
|
+
Timecop.return
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Librato::Sidekiq::ClientMiddleware do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
stub_const "Librato::Rails", Class.new
|
7
|
+
stub_const "Sidekiq", Module.new
|
8
|
+
stub_const "Sidekiq::Stats", Class.new
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:middleware) do
|
12
|
+
allow(Sidekiq).to receive(:configure_client)
|
13
|
+
Librato::Sidekiq::ClientMiddleware.new
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#initialize' do
|
17
|
+
it 'should not call reconfigure' do
|
18
|
+
expect(Sidekiq).not_to receive(:configure_client)
|
19
|
+
Librato::Sidekiq::ClientMiddleware.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#configure' do
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
allow(described_class).to receive(:reconfigure)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should yield with it self as argument' do
|
30
|
+
expect { |b| Librato::Sidekiq::ClientMiddleware.configure &b }.to yield_with_args(Librato::Sidekiq::ClientMiddleware)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should call reconfigure' do
|
34
|
+
expect(described_class).to receive(:reconfigure)
|
35
|
+
described_class.configure
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should return a new instance' do
|
39
|
+
expect(Librato::Sidekiq::ClientMiddleware.configure).to be_an_instance_of Librato::Sidekiq::ClientMiddleware
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '.reconfigure' do
|
45
|
+
|
46
|
+
let(:chain) { double() }
|
47
|
+
let(:config) { double() }
|
48
|
+
|
49
|
+
it 'should add itself to the server middleware chain' do
|
50
|
+
expect(chain).to receive(:remove).with Librato::Sidekiq::ClientMiddleware
|
51
|
+
expect(chain).to receive(:add).with Librato::Sidekiq::ClientMiddleware,
|
52
|
+
described_class.options
|
53
|
+
|
54
|
+
expect(config).to receive(:client_middleware).once.and_yield(chain)
|
55
|
+
expect(Sidekiq).to receive(:configure_client).once.and_yield(config)
|
56
|
+
|
57
|
+
described_class.reconfigure
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#call' do
|
62
|
+
|
63
|
+
let(:meter) { double(measure: nil, increment: nil, group: nil) }
|
64
|
+
|
65
|
+
let(:queue_name) { 'some_awesome_queue' }
|
66
|
+
let(:some_worker_instance) { nil }
|
67
|
+
let(:some_message) { Hash['class', double(underscore: queue_name)] }
|
68
|
+
|
69
|
+
let(:sidekiq_stats_instance_double) do
|
70
|
+
double("Sidekiq::Stats", :enqueued => 1, :failed => 2, :scheduled_size => 3)
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when middleware is not enabled' do
|
74
|
+
|
75
|
+
before(:each) { middleware.enabled = false }
|
76
|
+
|
77
|
+
it { expect { |b| middleware.call(1,2,3,&b) }.to yield_with_no_args }
|
78
|
+
|
79
|
+
it 'should not send any metrics' do
|
80
|
+
Librato.should_not_receive(:group)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'when middleware is enabled but queue is blacklisted' do
|
86
|
+
|
87
|
+
before(:each) do
|
88
|
+
allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_instance_double)
|
89
|
+
allow(Librato).to receive(:group).with('sidekiq').and_yield meter
|
90
|
+
end
|
91
|
+
|
92
|
+
before(:each) do
|
93
|
+
middleware.enabled = true
|
94
|
+
middleware.blacklist_queues = []
|
95
|
+
middleware.blacklist_queues << queue_name
|
96
|
+
end
|
97
|
+
|
98
|
+
it { expect { |b| middleware.call(some_worker_instance, some_message, queue_name, &b) }.to yield_with_no_args }
|
99
|
+
|
100
|
+
it 'should measure increment queued metric' do
|
101
|
+
expect(meter).to receive(:increment).with 'queued'
|
102
|
+
middleware.call(some_worker_instance, some_message, queue_name) {}
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'when middleware is enabled and everything is whitlisted' do
|
108
|
+
|
109
|
+
let(:sidekiq_group) { double(measure: nil, increment: nil, group: nil) }
|
110
|
+
let(:queue_group) { double(measure: nil, increment: nil, timing: nil, group: nil) }
|
111
|
+
let(:class_group) { double(measure: nil, increment: nil, timing: nil, group: nil) }
|
112
|
+
|
113
|
+
before(:each) do
|
114
|
+
middleware.enabled = true
|
115
|
+
middleware.blacklist_queues = []
|
116
|
+
end
|
117
|
+
|
118
|
+
before(:each) do
|
119
|
+
allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_instance_double)
|
120
|
+
allow(Librato).to receive(:group).with('sidekiq').and_yield(sidekiq_group)
|
121
|
+
allow(sidekiq_stats_instance_double).to receive(:queues)
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should measure queue metrics' do
|
125
|
+
expect(sidekiq_group).to receive(:group).and_yield(queue_group)
|
126
|
+
|
127
|
+
expect(queue_group).to receive(:increment).with "queued"
|
128
|
+
|
129
|
+
middleware.call(some_worker_instance, some_message, queue_name) {}
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'should measure class metrics' do
|
133
|
+
expect(sidekiq_group).to receive(:group).and_yield(queue_group)
|
134
|
+
expect(queue_group).to receive(:group).with(queue_name).and_yield(class_group)
|
135
|
+
|
136
|
+
expect(class_group).to receive(:increment).with "queued"
|
137
|
+
|
138
|
+
middleware.call(some_worker_instance, some_message, queue_name) {}
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Librato::Sidekiq::Middleware do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
stub_const "Librato::Rails", Class.new
|
7
|
+
stub_const "Sidekiq", Module.new
|
8
|
+
stub_const "Sidekiq::Stats", Class.new
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:middleware) do
|
12
|
+
allow(Sidekiq).to receive(:configure_server)
|
13
|
+
Librato::Sidekiq::Middleware.new
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#initialize' do
|
17
|
+
it 'should not call reconfigure' do
|
18
|
+
expect(Sidekiq).not_to receive(:configure_server)
|
19
|
+
Librato::Sidekiq::Middleware.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '.configure' do
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
allow(described_class).to receive(:reconfigure)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should yield with it self as argument' do
|
30
|
+
expect { |b| Librato::Sidekiq::Middleware.configure &b }.to yield_with_args(Librato::Sidekiq::Middleware)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should call reconfigure' do
|
34
|
+
expect(described_class).to receive(:reconfigure)
|
35
|
+
described_class.configure
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should return a new instance' do
|
39
|
+
expect(Librato::Sidekiq::Middleware.configure).to be_an_instance_of Librato::Sidekiq::Middleware
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '.reconfigure' do
|
45
|
+
|
46
|
+
let(:chain) { double() }
|
47
|
+
let(:config) { spy() }
|
48
|
+
|
49
|
+
it 'should add itself to the server middleware chain' do
|
50
|
+
expect(chain).to receive(:remove).with Librato::Sidekiq::Middleware
|
51
|
+
expect(chain).to receive(:add).with Librato::Sidekiq::Middleware,
|
52
|
+
described_class.options
|
53
|
+
|
54
|
+
expect(config).to receive(:server_middleware).once.and_yield(chain)
|
55
|
+
expect(Sidekiq).to receive(:configure_server).once.and_yield(config)
|
56
|
+
|
57
|
+
described_class.reconfigure
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should add ClientMiddleware to the client middleware chain' do
|
61
|
+
expect(chain).to receive(:remove).with Librato::Sidekiq::ClientMiddleware
|
62
|
+
expect(chain).to receive(:add).with Librato::Sidekiq::ClientMiddleware, described_class.options
|
63
|
+
|
64
|
+
expect(config).to receive(:client_middleware).once.and_yield(chain)
|
65
|
+
expect(Sidekiq).to receive(:configure_server).once.and_yield(config)
|
66
|
+
|
67
|
+
described_class.reconfigure
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#call' do
|
73
|
+
|
74
|
+
let(:meter) { double(measure: nil, increment: nil, group: nil) }
|
75
|
+
|
76
|
+
let(:queue_name) { 'some_awesome_queue' }
|
77
|
+
let(:some_worker_instance) { nil }
|
78
|
+
let(:some_message) { Hash['class', double(underscore: queue_name)] }
|
79
|
+
|
80
|
+
let(:sidekiq_stats_instance_double) do
|
81
|
+
double("Sidekiq::Stats", :enqueued => 1, :failed => 2, :scheduled_size => 3)
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'when middleware is not enabled' do
|
85
|
+
|
86
|
+
before(:each) { middleware.enabled = false }
|
87
|
+
|
88
|
+
it { expect { |b| middleware.call(1,2,3,&b) }.to yield_with_no_args }
|
89
|
+
|
90
|
+
it 'should not send any metrics' do
|
91
|
+
Librato.should_not_receive(:group)
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when middleware is enabled but queue is blacklisted' do
|
97
|
+
|
98
|
+
before(:each) do
|
99
|
+
allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_instance_double)
|
100
|
+
allow(Librato).to receive(:group).with('sidekiq').and_yield meter
|
101
|
+
end
|
102
|
+
|
103
|
+
before(:each) do
|
104
|
+
middleware.enabled = true
|
105
|
+
middleware.blacklist_queues = []
|
106
|
+
middleware.blacklist_queues << queue_name
|
107
|
+
end
|
108
|
+
|
109
|
+
it { expect { |b| middleware.call(some_worker_instance, some_message, queue_name, &b) }.to yield_with_no_args }
|
110
|
+
|
111
|
+
it 'should measure increment processed metric' do
|
112
|
+
expect(meter).to receive(:increment).with "processed"
|
113
|
+
middleware.call(some_worker_instance, some_message, queue_name) {}
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should measure general metrics' do
|
117
|
+
{"enqueued" => 1, "failed" => 2, "scheduled" => 3 }.each do |method, stat|
|
118
|
+
expect(meter).to receive(:measure).with(method.to_s, stat)
|
119
|
+
end
|
120
|
+
expect(meter).to receive(:increment).with "processed"
|
121
|
+
|
122
|
+
middleware.call(some_worker_instance, some_message, queue_name) {}
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'when middleware is enabled and everything is whitlisted' do
|
128
|
+
|
129
|
+
let(:some_enqueued_value) { 20 }
|
130
|
+
let(:queue_stat_hash) { Hash[queue_name, some_enqueued_value] }
|
131
|
+
let(:sidekiq_group) { double(measure: nil, increment: nil, group: nil) }
|
132
|
+
let(:queue_group) { double(measure: nil, increment: nil, timing: nil, group: nil) }
|
133
|
+
let(:class_group) { double(measure: nil, increment: nil, timing: nil, group: nil) }
|
134
|
+
|
135
|
+
before(:each) do
|
136
|
+
middleware.enabled = true
|
137
|
+
middleware.blacklist_queues = []
|
138
|
+
end
|
139
|
+
|
140
|
+
before(:each) do
|
141
|
+
allow(Sidekiq::Stats).to receive(:new).and_return(sidekiq_stats_instance_double)
|
142
|
+
allow(Librato).to receive(:group).with('sidekiq').and_yield(sidekiq_group)
|
143
|
+
allow(sidekiq_stats_instance_double).to receive(:queues).and_return queue_stat_hash
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'should measure queue metrics' do
|
147
|
+
expect(sidekiq_stats_instance_double).to receive(:queues).and_return queue_stat_hash
|
148
|
+
|
149
|
+
expect(sidekiq_group).to receive(:group).and_yield(queue_group)
|
150
|
+
|
151
|
+
expect(queue_group).to receive(:increment).with "processed"
|
152
|
+
expect(queue_group).to receive(:timing).with "time", 0
|
153
|
+
expect(queue_group).to receive(:measure).with "enqueued", some_enqueued_value
|
154
|
+
|
155
|
+
middleware.call(some_worker_instance, some_message, queue_name) {}
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should measure class metrics' do
|
159
|
+
expect(sidekiq_group).to receive(:group).and_yield(queue_group)
|
160
|
+
expect(queue_group).to receive(:group).with(queue_name).and_yield(class_group)
|
161
|
+
|
162
|
+
expect(class_group).to receive(:increment).with "processed"
|
163
|
+
expect(class_group).to receive(:timing).with "time", 0
|
164
|
+
|
165
|
+
middleware.call(some_worker_instance, some_message, queue_name) {}
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thibaudgg-librato-sidekiq
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Klein
|
8
|
+
- Ole Michaelis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-10-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sidekiq
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activesupport
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: timecop
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rake
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: Sidekiq hooks to push stats into Librato
|
85
|
+
email: scott@statuspage.io
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- Gemfile
|
91
|
+
- History.md
|
92
|
+
- LICENSE
|
93
|
+
- README.md
|
94
|
+
- lib/librato-sidekiq.rb
|
95
|
+
- lib/librato-sidekiq/client_middleware.rb
|
96
|
+
- lib/librato-sidekiq/middleware.rb
|
97
|
+
- lib/librato-sidekiq/version.rb
|
98
|
+
- librato-sidekiq.gemspec
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/unit/client_middleware_spec.rb
|
101
|
+
- spec/unit/middleware_spec.rb
|
102
|
+
homepage: http://github.com/thibaudgg/librato-sidekiq
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options:
|
108
|
+
- "--charset=UTF-8"
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.2.2
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Sidekiq hooks to push stats into Librato
|
127
|
+
test_files:
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
- spec/unit/client_middleware_spec.rb
|
130
|
+
- spec/unit/middleware_spec.rb
|