rack-request-profiler 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  .rspec
2
2
  Gemfile.lock
3
+ pkg
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ree
6
+ - ruby-head
7
+ - rbx
data/Gemfile CHANGED
@@ -1,9 +1,13 @@
1
1
  source :rubygems
2
2
 
3
+ gem 'rake'
4
+
3
5
  group :test do
4
6
  gem 'em-stathat'
7
+ gem 'mocha'
5
8
  gem 'rack'
6
9
  gem 'rspec', '>= 2.7'
7
10
  gem 'rack-test', :require => 'rack/test'
11
+ gem 'sinatra', :require => 'sinatra/base'
8
12
  gem 'statsd-ruby', :require => 'statsd'
9
13
  end
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](http://travis-ci.org/ajsharp/rack-request-profiler.png)](http://travis-ci.org/ajsharp/rack-request-profiler)
2
+
1
3
  This project provides a simple "framework" for profiling request / response times, and sending the data to another service. It includes a base class `Rack::RequestProfiler` for handling the logic of wrapping and timing the request / response cycle in a rack app.
2
4
 
3
5
  By default, the `Rack::RequestProfiler` middleware does not do anything with the profiling data. Instead, this logic must be implemented by subclasses by defining the `handle_results` instance method. For example, you might send profiling data to an external web service (stathat, papertrail, loggly, etc), statsd, write to a logfile on disk, put it in a persistent store like redis or mongo, or really anything else your heart desires.
@@ -0,0 +1,4 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec)
3
+
4
+ task :default => :spec
@@ -27,7 +27,9 @@ module Rack
27
27
 
28
28
  status, headers, body = @app.call(env)
29
29
 
30
- handle_results(env, request)
30
+ unless status == 404
31
+ handle_results(env, request)
32
+ end
31
33
  [status, headers, body]
32
34
  end
33
35
 
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class RequestProfiler
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -4,11 +4,16 @@ describe Rack::Profilers::Stathat do
4
4
  it "accepts callbacks" do
5
5
  EM.run {
6
6
  app = Rack::Builder.app do
7
- use Rack::Profilers::Stathat, :callback => lambda { |req| puts("hey"); EM.stop }
7
+ use Rack::Profilers::Stathat, :callback => lambda { |req| }
8
8
 
9
9
  run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['']]}
10
10
  end
11
- req = Rack::MockRequest.new(app).get('/')
11
+
12
+ deferrable = mock("Deferrable")
13
+ deferrable.expects(:callback).returns(EM.stop)
14
+ EM::StatHat.any_instance.expects(:ez_value).returns(deferrable)
15
+
16
+ Rack::MockRequest.new(app).get('/zomg/this/is/a/test')
12
17
  }
13
18
  end
14
19
  end
@@ -2,26 +2,38 @@ require 'spec_helper'
2
2
 
3
3
  describe Rack::Profilers::Statsd do
4
4
  include Rack::Test::Methods
5
- $statsd = Statsd.new 'localhost'
6
5
 
7
6
  it "sends stuff to statsd" do
7
+ statsd = Statsd.new 'localhost'
8
8
  app = Rack::Builder.app do
9
- use Rack::Profilers::Statsd, $statsd
9
+ use Rack::Profilers::Statsd, statsd
10
10
  run lambda { |env| [200, {}, ['']]}
11
11
  end
12
12
 
13
- obj = mock("object", :timing => true)
14
- $statsd.should_receive(:timing)
13
+ statsd.expects(:timing)
15
14
  response = Rack::MockRequest.new(app).get('/')
16
15
  end
17
16
 
18
17
  it "does not send stuff for ignored paths" do
18
+ statsd = Statsd.new 'localhost'
19
19
  app = Rack::Builder.app do
20
- use Rack::Profilers::Statsd, $statsd, :ignore_path => /ignore-me/
20
+ use Rack::Profilers::Statsd, statsd, :ignore_path => /ignore-me/
21
21
  run lambda { |env| [200, {}, ['']]}
22
22
  end
23
23
 
24
- $statsd.should_not_receive(:timing)
24
+ statsd.expects(:timing).never
25
25
  Rack::MockRequest.new(app).get('/ignore-me')
26
26
  end
