fisher-rack 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/fisher-rack.gemspec +26 -0
- data/lib/fisher/rack.rb +7 -0
- data/lib/fisher/rack/middleware.rb +108 -0
- data/lib/fisher/rack/version.rb +5 -0
- metadata +119 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Trae Robrock
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Fisher::Rack
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'fisher-rack'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install fisher-rack
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/fisher-rack.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fisher/rack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fisher-rack"
|
8
|
+
spec.version = Fisher::Rack::VERSION
|
9
|
+
spec.authors = ["Trae Robrock"]
|
10
|
+
spec.email = ["trobrock@fisherapp.com"]
|
11
|
+
spec.description = %q{Simple piece of rack middleware to track stats}
|
12
|
+
spec.summary = %q{Simple piece of rack middleware to track stats}
|
13
|
+
spec.homepage = "https://github.com/fisherapp/fisher-rack"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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_dependency "rack"
|
22
|
+
spec.add_dependency "ruby-statsd", "~> 1.2.1"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
data/lib/fisher/rack.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
module Fisher
|
2
|
+
module Rack
|
3
|
+
class Middleware
|
4
|
+
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
|
5
|
+
VALID_METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'].freeze
|
6
|
+
|
7
|
+
# Initializes the middleware.
|
8
|
+
#
|
9
|
+
# app - The next Rack app in the pipeline.
|
10
|
+
# options - Hash of options.
|
11
|
+
# :stats - Optional StatsD client.
|
12
|
+
# :hostname - Optional String hostname. Set to nil
|
13
|
+
# to exclude.
|
14
|
+
# :stats_prefix - Optional String prefix for StatsD keys.
|
15
|
+
# Default: "rack"
|
16
|
+
def initialize(app, options = {})
|
17
|
+
@app = app
|
18
|
+
|
19
|
+
if @stats = options[:stats]
|
20
|
+
prefix = [options[:stats_prefix] || :rack]
|
21
|
+
if options.has_key?(:hostname)
|
22
|
+
prefix << options[:hostname].gsub(/\./, '_') unless options[:hostname].nil?
|
23
|
+
else
|
24
|
+
prefix << `hostname -s`.chomp.gsub(/\./, '_')
|
25
|
+
end
|
26
|
+
@stats_prefix = prefix.join(".")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# called immediately after a request to record statistics
|
31
|
+
def record_request(status, env)
|
32
|
+
now = Time.now
|
33
|
+
diff = (now - @start)
|
34
|
+
|
35
|
+
if @stats
|
36
|
+
@stats.timing("#{@stats_prefix}.response_time", diff * 1000)
|
37
|
+
if VALID_METHODS.include?(env[REQUEST_METHOD])
|
38
|
+
stat = "#{@stats_prefix}.response_time.#{env[REQUEST_METHOD].downcase}"
|
39
|
+
@stats.timing(stat, diff * 1000)
|
40
|
+
end
|
41
|
+
|
42
|
+
if suffix = status_suffix(status)
|
43
|
+
@stats.increment "#{@stats_prefix}.status_code.#{status_suffix(status)}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
rescue => e
|
47
|
+
warn "Middleware#record_request failed: #{e.inspect}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def status_suffix(status)
|
51
|
+
suffix = case status.to_i
|
52
|
+
when 200 then :ok
|
53
|
+
when 201 then :created
|
54
|
+
when 202 then :accepted
|
55
|
+
when 301 then :moved_permanently
|
56
|
+
when 302 then :found
|
57
|
+
when 303 then :see_other
|
58
|
+
when 304 then :not_modified
|
59
|
+
when 305 then :use_proxy
|
60
|
+
when 307 then :temporary_redirect
|
61
|
+
when 400 then :bad_request
|
62
|
+
when 401 then :unauthorized
|
63
|
+
when 402 then :payment_required
|
64
|
+
when 403 then :forbidden
|
65
|
+
when 404 then :missing
|
66
|
+
when 410 then :gone
|
67
|
+
when 422 then :invalid
|
68
|
+
when 500 then :error
|
69
|
+
when 502 then :bad_gateway
|
70
|
+
when 503 then :node_down
|
71
|
+
when 504 then :gateway_timeout
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Body wrapper. Yields to the block when body is closed. This is used to
|
76
|
+
# signal when a response is fully finished processing.
|
77
|
+
class Body
|
78
|
+
def initialize(body, &block)
|
79
|
+
@body = body
|
80
|
+
@block = block
|
81
|
+
end
|
82
|
+
|
83
|
+
def each(&block)
|
84
|
+
if @body.respond_to?(:each)
|
85
|
+
@body.each(&block)
|
86
|
+
else
|
87
|
+
block.call(@body)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def close
|
92
|
+
@body.close if @body.respond_to?(:close)
|
93
|
+
@block.call
|
94
|
+
nil
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Rack entry point.
|
99
|
+
def call(env)
|
100
|
+
@start = Time.now
|
101
|
+
|
102
|
+
status, headers, body = @app.call(env)
|
103
|
+
body = Body.new(body) { record_request(status, env) }
|
104
|
+
[status, headers, body]
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fisher-rack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Trae Robrock
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: ruby-statsd
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.2.1
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.2.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Simple piece of rack middleware to track stats
|
79
|
+
email:
|
80
|
+
- trobrock@fisherapp.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- fisher-rack.gemspec
|
91
|
+
- lib/fisher/rack.rb
|
92
|
+
- lib/fisher/rack/middleware.rb
|
93
|
+
- lib/fisher/rack/version.rb
|
94
|
+
homepage: https://github.com/fisherapp/fisher-rack
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.8.23
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: Simple piece of rack middleware to track stats
|
119
|
+
test_files: []
|