sidekiq_statsd_middleware 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10eddb18f8dcab082e13db6fcbf317727bf57133
4
+ data.tar.gz: 821263ccabfb151f223e6286318c4b58fcf064df
5
+ SHA512:
6
+ metadata.gz: 871845d889bba831a57cc7c7569f49354bd355581958b900e9eb6d2fea0c59bc160b08936ad97117a9f2687e6a47fe1de46ec93162d30aeb838038c7768c983a
7
+ data.tar.gz: 58608e5a1b7098e132cc24c396354e03a838d398097b676edd39abb740a9e38197383cd34cd69569da63c984a1a14abafc68d1cce6c728298233e16c9a3e097e
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sidekiq_statsd_middleware (0.1.0)
5
+ activesupport (~> 3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (3.2.14)
11
+ i18n (~> 0.6, >= 0.6.4)
12
+ multi_json (~> 1.0)
13
+ diff-lcs (1.2.4)
14
+ i18n (0.6.5)
15
+ multi_json (1.7.9)
16
+ rake (10.1.0)
17
+ rspec (2.14.1)
18
+ rspec-core (~> 2.14.0)
19
+ rspec-expectations (~> 2.14.0)
20
+ rspec-mocks (~> 2.14.0)
21
+ rspec-core (2.14.5)
22
+ rspec-expectations (2.14.2)
23
+ diff-lcs (>= 1.1.3, < 2.0)
24
+ rspec-mocks (2.14.3)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.3)
31
+ rake
32
+ rspec (~> 2.14)
33
+ sidekiq_statsd_middleware!
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # sidekiq_statsd_middleware
2
+
3
+ A Ruby gem that provides a server middleware component for Sidekiq that sends
4
+ worker success and failure counts, and job timing information to StatsD.
5
+
6
+ ## Usage
7
+
8
+ statsd_client = Statsd.new
9
+
10
+ Sidekiq.configure_server do |config|
11
+ config.server_middleware do |chain|
12
+ chain.add Sidekiq::Statsd::ServerMiddleware, statsd_client, prefix: "app.production.sidekiq"
13
+ end
14
+ end
15
+
16
+ When a worker with the class of, say, `MyBigJob` executes, you'll see a StatsD
17
+ count appear with the name `app.production.sidekiq.my_big_job.success` or
18
+ `app.production.sidekiq.my_big_job.failure`. The execution timing for the job
19
+ will appear as `app.production.sidekiq.my_big_job.duration` and the latency
20
+ (the time difference between when the job was queued and when the job was executed
21
+ by the worker), will appear as `app.production.sidekiq.my_big_job.latency`.
22
+
23
+ ## License
24
+
25
+ Copyright (C) 2013 Tom Taylor
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
28
+ this software and associated documentation files (the "Software"), to deal in
29
+ the Software without restriction, including without limitation the rights to
30
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
31
+ of the Software, and to permit persons to whom the Software is furnished to do
32
+ so, subject to the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be included in all
35
+ copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,40 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
3
+ module Sidekiq
4
+ module Statsd
5
+ class ServerMiddleware
6
+
7
+ attr_reader :client, :prefix
8
+
9
+ def initialize(client, options = {})
10
+ @client = client
11
+ @prefix = options.delete(:prefix) || "sidekiq"
12
+ end
13
+
14
+ def call(worker, msg, queue)
15
+ if msg['enqueued_at']
16
+ # convert and round to ms
17
+ latency = ((Time.now.to_f - msg['enqueued_at']) * 1000).round
18
+ client.timing(metric_key(worker, "latency"), latency)
19
+ end
20
+
21
+ client.time(metric_key(worker, "duration")) do
22
+ yield
23
+ end
24
+
25
+ client.increment(metric_key(worker, "success"))
26
+ rescue => e
27
+ client.increment(metric_key(worker, "failure"))
28
+ raise e
29
+ end
30
+
31
+ private
32
+
33
+ def metric_key(worker, key)
34
+ worker_name = worker.class.name.underscore.gsub("/", ".")
35
+ [prefix, worker_name, key].reject(&:nil?).join(".")
36
+ end
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Statsd
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'sidekiq/statsd/version'
2
+ require 'sidekiq/statsd/server_middleware'
@@ -0,0 +1 @@
1
+ require 'sidekiq/statsd'
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'sidekiq/statsd/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'sidekiq_statsd_middleware'
7
+ spec.version = Sidekiq::Statsd::VERSION
8
+ spec.authors = ['Tom Taylor']
9
+ spec.email = ['tom@newspaperclub.com']
10
+ spec.homepage = 'https://github.com/newspaperclub/sidekiq_statsd_middleware'
11
+ spec.summary = 'Server Middleware for Sidekiq to report metrics from workers to StatsD'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^spec/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'activesupport', '~> 3'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '~> 2.14'
24
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+ require 'sidekiq/statsd/server_middleware'
3
+
4
+ class DummyWorker; end
5
+ module DummyModule
6
+ class DummyWorker; end
7
+ end
8
+
9
+ module Sidekiq::Statsd
10
+ describe ServerMiddleware do
11
+ let(:client) { double }
12
+ let(:options) { Hash.new }
13
+
14
+ subject(:middleware) { ServerMiddleware.new(client, options) }
15
+
16
+ its(:client) { should == client }
17
+ its(:prefix) { should == "sidekiq" }
18
+
19
+ context 'being called' do
20
+ let(:worker) do
21
+ DummyWorker.new
22
+ end
23
+ let(:msg) { { 'enqueued_at' => Time.now.to_f } }
24
+ let(:queue) { double }
25
+
26
+ context 'yielding the worker block' do
27
+ it 'counts a success and times the duration' do
28
+ client.should_receive(:time).with("sidekiq.dummy_worker.duration").and_yield
29
+ client.should_receive(:timing).with("sidekiq.dummy_worker.latency", an_instance_of(Fixnum))
30
+ client.should_receive(:increment).with("sidekiq.dummy_worker.success")
31
+ middleware.call(worker, msg, queue) { double }
32
+ end
33
+ end
34
+
35
+ context 'raising in the worker block' do
36
+ it 'counts a failure and times the duration' do
37
+ client.should_receive(:timing).with("sidekiq.dummy_worker.latency", an_instance_of(Fixnum))
38
+ client.should_receive(:time).with("sidekiq.dummy_worker.duration").and_yield
39
+ client.should_receive(:increment).with("sidekiq.dummy_worker.failure")
40
+ expect { middleware.call(worker, msg, queue) { raise } }.to raise_error
41
+ end
42
+ end
43
+
44
+ context 'without a nil enqueued_at key in the msg' do
45
+ let(:msg) { Hash.new }
46
+
47
+ it 'should not time the latency' do
48
+ client.should_receive(:time).with("sidekiq.dummy_worker.duration").and_yield
49
+ client.should_receive(:increment).with("sidekiq.dummy_worker.success")
50
+ client.should_not_receive(:timing)
51
+ middleware.call(worker, msg, queue) { double }
52
+ end
53
+ end
54
+ end
55
+
56
+ context 'with a prefix' do
57
+ let(:options) { { :prefix => "foo"} }
58
+ its(:prefix) { should == "foo" }
59
+
60
+ context 'being called successfully' do
61
+ let(:worker) do
62
+ DummyWorker.new
63
+ end
64
+ let(:msg) { { 'enqueued_at' => Time.now.to_f } }
65
+ let(:queue) { double }
66
+
67
+ it 'should count with the correct prefix' do
68
+ client.should_receive(:time).with("foo.dummy_worker.duration").and_yield
69
+ client.should_receive(:timing).with("foo.dummy_worker.latency", an_instance_of(Fixnum))
70
+ client.should_receive(:increment).with("foo.dummy_worker.success")
71
+ middleware.call(worker, msg, queue) { double }
72
+ end
73
+ end
74
+ end
75
+
76
+ context 'with a worker class containing a namespace' do
77
+ let(:worker) do
78
+ DummyModule::DummyWorker.new
79
+ end
80
+ let(:msg) { { 'enqueued_at' => Time.now.to_f } }
81
+ let(:queue) { double }
82
+
83
+ it 'should count with the correct prefix' do
84
+ client.should_receive(:time).with("sidekiq.dummy_module.dummy_worker.duration").and_yield
85
+ client.should_receive(:timing).with("sidekiq.dummy_module.dummy_worker.latency", an_instance_of(Fixnum))
86
+ client.should_receive(:increment).with("sidekiq.dummy_module.dummy_worker.success")
87
+ middleware.call(worker, msg, queue) { double }
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,11 @@
1
+ RSpec.configure do |config|
2
+ config.treat_symbols_as_metadata_keys_with_true_values = true
3
+ config.run_all_when_everything_filtered = true
4
+ config.filter_run :focus
5
+
6
+ # Run specs in random order to surface order dependencies. If you find an
7
+ # order dependency and want to debug it, you can fix the order by providing
8
+ # the seed, which is printed after each run.
9
+ # --seed 1234
10
+ config.order = 'random'
11
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq_statsd_middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Taylor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.14'
69
+ description:
70
+ email:
71
+ - tom@newspaperclub.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - README.md
80
+ - Rakefile
81
+ - lib/sidekiq/statsd.rb
82
+ - lib/sidekiq/statsd/server_middleware.rb
83
+ - lib/sidekiq/statsd/version.rb
84
+ - lib/sidekiq_statsd_middleware.rb
85
+ - sidekiq_statsd_middleware.gemspec
86
+ - spec/sidekiq/statsd/server_middleware_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/newspaperclub/sidekiq_statsd_middleware
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Server Middleware for Sidekiq to report metrics from workers to StatsD
112
+ test_files:
113
+ - spec/sidekiq/statsd/server_middleware_spec.rb
114
+ - spec/spec_helper.rb