27
+
28
+ it "prepends the namespace if passed in" do
29
+ statsd = Statsd.new 'localhost'
30
+ app = Rack::Builder.app do
31
+ use Rack::Profilers::Statsd, statsd, :namespace => 'namespace.me.'
32
+ run lambda { |env| [200, {}, ['']]}
33
+ end
34
+
35
+ Rack::Profilers::Statsd.any_instance.stubs(:run_time => 200)
36
+ statsd.expects(:timing).with('namespace.me.GET.', 200)
37
+ Rack::MockRequest.new(app).get('/')
38
+ end
27
39
  end
@@ -2,34 +2,39 @@ require 'spec_helper'
2
2
 
3
3
  describe Rack::RequestProfiler do
4
4
  include Rack::Test::Methods
5
+ class MockApp < Sinatra::Base
6
+ use Rack::RequestProfiler, :ignore_path => /ignore_me/
7
+ get '/' do
8
+ [200, "Hello, world"]
9
+ end
5
10
 
6
- it "implements the handle_results interface" do
7
- Rack::RequestProfiler.new(lambda {}).should respond_to :handle_results
11
+ get '/ignore_me' do
12
+ [200, "Ignore me"]
13
+ end
8
14
  end
9
15
 
10
- it "invokes the handle_results interface" do
11
- class Rack::CustomProfiler < Rack::RequestProfiler
12
- def handle_results(env)
13
- end
16
+ def app
17
+ @app ||= Rack::Builder.app do
18
+ run MockApp
14
19
  end
15
- app = Rack::Builder.app do
16
- use Rack::CustomProfiler
20
+ end
17
21
 
18
- run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['']]}
19
- end
22
+ it "implements the handle_results interface" do
23
+ Rack::RequestProfiler.new(lambda {}).should respond_to :handle_results
24
+ end
20
25
 
21
- Rack::CustomProfiler.any_instance.should_receive(:handle_results)
26
+ it "invokes the handle_results interface" do
27
+ Rack::RequestProfiler.any_instance.expects(:handle_results).once
22
28
  Rack::MockRequest.new(app).get('/')
23
29
  end
24
30
 
25
31
  it "does not invoke handle_results if ignore_path matches" do
26
- app = Rack::Builder.app do
27
- use Rack::RequestProfiler, :ignore_path => /ignore_me/
28
-
29
- run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['']]}
30
- end
31
-
32
- Rack::RequestProfiler.any_instance.should_not_receive(:handle_results)
32
+ Rack::RequestProfiler.any_instance.expects(:handle_results).never
33
33
  Rack::MockRequest.new(app).get('/ignore_me')
34
34
  end
35
+
36
+ it "does not invoke handlers if the status is 404" do
37
+ Rack::RequestProfiler.any_instance.expects(:handle_results).never
38
+ Rack::MockRequest.new(app).get('/does/not/exist')
39
+ end
35
40
  end
@@ -4,6 +4,7 @@ require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'rack-r
4
4
 
5
5
  require 'rack/test'
6
6
  require 'em-stathat'
7
+ require 'sinatra/base'
7
8
 
8
9
  EventMachine::StatHat.config do |c|
9
10
  c.ukey = 'key'
@@ -11,4 +12,5 @@ EventMachine::StatHat.config do |c|
11
12
  end
12
13
 
13
14
  RSpec.configure do |config|
15
+ config.mock_with :mocha
14
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-request-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-09 00:00:00.000000000 Z
12
+ date: 2012-02-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Provides a framework for sending wall time statistics to external services,
15
15
  such as statsd, stathat, etc.
@@ -20,8 +20,10 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - .gitignore
23
+ - .travis.yml
23
24
  - Gemfile
24
25
  - README.md
26
+ - Rakefile
25
27
  - lib/rack-request-profiler.rb
26
28
  - lib/rack/profilers/stathat.rb
27
29
  - lib/rack/profilers/statsd.rb
@@ -54,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
56
  version: '0'
55
57
  requirements: []
56
58
  rubyforge_project:
57
- rubygems_version: 1.8.10
59
+ rubygems_version: 1.8.15
58
60
  signing_key:
59
61
  specification_version: 3
60
62
  summary: Rack middleware for profiling request / response cycles.