strumbar 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -20,13 +20,23 @@ Or install it yourself as:
20
20
  ## Usage
21
21
 
22
22
 
23
- Configuration:
23
+ Configuration (all options shown with default values):
24
24
 
25
25
  ``` ruby
26
26
  Strumbar.configure do |config|
27
- config.port = 80
28
- config.host = 'instrument.majorleaguegaming.com'
29
- config.application = 'instrument'
27
+
28
+ # Application name as it should be stored by your Statsd backend.
29
+ config.application = 'statsd_appname'
30
+
31
+ # Statsd hostname
32
+ config.host = 'statsd.appname.example'
33
+
34
+ # Statsd port
35
+ config.port = 8125
36
+
37
+ # Default sample rate for all events.
38
+ config.default_rate = 1
39
+
30
40
  end
31
41
  ```
32
42
 
@@ -66,7 +76,19 @@ loading default instrumentation subscriptions. Currently, this list includes:
66
76
  - ActiveRecord
67
77
  - Redis
68
78
 
69
- More default instrumentation defaults will be added.
79
+ Alternatively, you can choose specific instrumentations to load
80
+ and set your own sample rates for each by passing a block to config.instrumentation, like so:
81
+
82
+ ``` ruby
83
+ Strumbar.configure do |config|
84
+ config.instrumentation do
85
+ Strumbar::Instrumentation::ActionController.load rate: 0.5
86
+ Strumbar::Instrumentation::ActiveRecord.load
87
+ Strumbar::Instrumentation::Redis.load
88
+
89
+ AppName::Instrumentation::ThirdPartyInstrument.load rate: 0.8
90
+ end
91
+ end
70
92
 
71
93
  ## Authors
72
94
 
@@ -7,15 +7,15 @@ module Strumbar
7
7
  @port = port
8
8
  end
9
9
 
10
- def timing stat, time, sample_rate = 1
10
+ def timing stat, time, sample_rate = Strumbar.default_rate
11
11
  super "#{Strumbar.application}.#{stat}", time, sample_rate
12
12
  end
13
13
 
14
- def increment stat, sample_rate = 1
14
+ def increment stat, sample_rate = Strumbar.default_rate
15
15
  super "#{Strumbar.application}.#{stat}", sample_rate
16
16
  end
17
17
 
18
- def decrement stat, sample_rate = 1
18
+ def decrement stat, sample_rate = Strumbar.default_rate
19
19
  super "##{Strumbar.application}.#{stat}", sample_rate
20
20
  end
21
21
  end
@@ -1,5 +1,13 @@
1
1
  module Strumbar
2
2
  class Configuration
3
- attr_accessor :port, :host, :application
3
+ attr_accessor :port,
4
+ :host,
5
+ :application,
6
+ :default_rate,
7
+ :custom_load
8
+
9
+ def instrumentation(&block)
10
+ @custom_load = block
11
+ end
4
12
  end
5
13
  end
@@ -1,15 +1,17 @@
1
1
  module Strumbar
2
2
  module Instrumentation
3
3
  module ActionController
4
- def self.load
4
+ def self.load(options={})
5
+ options[:rate] ||= Strumbar.default_rate
6
+
5
7
  Strumbar.subscribe /process_action.action_controller/ do |client, event|
6
8
  key = "#{event.payload[:controller]}.#{event.payload[:action]}"
7
9
 
8
- client.timing "#{key}.total_time", event.duration
9
- client.timing "#{key}.view_time", event.payload[:view_runtime]
10
- client.timing "#{key}.db_time", event.payload[:db_runtime]
10
+ client.timing "#{key}.total_time", event.duration, options[:rate]
11
+ client.timing "#{key}.view_time", event.payload[:view_runtime], options[:rate]
12
+ client.timing "#{key}.db_time", event.payload[:db_runtime], options[:rate]
11
13
 
12
- client.increment "#{key}.status.#{event.payload[:status]}"
14
+ client.increment "#{key}.status.#{event.payload[:status]}", options[:rate]
13
15
  end
14
16
  end
15
17
  end
@@ -1,9 +1,11 @@
1
1
  module Strumbar
2
2
  module Instrumentation
3
3
  module ActiveRecord
4
- def self.load
4
+ def self.load(options={})
5
+ options[:rate] ||= Strumbar.default_rate
6
+
5
7
  Strumbar.subscribe /sql.active_record/ do |client, event|
6
- client.timing 'query_log', event.duration
8
+ client.timing 'query_log', event.duration, options[:rate]
7
9
  end
8
10
  end
9
11
  end
@@ -1,10 +1,12 @@
1
1
  module Strumbar
2
2
  module Instrumentation
3
3
  module Redis
4
- def self.load
4
+ def self.load(options={})
5
+ options[:rate] ||= Strumbar.default_rate
6
+
5
7
  Strumbar.subscribe 'query.redis' do |client, event|
6
- client.increment 'query.redis'
7
- client.increment 'failure.redis' if event.payload[:failure]
8
+ client.increment 'query.redis', options[:rate]
9
+ client.increment('failure.redis', options[:rate]) if event.payload[:failure]
8
10
 
9
11
  command = case event.payload[:command]
10
12
  when NilClass then nil
@@ -14,7 +16,7 @@ module Strumbar
14
16
 
15
17
  command.gsub!(/:/, '_') unless command.nil?
16
18
 
17
- client.timing "#{command}.redis", event.duration
19
+ client.timing "#{command}.redis", event.duration, options[:rate]
18
20
  end
19
21
 
20
22
  unless ::Redis::Client.instance_methods.include? :call_with_instrumentation
@@ -5,9 +5,15 @@ module Strumbar
5
5
  autoload :ActiveRecord, 'strumbar/instrumentation/active_record'
6
6
 
7
7
  def self.load
8
- Strumbar::Instrumentation::ActionController.load if defined? ::ActionController
9
- Strumbar::Instrumentation::ActiveRecord.load if defined? ::ActiveRecord
10
- Strumbar::Instrumentation::Redis.load if defined? ::Redis
8
+ custom_load = Strumbar.configuration.custom_load
9
+
10
+ if custom_load
11
+ custom_load.call
12
+ else
13
+ Strumbar::Instrumentation::ActionController.load if defined?(::ActionController)
14
+ Strumbar::Instrumentation::ActiveRecord.load if defined?(::ActiveRecord)
15
+ Strumbar::Instrumentation::Redis.load if defined?(::Redis)
16
+ end
11
17
  end
12
18
  end
13
19
  end
@@ -1,3 +1,3 @@
1
1
  module Strumbar
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/strumbar.rb CHANGED
@@ -29,7 +29,11 @@ module Strumbar
29
29
  end
30
30
 
31
31
  def application
32
- configuration.try(:application) || 'my_awesome_app'
32
+ configuration.try(:application) || 'statsd_appname'
33
+ end
34
+
35
+ def default_rate
36
+ configuration.try(:default_rate) || 1
33
37
  end
34
38
 
35
39
  def subscribe identifier
@@ -27,7 +27,7 @@ describe Strumbar::Instrumentation::Redis do
27
27
  end
28
28
 
29
29
  it 'subscribes to query.redis notifications' do
30
- Strumbar.client.should_receive(:increment).with('query.redis')
30
+ Strumbar.client.should_receive(:increment).with('query.redis', 1)
31
31
  Strumbar.strum 'query.redis', {}
32
32
  end
33
33
 
@@ -35,4 +35,20 @@ describe Strumbar::Instrumentation::Redis do
35
35
  client = Redis::Client.new
36
36
  client.call 'foo'
37
37
  end
38
+
39
+ context 'with a user-defined redis sample rate' do
40
+ before do
41
+ Strumbar.configure do |config|
42
+ c.instrumentation do
43
+ Strumbar::Instrumentation::Redis.load rate: 0.5
44
+ end
45
+ end
46
+
47
+ it 'subscribes to query.redis notifications' do
48
+ Strumbar.client.should_receive(:increment).with('query.redis', 0.5)
49
+ Strumbar.strum 'query.redis', {}
50
+ end
51
+
52
+ end
53
+ end
38
54
  end
@@ -6,51 +6,100 @@ describe Strumbar::Instrumentation do
6
6
  Object.send :remove_const, klass
7
7
  end
8
8
 
9
- it 'loads the ActionController subscription if ActionController is defined' do
9
+ context 'without a custom instrumentation loader' do
10
+ before do
11
+ Strumbar.configure do |config|
12
+ end
13
+ end
10
14
 
11
- ActionController = true
15
+ it 'loads the ActionController subscription if ActionController is defined' do
12
16
 
13
- Strumbar::Instrumentation::ActionController.should_receive :load
14
- Strumbar::Instrumentation.load
17
+ ActionController = true
15
18
 
16
- undefine :ActionController
17
- end
19
+ Strumbar::Instrumentation::ActionController.should_receive :load
20
+ Strumbar::Instrumentation.load
18
21
 
19
- it 'does not load ActionController subscription if not defined' do
20
- Strumbar::Instrumentation::ActionController.should_not_receive :load
21
- Strumbar::Instrumentation.load
22
- end
22
+ undefine :ActionController
23
+ end
23
24
 
24
- it 'loads the ActiveRecord subscription if ActiveRecord is loaded' do
25
- ActiveRecord = true
25
+ it 'does not load ActionController subscription if not defined' do
26
+ Strumbar::Instrumentation::ActionController.should_not_receive :load
27
+ Strumbar::Instrumentation.load
28
+ end
26
29
 
27
- Strumbar::Instrumentation::ActiveRecord.should_receive :load
28
- Strumbar::Instrumentation.load
30
+ it 'loads the ActiveRecord subscription if ActiveRecord is loaded' do
31
+ ActiveRecord = true
29
32
 
30
- undefine :ActiveRecord
31
- end
33
+ Strumbar::Instrumentation::ActiveRecord.should_receive :load
34
+ Strumbar::Instrumentation.load
32
35
 
33
- it 'does not load ActiveRecord subscription if not defined' do
34
- Strumbar::Instrumentation::ActiveRecord.should_not_receive :load
35
- Strumbar::Instrumentation.load
36
- end
36
+ undefine :ActiveRecord
37
+ end
38
+
39
+ it 'does not load ActiveRecord subscription if not defined' do
40
+ Strumbar::Instrumentation::ActiveRecord.should_not_receive :load
41
+ Strumbar::Instrumentation.load
42
+ end
37
43
 
38
- it 'loads the Redis subscription if Redis is defined' do
39
- class Redis
40
- class Client
41
- def process ; end
44
+ it 'loads the Redis subscription if Redis is defined' do
45
+ class Redis
46
+ class Client
47
+ def process ; end
48
+ end
42
49
  end
50
+
51
+ Strumbar::Instrumentation::Redis.should_receive :load
52
+ Strumbar::Instrumentation.load
53
+
54
+ undefine :Redis
43
55
  end
44
56
 
45
- Strumbar::Instrumentation::Redis.should_receive :load
46
- Strumbar::Instrumentation.load
57
+ it 'does not load Redis subscription if not defined' do
58
+ Strumbar::Instrumentation::Redis.should_not_receive :load
59
+ Strumbar::Instrumentation.load
60
+ end
47
61
 
48
- undefine :Redis
49
62
  end
50
63
 
51
- it 'does not load Redis subscription if not defined' do
52
- Strumbar::Instrumentation::Redis.should_not_receive :load
53
- Strumbar::Instrumentation.load
64
+ context 'with a custom instrumentations loader' do
65
+ before do
66
+ Strumbar.configure do |config|
67
+ config.instrumentation do
68
+ Strumbar::Instrumentation::ActionController.load
69
+ end
70
+ end
71
+ end
72
+
73
+ it 'does load the ActionController subscription' do
74
+ ActionController = true
75
+ Strumbar::Instrumentation::ActionController.should_receive :load
76
+ Strumbar::Instrumentation.load
77
+ undefine :ActionController
78
+ end
79
+
80
+ it 'does not load the ActiveRecord subscription even if ActiveRecord is loaded' do
81
+ ActiveRecord = true
82
+
83
+ Strumbar::Instrumentation::ActiveRecord.should_not_receive :load
84
+ Strumbar::Instrumentation.load
85
+
86
+ undefine :ActiveRecord
87
+ end
88
+
89
+ it 'does not load the Redis subscription even if Redis is defined' do
90
+ class Redis
91
+ class Client
92
+ def process ; end
93
+ end
94
+ end
95
+
96
+ Strumbar::Instrumentation::Redis.should_not_receive :load
97
+ Strumbar::Instrumentation.load
98
+
99
+ undefine :Redis
100
+ end
101
+
54
102
  end
103
+
55
104
  end
56
105
  end
@@ -59,19 +59,39 @@ describe Strumbar do
59
59
  end
60
60
  end
61
61
 
62
+ describe 'after #configure' do
63
+ context 'with default values' do
64
+ before { Strumbar.configure { } }
65
+ subject { Strumbar }
66
+
67
+ its(:default_rate) { should == 1 }
68
+ end
69
+
70
+ context 'with user values' do
71
+ before do
72
+ Strumbar.configure do |config|
73
+ config.default_rate = 0.5
74
+ end
75
+ end
76
+ subject { Strumbar }
77
+
78
+ its(:default_rate) { should == 0.5 }
79
+ end
80
+ end
81
+
62
82
  describe "#application" do
63
83
  it "returns the configured value" do
64
- Strumbar.configure { |c| c.application = "foobar" }
84
+ Strumbar.configure { |config| config.application = "foobar" }
65
85
  Strumbar.application.should == "foobar"
66
86
  end
67
87
  it "defaults to something comically bad so you'll change it" do
68
- Strumbar.application.should == "my_awesome_app"
88
+ Strumbar.application.should == "statsd_appname"
69
89
  end
70
90
  end
71
91
 
72
92
  describe "#host" do
73
93
  it "returns the configured value" do
74
- Strumbar.configure { |c| c.host = "statsd.app" }
94
+ Strumbar.configure { |config| config.host = "statsd.app" }
75
95
  Strumbar.host.should == "statsd.app"
76
96
  end
77
97
  it "defaults to localhost" do
@@ -81,7 +101,7 @@ describe Strumbar do
81
101
 
82
102
  describe "#port" do
83
103
  it "returns the configured port" do
84
- Strumbar.configure { |c| c.port = 9999 }
104
+ Strumbar.configure { |config| config.port = 9999 }
85
105
  Strumbar.port.should == 9999
86
106
  end
87
107
  it "defaults to 8125" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strumbar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-06 00:00:00.000000000 Z
13
+ date: 2012-08-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
17
- requirement: &70199312049800 !ruby/object:Gem::Requirement
17
+ requirement: &70333463715220 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70199312049800
25
+ version_requirements: *70333463715220
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: statsd
28
- requirement: &70199312048940 !ruby/object:Gem::Requirement
28
+ requirement: &70333463714500 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70199312048940
36
+ version_requirements: *70333463714500
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &70199312064100 !ruby/object:Gem::Requirement
39
+ requirement: &70333463713740 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70199312064100
47
+ version_requirements: *70333463713740
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rake
50
- requirement: &70199312060980 !ruby/object:Gem::Requirement
50
+ requirement: &70333463713100 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70199312060980
58
+ version_requirements: *70333463713100
59
59
  description: An instrumentation utility.
60
60
  email:
61
61
  - anordman@majorleaguegaming.com
@@ -97,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  segments:
99
99
  - 0
100
- hash: -952329107978031015
100
+ hash: -4086101781260081787
101
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  none: false
103
103
  requirements:
@@ -106,10 +106,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  segments:
108
108
  - 0
109
- hash: -952329107978031015
109
+ hash: -4086101781260081787
110
110
  requirements: []
111
111
  rubyforge_project:
112
- rubygems_version: 1.8.17
112
+ rubygems_version: 1.8.10
113
113
  signing_key:
114
114
  specification_version: 3
115
115
  summary: Helper library to strum along in your application.