strumbar 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.0 (2013-02-09)
4
+
5
+ #### /!\ Breaks Backwards Compatibility /!\
6
+
7
+ * instrumentation is no longer loaded by default.
8
+ * `Strumbar::Configuration#instrumentation &block` is now gone: use the new `config.instruments.use MyInstrument` syntax instead.
9
+ * default values for options are now stored on the configuration object instead of the `Strumbar` module.
10
+
3
11
  ## 0.3.0 (2013-02-04)
4
12
 
5
13
  * Adds wrapper for gauge, set, and count: `Strumbar::Client.(gauge|set|count) key, value, sample_rate = Strumbar.default_rate`.
data/README.md CHANGED
@@ -66,30 +66,38 @@ Strumbar.strum 'view.render', payload do
66
66
  end
67
67
  ```
68
68
 
69
+ ## Instruments
69
70
 
70
- ## Default Instruments
71
+ Strumbar allows middleware-style instruments to be added via the configure block. These instruments
72
+ need only respond to `#load` with an optional hash. Strumbar comes with a few instruments to be added.
73
+ Here's an example:
71
74
 
72
- Strumbar takes the approach of auto-detecting the libraries being used and
73
- loading default instrumentation subscriptions. Currently, this list includes:
75
+ ``` ruby
76
+ class Guitar
77
+ def self.load options = {}
78
+ Strumbar.subscribe 'query.*' do |client, event|
79
+ client.increment "query.#{event.payload[:query]}", options[:rate]
80
+ end
81
+ end
82
+ end
74
83
 
75
- - ActionController
76
- - ActiveRecord
77
- - Redis
84
+ Strumbar.configure do |config|
85
+ # Can pass optional hash of data to controller for access when loading
86
+ config.instruments.use Strumbar::Instrumentation::ActiveRecord, rate: 0.8
87
+ config.instruments.use Strumbar::Instrumentation::ActionController, rate: 1.0
78
88
 
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:
89
+ # Unless passed, `rate` will be passed as the value of Strumbar.default_rate
90
+ config.instruments.use Strumbar::Instrumentation::Redis
81
91
 
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
92
+ # When passing an array of objects, each element of the array will use the
93
+ # same optional information, and will use the default rate if not supplied
88
94
 
89
- AppName::Instrumentation::ThirdPartyInstrument.load rate: 0.8
90
- end
91
- end
95
+ # Guitar and SnareDrum will receive `{ rate: Strumbar.default_rate }`
96
+ config.instruments.use [Guitar, SnareDrum]
92
97
 
98
+ # SixStringBass and FourStringBass will receive the same hash
99
+ config.instruments.use [SixStringBass, FourStringBass], rate: 0.75
100
+ end
93
101
  ```
94
102
 
95
103
  ## Authors
@@ -5,11 +5,10 @@ require 'strumbar/instrumentation'
5
5
 
6
6
  require 'active_support'
7
7
  require 'active_support/core_ext/object/try'
8
+ require 'active_support/core_ext/module/delegation'
8
9
 
9
10
  module Strumbar
10
11
  class << self
11
- attr_reader :configuration
12
-
13
12
  def configure
14
13
  @configuration = Configuration.new
15
14
  yield @configuration
@@ -20,21 +19,11 @@ module Strumbar
20
19
  @client ||= Client.new host, port
21
20
  end
22
21
 
23
- def host
24
- configuration.try(:host) || 'localhost'
25
- end
26
-
27
- def port
28
- configuration.try(:port) || 8125
22
+ def configuration
23
+ @configuration ||= Configuration.new
29
24
  end
30
25
 
31
- def application
32
- configuration.try(:application) || 'statsd_appname'
33
- end
34
-
35
- def default_rate
36
- configuration.try(:default_rate) || 1
37
- end
26
+ delegate :host, :port, :instruments, :application, :default_rate, to: :configuration
38
27
 
39
28
  def subscribe identifier
40
29
  ActiveSupport::Notifications.subscribe identifier do |*args|
@@ -1,13 +1,69 @@
1
1
  module Strumbar
2
+ class InstrumentList
3
+ include Enumerable
4
+
5
+ def initialize
6
+ @instruments = []
7
+ end
8
+
9
+ def each &block
10
+ @instruments.each &block
11
+ end
12
+
13
+ def empty?
14
+ @instruments.empty?
15
+ end
16
+
17
+ def use instruments, options = {}
18
+ if instruments.respond_to? :each
19
+ instruments.each do |instrument|
20
+ add instrument, options
21
+ end
22
+ else
23
+ add instruments, options
24
+ end
25
+ end
26
+
27
+ def delete instrument
28
+ @instruments.delete_if { |i| i[0] == instrument }
29
+ end
30
+
31
+ private
32
+
33
+ def add instrument, options
34
+ if instrument.respond_to? :load
35
+ @instruments << [instrument, options]
36
+ else
37
+ raise 'Instrument does not respond to load'
38
+ end
39
+ end
40
+ end
41
+
2
42
  class Configuration
