statsd-rack 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29bac7b67fdd0cc7d27b534e74c805a927a56e51
4
+ data.tar.gz: b274e6216e6ab549ceb0aa3b0bb9d6f7288f2a6a
5
+ SHA512:
6
+ metadata.gz: 11b925b517bfe92cdc7c69e2c293cb5d547016b9f9d98785d605454acf0e5bd13bef91e3cc5d25b26a220e803cbf21fc8cd109f30e954b39abef283d9265e06d
7
+ data.tar.gz: c33a89955c6d02a1678a5e7cb2c6172da0a9e3fc17ae634046f85268826fb97bce7f010ab25096843db25dedf80e6abb2e207e2cd90880f5bd8dd8fc61c6cba7
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in statsd-rack.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Raghuram Sreenath
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,50 @@
1
+ # Statsd::Rack
2
+
3
+ This gem initializes a global variable `$statsd` that can be used pump metrics to a statsd server. Inspired by [rack-statsd](https://github.com/github/rack-statsd).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'statsd-rack'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install statsd-rack
18
+
19
+ ## Usage - Padrino
20
+
21
+ Edit your app.rb file
22
+
23
+ ```ruby
24
+ module YourApp
25
+ class App < Padrino::Application
26
+ use StatsdRack::Rack "myapp" # Will namespace statsd metric with 'myapp'
27
+ ...
28
+ end
29
+ end
30
+ ```
31
+ ## Usage - Rails
32
+
33
+ Edit your config/application.rb file:
34
+
35
+ ```ruby
36
+ module YourApp
37
+ class Application < Rails::Application
38
+ config.middleware.use StatsdRack::Rack, 'yourapp'
39
+ ...
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,107 @@
1
+ require "statsd-rack/version"
2
+ require "statsd-ruby"
3
+
4
+ module StatsdRack
5
+
6
+ class Rack
7
+ REQUEST_METHOD = 'REQUEST_METHOD'.freeze
8
+ VALID_METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'].freeze
9
+
10
+ # Initializes the middleware
11
+ #
12
+ # prefix - String prefix for Statsd key.
13
+ # Defaults to 'rack'
14
+ #
15
+ def initialize(app, prefix=nil)
16
+ @app = app
17
+ stats_prefix = prefix || 'rack'
18
+ @track_gc = GC.respond_to?(:time)
19
+ if !$statsd
20
+ $statsd = ::Statsd.new('localhost', 9125).tap{|sd| sd.namespace = stats_prefix}
21
+ end
22
+ end
23
+
24
+ # called after request is processed.
25
+ def record_request(status, env)
26
+ now = Time.now
27
+ diff = (now - @start)
28
+
29
+ if $statsd
30
+ $statsd.timing("response_time", diff * 1000)
31
+ if VALID_METHODS.include?(env[REQUEST_METHOD])
32
+ stat = "response_time.#{env[REQUEST_METHOD].downcase}"
33
+ $statsd.timing(stat, diff * 1000)
34
+ end
35
+
36
+ if suffix = status_suffix(status)
37
+ $statsd.increment "status_code.#{status_suffix(status)}"
38
+ end
39
+ if @track_gc && GC.time > 0
40
+ $statsd.timing "gc.time", GC.time / 1000
41
+ $statsd.count "gc.collections", GC.collections
42
+ end
43
+ end
44
+
45
+ rescue => boom
46
+ warn "Statsd::Rack#record_request failed: #{boom}"
47
+ end
48
+
49
+ def status_suffix(status)
50
+ suffix = case status.to_i
51
+ when 200 then :ok
52
+ when 201 then :created
53
+ when 202 then :accepted
54
+ when 301 then :moved_permanently
55
+ when 302 then :found
56
+ when 303 then :see_other
57
+ when 304 then :not_modified
58
+ when 305 then :use_proxy
59
+ when 307 then :temporary_redirect
60
+ when 400 then :bad_request
61
+ when 401 then :unauthorized
62
+ when 402 then :payment_required
63
+ when 403 then :forbidden
64
+ when 404 then :missing
65
+ when 410 then :gone
66
+ when 422 then :invalid
67
+ when 500 then :error
68
+ when 502 then :bad_gateway
69
+ when 503 then :node_down
70
+ when 504 then :gateway_timeout
71
+ end
72
+ end
73
+
74
+ # Refer: https://github.com/github/rack-statsd/blob/master/lib/rack-statsd.rb
75
+ #
76
+ # Body wrapper. Yields to the block when body is closed. This is used to
77
+ # signal when a response is fully finished processing.
78
+ class Body
79
+ def initialize(body, &block)
80
+ @body = body
81
+ @block = block
82
+ end
83
+
84
+ def each(&block)
85
+ if @body.respond_to?(:each)
86
+ @body.each(&block)
87
+ else
88
+ block.call(@body)
89
+ end
90
+ end
91
+
92
+ def close
93
+ @body.close if @body.respond_to?(:close)
94
+ @block.call
95
+ nil
96
+ end
97
+ end
98
+
99
+ def call(env)
100
+ @start = Time.now
101
+ GC.clear_stats if @track_gc
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
@@ -0,0 +1,3 @@
1
+ module StatsdRack
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1 @@
1
+ require 'statsd-rack/rack'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'statsd-rack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "statsd-rack"
8
+ spec.version = StatsdRack::VERSION
9
+ spec.authors = ["Raghuram Sreenath"]
10
+ spec.email = ["raghums@gmail.com"]
11
+ spec.description = %q{Initialize a statsd client that is optionally namespaced. By default pumps request and GC metrics.}
12
+ spec.summary = %q{Initialize a statsd client that is optionally namespaced. By default pumps request and GC metrics.}
13
+ spec.homepage = "http://github.com/raghums/statsd-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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "statsd-ruby"
24
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statsd-rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Raghuram Sreenath
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-13 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: statsd-ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Initialize a statsd client that is optionally namespaced. By default
56
+ pumps request and GC metrics.
57
+ email:
58
+ - raghums@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/statsd-rack.rb
69
+ - lib/statsd-rack/rack.rb
70
+ - lib/statsd-rack/version.rb
71
+ - statsd-rack.gemspec
72
+ homepage: http://github.com/raghums/statsd-rack
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Initialize a statsd client that is optionally namespaced. By default pumps
96
+ request and GC metrics.
97
+ test_files: []