fozzie 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/fozzie.gemspec CHANGED
@@ -21,5 +21,9 @@ Gem::Specification.new do |s|
21
21
  s.add_development_dependency 'rake'
22
22
  s.add_development_dependency 'rspec'
23
23
  s.add_development_dependency 'mocha'
24
+ s.add_development_dependency 'syntax'
25
+ s.add_development_dependency 'rack-test'
24
26
  s.add_development_dependency 'simplecov'
27
+ s.add_development_dependency 'sinatra'
28
+ s.add_development_dependency 'actionpack'
25
29
  end
data/lib/fozzie.rb CHANGED
@@ -1,7 +1,12 @@
1
+ # generic
1
2
  require "fozzie/config"
2
3
  require "fozzie/classes"
3
4
  require "fozzie/version"
4
5
 
6
+ # middleware
7
+ require "fozzie/rack/middleware"
8
+ require "fozzie/rails/middleware"
9
+
5
10
  module Fozzie
6
11
  extend Fozzie::Config
7
12
  include Fozzie::Classes
@@ -36,7 +36,7 @@ module Fozzie
36
36
  def event(type)
37
37
  timing "event.#{type.to_s}", Time.now.usec
38
38
  end
39
-
39
+
40
40
  def send_to_socket(message)
41
41
  begin
42
42
  super(message)
@@ -40,7 +40,7 @@ module Fozzie
40
40
  :host => '127.0.0.1',
41
41
  :port => 8125,
42
42
  :config_path => '',
43
- :env => (ENV['RAILS_ENV'] || 'development'),
43
+ :env => (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'),
44
44
  :appname => ''
45
45
  }.dup
46
46
  end