3
43
  attr_accessor :port,
4
44
  :host,
5
45
  :application,
6
46
  :default_rate,
7
- :custom_load
47
+ :instruments
48
+
49
+ def initialize
50
+ @instruments = InstrumentList.new
51
+ end
52
+
53
+ def host
54
+ @host || 'localhost'
55
+ end
56
+
57
+ def port
58
+ @port || 8125
59
+ end
60
+
61
+ def application
62
+ @application || 'statsd_appname'
63
+ end
8
64
 
9
- def instrumentation(&block)
10
- @custom_load = block
65
+ def default_rate
66
+ @default_rate || 1
11
67
  end
12
68
  end
13
69
  end
@@ -6,15 +6,13 @@ module Strumbar
6
6
  autoload :Redis, 'strumbar/instrumentation/redis'
7
7
 
8
8
  def self.load
9
- custom_load = Strumbar.configuration.custom_load
9
+ Strumbar.instruments.each do |instrument_info|
10
+ instrument = instrument_info[0]
11
+ options = instrument_info[1]
10
12
 
11
- if custom_load
12
- custom_load.call
13
- else
14
- Strumbar::Instrumentation::ActionController.load if defined?(::ActionController)
15
- Strumbar::Instrumentation::ActiveRecord.load if defined?(::ActiveRecord)
16
- Strumbar::Instrumentation::Mongoid.load if defined?(::Mongoid)
17
- Strumbar::Instrumentation::Redis.load if defined?(::Redis)
13
+ options[:rate] ||= Strumbar.default_rate
14
+
15
+ instrument.load options
18
16
  end
19
17
  end
20
18
  end
@@ -1,3 +1,3 @@
1
1
  module Strumbar
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module Strumbar
4
+ describe Configuration do
5
+ let(:config) { Configuration.new }
6
+
7
+ context "default configuration values" do
8
+ it "defaults #host to 'localhost'" do
9
+ config.host.should == 'localhost'
10
+ end
11
+
12
+ it "defaults #port to 8125" do
13
+ config.port.should == 8125
14
+ end
15
+
16
+ it "defaults #application to 'statsd_appname'" do
17
+ config.application.should == 'statsd_appname'
18
+ end
19
+
20
+ it "defaults #default_rate to 1" do
21
+ config.default_rate.should == 1
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -1,105 +1,97 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Guitar
4
+ def self.load options = {}
5
+
6
+ end
7
+ end
8
+
9
+ class BassGuitar < Guitar ; end
10
+ class Pencil ; end
11
+
3
12
  describe Strumbar::Instrumentation do
4
13
  describe '#load' do
5
- def undefine klass
6
- Object.send :remove_const, klass
7
- end
8
-
9
- context 'without a custom instrumentation loader' do
10
- before do
14
+ context 'configuration block' do
15
+ it 'accepts an instrument that responds to #load' do
11
16
  Strumbar.configure do |config|
17
+ config.instruments.use Guitar
12
18
  end
13
- end
14
-
15
- it 'loads the ActionController subscription if ActionController is defined' do
16
-
17
- ActionController = true
18
-
19
- Strumbar::Instrumentation::ActionController.should_receive :load
20
- Strumbar::Instrumentation.load
21
19
 
22
- undefine :ActionController
20
+ Strumbar.instruments.should include [Guitar, { rate: 1.0}]
23
21
  end
24
22
 
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
29
-
30
- it 'loads the ActiveRecord subscription if ActiveRecord is loaded' do
31
- ActiveRecord = true
32
-
33
- Strumbar::Instrumentation::ActiveRecord.should_receive :load
34
- Strumbar::Instrumentation.load
23
+ it 'accepts an array of instruments' do
24
+ Strumbar.configure do |config|
25
+ config.instruments.use [Guitar, BassGuitar]
26
+ end
35
27
 
36
- undefine :ActiveRecord
28
+ Strumbar.instruments.should include [Guitar, {rate: 1.0}]
29
+ Strumbar.instruments.should include [BassGuitar, {rate: 1.0}]
37
30
  end
38
31
 
39
- it 'does not load ActiveRecord subscription if not defined' do
40
- Strumbar::Instrumentation::ActiveRecord.should_not_receive :load
41
- Strumbar::Instrumentation.load
32
+ it 'allows deletion of instruments from Strumbar' do
33
+ Strumbar.configure do |config|
34
+ config.instruments.use Guitar
35
+ config.instruments.should include [Guitar, {}]
36
+ config.instruments.delete Guitar
37
+ config.instruments.should_not include [Guitar, {}]
38
+ end
42
39
  end
