rack-useful_procline 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5789281f880a804629b7efb859f4873b88a2149a
4
+ data.tar.gz: 0991ec2800518435f9413a7e2bb1b62342432050
5
+ SHA512:
6
+ metadata.gz: c08f84c878b7016cfaa61f16cbb20d02491e33d87a133b34ad3092bb433bd72bb8f117248575bb1c52066232e9766d873cb995c9dbf0865825f2ec5adf350731
7
+ data.tar.gz: 6ae5560e7f5eaf1ad12ddc72d2c48d70c94d0a30e787de911c96318863fc3246e5e49f79dda1a605a22a62ec73b00e1e15ae240ecb3e4cf8945f8e1b731fe350
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-useful_procline.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Aaron Wallis
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,63 @@
1
+ # Rack::UsefulProcline
2
+
3
+ Make your proclines informative with some lovely middleware.
4
+
5
+ Instead of
6
+
7
+ ```
8
+ unicorn master -p 5200
9
+ unicorn worker[0] -p 5200
10
+ ```
11
+
12
+ See this
13
+
14
+ ```
15
+ unicorn master -p 5200
16
+ unicorn worker[0] : last status: 200. last request_uuid: 53ebb737. Waiting for req.
17
+ unicorn worker[0] : GET : /all/the/things : cb18e336
18
+ ```
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'rack-useful_procline'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install rack-useful_procline
35
+
36
+ ## Usage
37
+
38
+ ### Configure
39
+
40
+ Default config is:
41
+
42
+ ```ruby
43
+ Rack::UsefulProcline.configure do |config|
44
+ config.opts[:request_uuid_key] = 'action_dispatch.request_id'
45
+ config.opts[:show_request_uuid] = true
46
+ config.opts[:show_request_method] = true
47
+ config.opts[:show_request_path] = true
48
+ end
49
+ ```
50
+
51
+ ### Use
52
+
53
+ ```ruby
54
+ use Rack::UsefulProcline
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( https://github.com/[my-github-username]/rack-useful_procline/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,95 @@
1
+ require 'rack'
2
+
3
+ module Rack
4
+ class UsefulProcline
5
+ VERSION = "0.0.1"
6
+
7
+ class << self
8
+ attr_accessor :config
9
+
10
+ def configure
11
+ @config ||= Config.new
12
+ yield(config) if block_given?
13
+ @config
14
+ end
15
+ end
16
+
17
+ class Config
18
+ attr_accessor :opts
19
+
20
+ def initialize
21
+ @opts = {
22
+ request_uuid_key: 'action_dispatch.request_id', # Rails' default
23
+ show_request_uuid: true,
24
+ show_request_method: true,
25
+ show_request_path: true
26
+ }
27
+ end
28
+ end
29
+
30
+ attr_accessor :app, :config
31
+
32
+ def initialize(app)
33
+ @app = app
34
+ @config = Rack::UsefulProcline.configure
35
+ end
36
+
37
+ # Public
38
+ def call(env)
39
+ prefix = $0.split(']')[0] + "] "
40
+ procline = prefix.dup
41
+ procline = procline_before_request(procline, env)
42
+
43
+ set_procline(procline[0..200])
44
+
45
+ status, headers, body = @app.call(env)
46
+
47
+ procline = prefix.dup
48
+ procline = procline_after_request(procline, env, status)
49
+ set_procline(procline)
50
+
51
+ [status, headers, body]
52
+ end
53
+
54
+ def set_procline(value)
55
+ $0 = sanitize_utf8(value)
56
+ end
57
+
58
+ def sanitize_utf8(string)
59
+ return string.force_encoding('utf-8') if string.valid_encoding?
60
+ string.chars.select(&:valid_encoding?).join.force_encoding('utf-8')
61
+ end
62
+
63
+ def procline_before_request(procline, env)
64
+ if @config.opts[:show_request_method]
65
+ procline.concat(" : #{env['REQUEST_METHOD']}")
66
+ end
67
+
68
+ if @config.opts[:show_request_path]
69
+ procline.concat(" : #{env['REQUEST_PATH']}")
70
+ end
71
+
72
+ if @config.opts[:show_request_uuid]
73
+ request_uuid = request_uuid_from(env) || "No request uuid found"
74
+ procline.concat(" : #{request_uuid_from(env)}")
75
+ end
76
+
77
+ procline
78
+ end
79
+
80
+ def request_uuid_from(env)
81
+ request_uuid = env[@config.opts[:request_uuid_key]]
82
+ request_uuid = request_uuid[0..7] if request_uuid
83
+ request_uuid
84
+ end
85
+
86
+ def procline_after_request(procline, env, status)
87
+ procline.concat(": last status: #{status}.")
88
+ if @config.opts[:show_request_uuid]
89
+ request_uuid = request_uuid_from(env) || "No request uuid found"
90
+ procline += " last request_uuid: #{request_uuid}."
91
+ end
92
+ procline += " Waiting for req."
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/useful_procline'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-useful_procline"
8
+ spec.version = Rack::UsefulProcline::VERSION
9
+ spec.authors = ["Aaron Wallis"]
10
+ spec.email = ["awallis@bleacherreport.com"]
11
+ spec.summary = %q{Modify Unicorn's procline to show useful information}
12
+ spec.description = %q{Rack middleware to modify Unicorn's procline to display request method, path and uuid during the request.}
13
+ spec.homepage = "https://github.com/wheels/rack-useful_procline"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "rack", "~> 1.5.2"
25
+ end
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::UsefulProcline do
4
+ it 'has a version number' do
5
+ expect(Rack::UsefulProcline::VERSION).not_to be nil
6
+ end
7
+
8
+ describe 'on initialization' do
9
+ subject { Rack::UsefulProcline.new(rack_app) }
10
+ end
11
+ end
12
+
13
+ describe Rack::UsefulProcline::Config do
14
+ describe 'default config' do
15
+ subject do
16
+ Rack::UsefulProcline.new(rack_app).config
17
+ end
18
+
19
+ it { expect(subject.opts[:request_uuid_key]).to eql('action_dispatch.request_id') }
20
+ it { expect(subject.opts[:show_request_uuid]).to be true }
21
+ it { expect(subject.opts[:show_request_method]).to be true }
22
+ it { expect(subject.opts[:show_request_path]).to be true }
23
+ end
24
+
25
+ describe 'modified config' do
26
+ before(:each) do
27
+ Rack::UsefulProcline.configure do |config|
28
+ config.opts[:request_uuid_key] = 'my_request_uuid_key'
29
+ config.opts[:show_request_uuid] = false
30
+ config.opts[:show_request_method] = false
31
+ config.opts[:show_request_path] = false
32
+ end
33
+ end
34
+
35
+ subject do
36
+ Rack::UsefulProcline.new(rack_app).config
37
+ end
38
+
39
+ it { expect(subject.opts[:request_uuid_key]).to eql('my_request_uuid_key') }
40
+ it { expect(subject.opts[:show_request_uuid]).to be false }
41
+ it { expect(subject.opts[:show_request_method]).to be false }
42
+ it { expect(subject.opts[:show_request_path]).to be false }
43
+ end
44
+
45
+ end
46
+
47
+ describe Rack::UsefulProcline do
48
+ subject { Rack::UsefulProcline.new(rack_app) }
49
+
50
+ describe '#call' do
51
+ subject do
52
+ Rack::UsefulProcline.new(rack_app).call(rack_env)
53
+ end
54
+
55
+ it { expect(subject[0]).to eql("200") }
56
+ it { expect(subject[1]).to eql({'Content-Type' => 'text/html'}) }
57
+ it { expect(subject[2]).to eql(['A Rack app!']) }
58
+ end
59
+
60
+ describe '#sanitize_utf8' do
61
+ # not sure what to do here
62
+ end
63
+
64
+ describe '#set_procline' do
65
+ # or here
66
+ end
67
+
68
+ describe '#request_uuid_from' do
69
+ it 'should retrieve the truncated request uuid' do
70
+ env = {'action_dispatch.request_id' => 'my_request_uuid'}
71
+ expect(subject.request_uuid_from(env).length).to eql(8)
72
+ end
73
+ end
74
+
75
+ describe '#procline_before_request' do
76
+ subject do
77
+ Rack::UsefulProcline.new(rack_app).procline_before_request("", rack_env)
78
+ end
79
+
80
+ describe "with default config (everything true)" do
81
+ it { expect(subject).to include("GET") }
82
+ it { expect(subject).to include("/path/to/things") }
83
+ it { expect(subject).to include("12345678") }
84
+ end
85
+
86
+ describe "with show_request_method, show_request_uuid and show_request_path turned off" do
87
+ subject do
88
+ Rack::UsefulProcline.configure do |config|
89
+ config.opts[:show_request_method] = false
90
+ config.opts[:show_request_path] = false
91
+ config.opts[:show_request_uuid] = false
92
+ end
93
+ Rack::UsefulProcline.new(rack_app).procline_before_request("", rack_env)
94
+ end
95
+
96
+ it do
97
+ expect(subject).not_to include("GET")
98
+ expect(subject).not_to include("/path/to/things")
99
+ expect(subject).not_to include("12345678")
100
+ end
101
+ end
102
+
103
+ describe "with custom request_uuid_key" do
104
+ subject do
105
+ key = "my_request_uuid_key"
106
+ Rack::UsefulProcline.configure { |config| config.opts[:request_uuid_key] = key }
107
+ Rack::UsefulProcline.new(rack_app).procline_before_request("", rack_env(request_uuid_key: key))
108
+ end
109
+
110
+ it { expect(subject).to include("12345678") }
111
+ end
112
+ end
113
+
114
+ describe '#procline_after_request' do
115
+ before(:each) do
116
+ def create_subject(opts = {})
117
+ opts[:rack_app] ||= rack_app
118
+ opts[:rack_env] ||= rack_env
119
+ Rack::UsefulProcline.new(opts[:rack_app]).procline_after_request("", opts[:rack_env], "200")
120
+ end
121
+ end
122
+
123
+ describe "with default config (everything true)" do
124
+ subject { create_subject }
125
+
126
+ it do
127
+ expect(subject).not_to include("GET")
128
+ expect(subject).not_to include("/path/to/things")
129
+ expect(subject).to include("12345678")
130
+ expect(subject).to include("last status: 200.")
131
+ expect(subject).to include("Waiting for req.")
132
+ end
133
+ end
134
+
135
+ describe "with show_request_method, show_request_uuid and show_request_path turned off" do
136
+ subject do
137
+ Rack::UsefulProcline.configure do |config|
138
+ config.opts[:show_request_method] = false
139
+ config.opts[:show_request_path] = false
140
+ config.opts[:show_request_uuid] = false
141
+ end
142
+ create_subject
143
+ end
144
+
145
+ it do
146
+ expect(subject).not_to include("GET")
147
+ expect(subject).not_to include("/path/to/things")
148
+ expect(subject).not_to include("12345678")
149
+ expect(subject).to include("last status: 200.")
150
+ expect(subject).to include("Waiting for req.")
151
+ end
152
+ end
153
+
154
+ describe "with custom request uuid key" do
155
+ subject do
156
+ key = 'my_request_uuid_key'
157
+ Rack::UsefulProcline.configure { |config| config.opts[:request_uuid_key] = key }
158
+ env = rack_env(request_uuid_key: key)
159
+ create_subject(rack_env: env)
160
+ end
161
+
162
+ it { expect(subject).to include("12345678") }
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'rack'
3
+ require 'rack/useful_procline'
4
+
5
+ RSpec.configure do |config|
6
+ def rack_app(opts = {})
7
+ status = opts[:status] || '200'
8
+ content_type = opts[:content_type] || 'text/html'
9
+ body = opts[:body] || 'A Rack app!'
10
+
11
+ Proc.new do |env|
12
+ [status, {'Content-Type' => content_type}, [body]]
13
+ end
14
+ end
15
+
16
+ def rack_env(opts = {})
17
+ opts[:request_uuid_key] ||= 'action_dispatch.request_id'
18
+ {
19
+ opts[:request_uuid_key] => '12345678 longish string',
20
+ 'REQUEST_METHOD' => 'GET',
21
+ 'REQUEST_PATH' => '/path/to/things'
22
+ }
23
+ end
24
+
25
+ config.after(:each) do
26
+ Rack::UsefulProcline.config = nil
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-useful_procline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Wallis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.5.2
69
+ description: Rack middleware to modify Unicorn's procline to display request method,
70
+ path and uuid during the request.
71
+ email:
72
+ - awallis@bleacherreport.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/rack/useful_procline.rb
85
+ - rack-useful_procline.gemspec
86
+ - spec/rack/useful_procline_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/wheels/rack-useful_procline
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.14
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Modify Unicorn's procline to show useful information
112
+ test_files:
113
+ - spec/rack/useful_procline_spec.rb
114
+ - spec/spec_helper.rb