@@ -0,0 +1,39 @@
1
+ module Fozzie
2
+ module Rack
3
+ class Middleware
4
+
5
+ attr_reader :app
6
+
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ k = generate_key(env)
13
+ if k.nil?
14
+ self.call_without_timer(env)
15
+ else
16
+ self.call_with_timer(k, env)
17
+ end
18
+ end
19
+
20
+ def call_without_timer(env)
21
+ @app.call(env)
22
+ end
23
+
24
+ def call_with_timer(key, env)
25
+ S.time_to_do key do
26
+ @app.call(env)
27
+ end
28
+ end
29
+
30
+ def generate_key(env)
31
+ s = env['PATH_INFO']
32
+ return nil if s.nil?
33
+ s = (s == '/' ? 'index' : s.gsub(/.(\/)./) {|m| m.gsub('/', '.') }.gsub(/\//, '').strip)
34
+ (s.nil? || s.empty? ? nil : "#{s}.render")
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ require 'fozzie/rack/middleware'
2
+
3
+ module Fozzie
4
+ module Rails
5
+ class Middleware < Fozzie::Rack::Middleware
6
+
7
+ def generate_key(env)
8
+ s = env['PATH_INFO']
9
+ return nil if s.nil?
10
+ path = ActionController::Routing::Routes.recognize_path(s)
11
+ [path[:controller], path[:action], "render"].join('.')
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module Fozzie
2
+ module Rack
3
+ class Railtie < Rails::Railtie
4
+
5
+ initializer "fozzie.rack.insert_middleware" do |app|
6
+ app.config.middleware.use "Fozzie::Rails::Middleware"
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Fozzie
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -10,7 +10,7 @@ describe Fozzie::Config do
10
10
  it { should respond_to(:configure) }
11
11
  it { should respond_to(:config) }
12
12
  it { should respond_to(:c) }
13
-
13
+
14
14
  it "allows dynamic assignment" do
15
15
  {
16
16
  :host => 'somewhere.local',
@@ -21,16 +21,16 @@ describe Fozzie::Configuration do
21
21
  it "defaults env" do
22
22
  subject.env.should == 'development'
23
23
  end
24
-
24
+
25
25
  it "creates a data prefix" do
26
26
  subject.data_prefix.should == 'development'
27
27
  end
28
-
28
+
29
29
  it "creates a data prefix with appname when set" do
30
30
  subject.appname = 'astoria'
31
31
  subject.data_prefix.should == 'astoria.development'
32
32
  end
33
-
33
+
34
34
  it "handles missing configuration namespace" do
35
35
  proc { Fozzie::Configuration.new({:env => 'blbala', :config_path => 'spec/'}) }.should_not raise_error
36
36
  end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+ require 'sinatra/base'
3
+ require 'rack/test'
4
+
5
+ describe Fozzie::Rack::Middleware do
6
+
7
+ subject do
8
+ RackApp = Class.new do
9
+ def call(env)
10
+ env
11
+ end
12
+ end unless defined?(RackApp)
13
+ Fozzie::Rack::Middleware.new RackApp.new
14
+ end
15
+
16
+ it { should respond_to(:call) }
17
+ it { should respond_to(:generate_key) }
18
+
19
+ describe "subject" do
20
+ it "returns env on call for testing" do
21
+ subject.call({}).should == {}
22
+ end
23
+ end
24
+
25
+ describe "#call" do
26
+
27
+ it "ignored stats request when path not valid" do
28
+ fake_env = { 'PATH_INFO' => '' }
29
+ subject.expects(:call_without_timer).with(fake_env)
30
+ subject.call(fake_env)
31
+ end
32
+
33
+ it "passes request with timer on index" do
34
+ fake_env = { 'PATH_INFO' => '/' }
35
+ subject.expects(:call_with_timer).with('index.render', fake_env)
36
+ subject.call(fake_env)
37
+ end
38
+
39
+ it "passes request with timer on full path" do
40
+ fake_env = { 'PATH_INFO' => '/somewhere/nice' }
41
+ subject.expects(:call_with_timer).with('somewhere.nice.render', fake_env)
42
+ subject.call(fake_env)
43
+ end
44
+
45
+ it "passes request onto app" do
46
+ envs = ['', '/', '/somewhere/nice'].each do |p|
47
+ fake_env = { 'PATH_INFO' => p }
48
+ subject.app.expects(:call).with(fake_env)
49
+ subject.call(fake_env)
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ describe "#generate_key" do
56
+
57
+ it "returns nil when applicable" do
58
+ fake_env = { 'PATH_INFO' => '' }
59
+ subject.generate_key(fake_env).should be_nil
60
+ end
61
+
62
+ it "returns index when root" do
63
+ fake_env = { 'PATH_INFO' => '/' }
64
+ subject.generate_key(fake_env).should == 'index.render'
65
+ end
66
+
67
+ it "returns dotted value" do
68
+ fake_env = { 'PATH_INFO' => '/somewhere/nice' }
69
+ subject.generate_key(fake_env).should == 'somewhere.nice.render'
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ describe "Sinatra Server with Middleware" do
77
+ include Rack::Test::Methods
78
+
79
+ def app
80
+ Sinatra.new do
81
+ set :environment, :test
82
+ use Fozzie::Rack::Middleware
83
+ get('/') { "echo" }
84
+ get('/somewhere/nice') { "echo" }
85
+ end
86
+ end
87
+
88
+ it "sends stats request on root" do
89
+ S.expects(:timing).with('index.render', any_parameters)
90
+ get '/'
91
+ last_response.should be_ok
92
+ last_response.body.should == 'echo'
93
+ end
94
+
95
+ it "sends stats request on nested path" do
96
+ S.expects(:timing).with('somewhere.nice.render', any_parameters)
97
+ get '/somewhere/nice'
98
+ last_response.should be_ok
99
+ last_response.body.should == 'echo'
100
+ end
101
+
102
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'action_controller'
3
+
4
+ describe Fozzie::Rails::Middleware do
5
+
6
+ subject do
7
+ RailsApp = Class.new do
8
+ def call(env)
9
+ env
10
+ end
11
+ end unless defined?(RailsApp)
12
+ Fozzie::Rails::Middleware.new RailsApp.new
13
+ end
14
+
15
+ describe "subject" do
16
+ it "returns env on call for testing" do
17
+ subject.call({}).should == {}
18
+ end
19
+ end
20
+
21
+ it "#generate_key" do
22
+ s = '/somewhere/railsy'
23
+ fake_env = { 'PATH_INFO' => s }
24
+ ActionController::Routing::Routes.expects(:recognize_path).with(s).returns({:controller => 'somewhere', :action => 'railsy'})
25
+ subject.generate_key(fake_env).should == 'somewhere.railsy.render'
26
+ end
27
+
28
+ end
@@ -5,18 +5,18 @@ describe Fozzie do
5
5
 
6
6
  it { should respond_to(:c) }
7
7
  it { should respond_to(:config) }
8
-
8
+
9
9
  it "has configuration" do
10
10
  Fozzie.config.should be_kind_of(Fozzie::Configuration)
11
11
  Fozzie.c.should be_kind_of(Fozzie::Configuration)
12
12
  end
13
-
13
+
14
14
  it "creates new classes for statistics gathering" do
15
15
  Fozzie::Classes::NAMESPACES.each do |k|
16
16
  Kernel.const_defined?(k).should == true
17
17
  end
18
18
  end
19
-
19
+
20
20
  it "acts like its inherited parent" do
21
21
  Fozzie::Classes::NAMESPACES.each do |k|
22
22
  kl = Kernel.const_get(k)
@@ -27,52 +27,52 @@ describe Fozzie do
27
27
  kl.should respond_to(:time)
28
28
  end
29
29
  end
30
-
30
+
31
31
  it "acts an a singleton" do
32
32
  Fozzie::Classes::NAMESPACES.each do |k|
33
33
  kl1, kl2 = Kernel.const_get(k), Kernel.const_get(k)
34
34
  kl1.should == kl2
35
35
  end
36
36
  end
37
-
37
+
38
38
  it "assigns namespace when passed" do
39
39
  Fozzie::AbstractFozzie.new(1,2, 'a').namespace.should == 'a'
40
40
  end
41
-
41
+
42
42
  it "times a given block" do
43
43
  Stats.expects(:timing).with() {|b, val, timing| b == 'data.bin' && (1000..1200).include?(val) }.twice
44
44
  Stats.time_for('data.bin') { sleep 1 }
45
45
  Stats.time_to_do('data.bin') { sleep 1 }
46
46
  end
47
-
47
+
48
48
  it "registers a commit" do
49
49
  Stats.expects(:timing).with('event.commit', anything).twice
50
50
  Stats.commit
51
51
  Stats.committed
52
52
  end
53
-
53
+
54
54
  it "registers a build" do
55
55
  Stats.expects(:timing).with('event.build', anything).twice
56
56
  Stats.build
57
57
  Stats.built
58
58
  end
59
-
59
+
60
60
  it "registers a deploy" do
61
61
  Stats.expects(:timing).with('event.deploy', anything).twice
62
62
  Stats.deploy
63
63
  Stats.deployed
64
64
  end
65
-
65
+
66
66
  it "ensures block is called on socket error" do
67
67
  UDPSocket.any_instance.stubs(:send).raises(SocketError)
68
68
  proc { Stats.time_for('data.bin') { sleep 1 } }.should_not raise_error
69
69
  proc { Stats.time_to_do('data.bin') { sleep 1 } }.should_not raise_error
70
70
  end
71
-
71
+
72
72
  it "raises exception if natural exception from block" do
73
73
  proc { Stats.time_for('data.bin') { raise ArgumentError, "testing" } }.should raise_error(ArgumentError)
74
74
  end
75
-
75
+
76
76
  it "only calls the block once on SocketError" do
77
77
  UDPSocket.any_instance.stubs(:send).raises(SocketError)
78
78
  i = 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fozzie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-06 00:00:00.000000000 Z
12
+ date: 2012-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: statsd-ruby
16
- requirement: &70172749402340 !ruby/object:Gem::Requirement
16
+ requirement: &70225957503840 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70172749402340
24
+ version_requirements: *70225957503840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70172749401920 !ruby/object:Gem::Requirement
27
+ requirement: &70225957670940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70172749401920
35
+ version_requirements: *70225957670940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70172749401240 !ruby/object:Gem::Requirement
38
+ requirement: &70225957670120 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70172749401240
46
+ version_requirements: *70225957670120
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mocha
49
- requirement: &70172749400660 !ruby/object:Gem::Requirement
49
+ requirement: &70225957669480 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,54 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70172749400660
57
+ version_requirements: *70225957669480
58
+ - !ruby/object:Gem::Dependency
59
+ name: syntax
60
+ requirement: &70225957668900 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70225957668900
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: &70225957668200 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70225957668200
58
80
  - !ruby/object:Gem::Dependency
59
81
  name: simplecov
60
- requirement: &70172749400240 !ruby/object:Gem::Requirement
82
+ requirement: &70225957667320 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70225957667320
91
+ - !ruby/object:Gem::Dependency
92
+ name: sinatra
93
+ requirement: &70225957664940 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70225957664940
102
+ - !ruby/object:Gem::Dependency
103
+ name: actionpack
104
+ requirement: &70225957664380 !ruby/object:Gem::Requirement
61
105
  none: false
62
106
  requirements:
63
107
  - - ! '>='
@@ -65,7 +109,7 @@ dependencies:
65
109
  version: '0'
66
110
  type: :development
67
111
  prerelease: false
68
- version_requirements: *70172749400240
112
+ version_requirements: *70225957664380
69
113
  description: Gem allows statistics gathering from Ruby and Ruby on Rails applications
70
114
  to Statsd
71
115
  email:
@@ -86,11 +130,16 @@ files:
86
130
  - lib/fozzie/classes.rb
87
131
  - lib/fozzie/config.rb
88
132
  - lib/fozzie/configuration.rb
133
+ - lib/fozzie/rack/middleware.rb
134
+ - lib/fozzie/rails/middleware.rb
135
+ - lib/fozzie/railtie.rb
89
136
  - lib/fozzie/version.rb
90
137
  - spec/config/fozzie.yml
91
138
  - spec/lib/core_ext/hash_spec.rb
92
139
  - spec/lib/fozzie/config_spec.rb
93
140
  - spec/lib/fozzie/configuration_spec.rb
141
+ - spec/lib/fozzie/rack/middleware_spec.rb
142
+ - spec/lib/fozzie/rails/middleware_spec.rb
94
143
  - spec/lib/fozzie/version_spec.rb
95
144
  - spec/lib/fozzie_spec.rb
96
145
  - spec/spec_helper.rb
@@ -108,7 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
157
  version: '0'
109
158
  segments:
110
159
  - 0
111
- hash: -3461329709865678557
160
+ hash: 2608603561307742535
112
161
  required_rubygems_version: !ruby/object:Gem::Requirement
113
162
  none: false
114
163
  requirements:
@@ -117,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
166
  version: '0'
118
167
  segments:
119
168
  - 0
120
- hash: -3461329709865678557
169
+ hash: 2608603561307742535
121
170
  requirements: []
122
171
  rubyforge_project: fozzie
123
172
  rubygems_version: 1.8.10
@@ -129,6 +178,8 @@ test_files:
129
178
  - spec/lib/core_ext/hash_spec.rb
130
179
  - spec/lib/fozzie/config_spec.rb
131
180
  - spec/lib/fozzie/configuration_spec.rb
181
+ - spec/lib/fozzie/rack/middleware_spec.rb
182
+ - spec/lib/fozzie/rails/middleware_spec.rb
132
183
  - spec/lib/fozzie/version_spec.rb
133
184
  - spec/lib/fozzie_spec.rb
134
185
  - spec/spec_helper.rb