43
40
 
44
- it 'loads the Redis subscription if Redis is defined' do
45
- class Redis
46
- class Client
47
- def process ; end
48
- end
41
+ it 'raises an exception when trying to load an Instrument that does not respond to load' do
42
+ Strumbar.configure do |config|
43
+ expect { config.instruments.use Pencil }.to raise_error
49
44
  end
50
-
51
- Strumbar::Instrumentation::Redis.should_receive :load
52
- Strumbar::Instrumentation.load
53
-
54
- undefine :Redis
55
45
  end
56
46
 
57
- it 'does not load Redis subscription if not defined' do
58
- Strumbar::Instrumentation::Redis.should_not_receive :load
59
- Strumbar::Instrumentation.load
47
+ it 'accepts a hash of options' do
48
+ Guitar.should_receive(:load).with(rate: 1.2)
49
+ Strumbar.configure do |config|
50
+ config.instruments.use Guitar, rate: 1.2
51
+ end
60
52
  end
61
53
 
62
- end
63
-
64
- context 'with a custom instrumentations loader' do
65
- before do
54
+ it 'passes the default rate to an instrument if no hash is specified' do
55
+ Guitar.should_receive(:load).with(rate: 1.0)
66
56
  Strumbar.configure do |config|
67
- config.instrumentation do
68
- Strumbar::Instrumentation::ActionController.load
69
- end
57
+ config.instruments.use Guitar
70
58
  end
71
59
  end
72
60
 
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
61
+ it 'passes the same hash to an array of instruments' do
62
+ Guitar.should_receive(:load).with(rate: 1.5)
63
+ BassGuitar.should_receive(:load).with(rate: 1.5)
79
64
 
80
- it 'does not load the ActiveRecord subscription even if ActiveRecord is loaded' do
81
- ActiveRecord = true
65
+ Strumbar.configure do |config|
66
+ config.instruments.use [Guitar, BassGuitar], rate: 1.5
67
+ end
68
+ end
82
69
 
83
- Strumbar::Instrumentation::ActiveRecord.should_not_receive :load
84
- Strumbar::Instrumentation.load
70
+ it 'passes the default rate to to an array of instruments' do
71
+ Guitar.should_receive(:load).with(rate: 1.0)
72
+ BassGuitar.should_receive(:load).with(rate: 1.0)
85
73
 
86
- undefine :ActiveRecord
74
+ Strumbar.configure do |config|
75
+ config.instruments.use [Guitar, BassGuitar]
76
+ end
87
77
  end
88
78
 
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
79
+ it 'loads no default instruments unless specified' do
80
+ Strumbar.configure do
81
+ # NOPE
94
82
  end
95
-
83
+ Strumbar.instruments.should be_empty
96
84
  Strumbar::Instrumentation::Redis.should_not_receive :load
97
- Strumbar::Instrumentation.load
98
-
99
- undefine :Redis
100
85
  end
101
86
 
102
- end
87
+ it 'sends the load message to all instruments in Strumbar' do
88
+ Guitar.should_receive :load
89
+ BassGuitar.should_receive :load
103
90
 
91
+ Strumbar.configure do |config|
92
+ config.instruments.use [Guitar, BassGuitar]
93
+ end
94
+ end
95
+ end
104
96
  end
105
97
  end
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.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-04 00:00:00.000000000 Z
13
+ date: 2013-02-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -103,6 +103,7 @@ files:
103
103
  - lib/strumbar/instrumentation/redis.rb
104
104
  - lib/strumbar/version.rb
105
105
  - spec/client_spec.rb
106
+ - spec/configuration_spec.rb
106
107
  - spec/instrumentation/mongoid/controller_runtime_spec.rb
107
108
  - spec/instrumentation/mongoid/runtime_tracker_spec.rb
108
109
  - spec/instrumentation/redis_spec.rb
@@ -124,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
125
  version: '0'
125
126
  segments:
126
127
  - 0
127
- hash: -2742179501174448112
128
+ hash: -3917631635399669521
128
129
  required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  none: false
130
131
  requirements:
@@ -133,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
134
  version: '0'
134
135
  segments:
135
136
  - 0
136
- hash: -2742179501174448112
137
+ hash: -3917631635399669521
137
138
  requirements: []
138
139
  rubyforge_project:
139
140
  rubygems_version: 1.8.24
@@ -142,6 +143,7 @@ specification_version: 3
142
143
  summary: Helper library to strum along in your application.
143
144
  test_files:
144
145
  - spec/client_spec.rb
146
+ - spec/configuration_spec.rb
145
147
  - spec/instrumentation/mongoid/controller_runtime_spec.rb
146
148
  - spec/instrumentation/mongoid/runtime_tracker_spec.rb
147
149
  - spec/instrumentation/redis_spec.rb