rack_heroku 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack_heroku (0.1.0)
5
+ rack
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.3)
11
+ rack (1.5.2)
12
+ rack-test (0.6.2)
13
+ rack (>= 1.0)
14
+ rake (10.0.4)
15
+ rspec (2.13.0)
16
+ rspec-core (~> 2.13.0)
17
+ rspec-expectations (~> 2.13.0)
18
+ rspec-mocks (~> 2.13.0)
19
+ rspec-core (2.13.1)
20
+ rspec-expectations (2.13.0)
21
+ diff-lcs (>= 1.1.3, < 2.0)
22
+ rspec-mocks (2.13.1)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler
29
+ rack-test
30
+ rack_heroku!
31
+ rake
32
+ rspec
@@ -0,0 +1,43 @@
1
+ = Rack Middleware for Heroku
2
+
3
+ This Gem contains Heroku specific middleware:
4
+
5
+ * Rack::Heroku::Profiler - Profiles Heroku HTTP Routing Queue Wait
6
+ and attaches a X-Heroku-Queue-Wait header to the response
7
+ * Rack::Heroku::Info - Returns stats and information about the
8
+ Heroku dyno
9
+
10
+ === Use
11
+
12
+ gem install rack_heroku
13
+
14
+ In your app:
15
+
16
+ require 'rack'
17
+ require 'rack/heroku'
18
+
19
+ use Rack::Heroku::Profiler
20
+ run MyApp
21
+
22
+ === Licence
23
+
24
+ Copyright (c) 2013 Black Square Media
25
+
26
+ Permission is hereby granted, free of charge, to any person obtaining
27
+ a copy of this software and associated documentation files (the
28
+ "Software"), to deal in the Software without restriction, including
29
+ without limitation the rights to use, copy, modify, merge, publish,
30
+ distribute, sublicense, and/or sell copies of the Software, and to
31
+ permit persons to whom the Software is furnished to do so, subject to
32
+ the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be
35
+ included in all copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
38
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
40
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
41
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
42
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
43
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ desc 'Default: run specs.'
7
+ task :default => :spec
@@ -0,0 +1,10 @@
1
+ require 'rack'
2
+
3
+ module Rack::Heroku
4
+ end
5
+
6
+ %w|profiler info|.each do |name|
7
+ require "rack/heroku/#{name}"
8
+ end
9
+
10
+ require 'rack/heroku/railtie' if defined?(Rails)
@@ -0,0 +1,19 @@
1
+ # Responds with 204 on :path
2
+ class Rack::Heroku::Info
3
+ HEADERS = { "Content-Type" => "text/plain" }.freeze
4
+
5
+ def initialize(app, path = "/_heroku/info")
6
+ @app = app
7
+ @path = path
8
+ end
9
+
10
+ def call(env)
11
+ case env["PATH_INFO"]
12
+ when @path
13
+ [200, HEADERS, [""]] # TODO: Return something interesting
14
+ else
15
+ @app.call(env)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,25 @@
1
+ # Appends a X-Heroku-Queue-Wait header to the response
2
+ class Rack::Heroku::Profiler
3
+ VALS_RANGE = (0..120_000).freeze
4
+ REQ_HEADER = "HTTP_X_REQUEST_START".freeze
5
+ RES_HEADER = "X-Heroku-Queue-Wait".freeze
6
+
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ ms = time_since env[REQ_HEADER]
13
+ status, headers, body = @app.call(env)
14
+ headers.update RES_HEADER => ms.to_s if ms && VALS_RANGE.include?(ms)
15
+
16
+ [status, headers, body]
17
+ end
18
+
19
+ private
20
+
21
+ def time_since(start)
22
+ (Time.now.to_f * 1_000).to_i - start.to_i if start
23
+ end
24
+
25
+ end
@@ -0,0 +1,8 @@
1
+ class Rack::Heroku::Railtie < Rails::Railtie
2
+
3
+ initializer "rack_heroku.middleware" do |app|
4
+ app.config.middleware.insert_before ActionDispatch::Static, Rack::Heroku::Profiler
5
+ app.config.middleware.insert_after ActionDispatch::DebugExceptions, Rack::Heroku::Info
6
+ end
7
+
8
+ end
@@ -0,0 +1 @@
1
+ require 'rack/heroku'
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.platform = Gem::Platform::RUBY
3
+ s.required_ruby_version = '>= 1.9.0'
4
+ s.required_rubygems_version = ">= 1.3.6"
5
+
6
+ s.name = "rack_heroku"
7
+ s.summary = "Rack middleware for Heroku"
8
+ s.version = "0.1.0"
9
+ s.license = "MIT"
10
+
11
+ s.authors = ["Dimitrij Denissenko"]
12
+ s.email = "dimitrij@blacksquaremedia.com"
13
+ s.homepage = "https://github.com/bsm/rack_heroku"
14
+
15
+ s.require_path = 'lib'
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- spec/*`.split("\n")
18
+
19
+ s.add_dependency "rack"
20
+ s.add_development_dependency "rake"
21
+ s.add_development_dependency "bundler"
22
+ s.add_development_dependency "rack-test"
23
+ s.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Heroku::Info do
4
+
5
+ it 'should ignore non-matching paths' do
6
+ get "/"
7
+ last_response.body.should == "OK"
8
+
9
+ get "/other"
10
+ last_response.body.should == "OK"
11
+ end
12
+
13
+ it 'should return info' do
14
+ get "/_heroku/info"
15
+ last_response.status.should == 200
16
+ last_response.body.should == ""
17
+ end
18
+
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Heroku::Profiler do
4
+
5
+ it 'should skip when header not set' do
6
+ get "/"
7
+ last_response.headers["X-Heroku-Queue-Wait"].should be_nil
8
+ end
9
+
10
+ it 'should skip when header is invalid' do
11
+ get "/", {}, "HTTP_X_REQUEST_START" => (Time.now.to_f * 1000 + 21).floor
12
+ last_response.headers["X-Heroku-Queue-Wait"].should be_nil
13
+ end
14
+
15
+ it 'should calculate queue delay' do
16
+ get "/", {}, "HTTP_X_REQUEST_START" => (Time.now.to_f * 1000 - 21).floor
17
+ last_response.headers["X-Heroku-Queue-Wait"].to_i.should be_within(1).of(21)
18
+ end
19
+
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'rspec'
2
+ require 'rack/test'
3
+ require 'rack/heroku'
4
+
5
+ module Rack::Heroku::TestMethods
6
+
7
+ def inner_app
8
+ @inner_app ||= ->env { [200, {'Content-Type' => 'text/plain'}, ['OK']] }
9
+ end
10
+
11
+ def app
12
+ described_class.new inner_app
13
+ end
14
+
15
+ end
16
+
17
+ RSpec.configure do |c|
18
+ c.include Rack::Test::Methods
19
+ c.include Rack::Heroku::TestMethods
20
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_heroku
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Dimitrij Denissenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ prerelease: false
16
+ type: :runtime
17
+ name: rack
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: '0'
24
+ requirement: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ prerelease: false
32
+ type: :development
33
+ name: rake
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirement: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ prerelease: false
48
+ type: :development
49
+ name: bundler
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirement: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ prerelease: false
64
+ type: :development
65
+ name: rack-test
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirement: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ prerelease: false
80
+ type: :development
81
+ name: rspec
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirement: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description:
95
+ email: dimitrij@blacksquaremedia.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files: []
99
+ files:
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - README.rdoc
103
+ - Rakefile
104
+ - lib/rack/heroku.rb
105
+ - lib/rack/heroku/info.rb
106
+ - lib/rack/heroku/profiler.rb
107
+ - lib/rack/heroku/railtie.rb
108
+ - lib/rack_heroku.rb
109
+ - rack_heroku.gemspec
110
+ - spec/rack/heroku/info_spec.rb
111
+ - spec/rack/heroku/profiler_spec.rb
112
+ - spec/spec_helper.rb
113
+ homepage: https://github.com/bsm/rack_heroku
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 1.9.0
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: 1.3.6
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.25
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Rack middleware for Heroku
138
+ test_files:
139
+ - spec/rack/heroku/info_spec.rb
140
+ - spec/rack/heroku/profiler_spec.rb
141
+ - spec/spec_helper.rb
142
+ has_rdoc: