rack-graphite 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+
7
+ group :test do
8
+ gem 'rspec', '~> 2.14'
9
+ gem 'rack-test'
10
+ gem 'sinatra'
11
+ gem 'debugger', :platform => :mri_19
12
+ gem 'debugger-pry', :platform => :mri_19
13
+ gem 'pry'
14
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Lookout
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ rack-graphite
2
+ =============
3
+
4
+ Rack middleware for automatically logging request information to Graphite.
5
+
6
+
7
+ This gem assumes that you're using
8
+ [lookout-statsd](https://github.com/lookout/statsd) and have already initalized
9
+ `Statsd.instance` in your environment before handling any requests.
10
+
11
+ By default this will log metrics such as:
12
+
13
+ * For a `GET /` request
14
+ * `requests.get.root.`
15
+ * `count`
16
+ * `lower`
17
+ * `mean`
18
+ * `upper`
19
+ * `upper_90`
20
+ * For a `GET /home` request
21
+ * `requests.get.home.`
22
+ * `count`
23
+ * `lower`
24
+ * `mean`
25
+ * `upper`
26
+ * `upper_90`
27
+ * For a `PUT /upload' request
28
+ * `requests.put.upload.`
29
+ * `count`
30
+ * `lower`
31
+ * `mean`
32
+ * `upper`
33
+ * `upper_90`
34
+ * For a `GET /user/login` request
35
+ * `requests.get.user.login.`
36
+ * `count`
37
+ * `lower`
38
+ * `mean`
39
+ * `upper`
40
+ * `upper_90`
41
+
42
+ ## Usage
43
+
44
+ **In Sinatra**
45
+
46
+ require 'rack/graphite'
47
+
48
+ class MyApp < Sinatra::Base
49
+ use Rack::Graphite
50
+
51
+ get '/' do
52
+ 'Hello!'
53
+ end
54
+ end
55
+
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,35 @@
1
+ require 'statsd'
2
+
3
+ module Rack
4
+ class Graphite
5
+ VERSION = '1.0.0'
6
+ PREFIX = 'requests'
7
+
8
+ def initialize(app, options={})
9
+ @app = app
10
+ @prefix = options[:prefix] || PREFIX
11
+ end
12
+
13
+ def call(env)
14
+ path = env['PATH_INFO'] || '/'
15
+ method = env['REQUEST_METHOD'] || 'GET'
16
+ metric = path_to_graphite(method, path)
17
+
18
+ result = nil
19
+ Statsd.instance.timing(metric) do
20
+ result = @app.call(env)
21
+ end
22
+ return result
23
+ end
24
+
25
+ def path_to_graphite(method, path)
26
+ method = method.downcase
27
+ if (path.nil?) || (path == '/') || (path.empty?)
28
+ "#{@prefix}.#{method}.root"
29
+ else
30
+ path = path.gsub('/', '.')
31
+ "#{@prefix}.#{method}#{path}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -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 'rack/graphite'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-graphite"
8
+ spec.version = Rack::Graphite::VERSION
9
+ spec.authors = ["R. Tyler Croy"]
10
+ spec.email = ["rtyler.croy@lookout.com"]
11
+ spec.description = "Simple Rack middleware for logging request counts/timing information"
12
+ spec.summary = "Simple Rack middleware for logging request counts/timing information"
13
+ spec.homepage = "https://github.com/lookout/rack-graphite"
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
+
23
+ spec.add_dependency 'lookout-statsd'
24
+ end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Graphite do
4
+ let(:statsd) { double('Mock Statsd::Client') }
5
+
6
+ before :each do
7
+ Statsd.stub(:instance).and_return(statsd)
8
+ end
9
+
10
+
11
+ describe '#path_to_graphite' do
12
+ let(:middleware) { described_class.new(nil) }
13
+ subject(:graphite) { middleware.path_to_graphite(method, path) }
14
+
15
+ context 'GET requests' do
16
+ let(:method) { 'GET' }
17
+
18
+ context 'with a / URL' do
19
+ let(:path) { '/' }
20
+ it { should eql('requests.get.root') }
21
+ end
22
+
23
+ context 'with a onelevel URL' do
24
+ let(:path) { '/onelevel' }
25
+ it { should eql('requests.get.onelevel') }
26
+ end
27
+
28
+ context 'with a twolevel URL' do
29
+ let(:path) { '/two/level' }
30
+ it { should eql('requests.get.two.level') }
31
+ end
32
+
33
+ context 'with an empty URL' do
34
+ let(:path) { '' }
35
+ it { should eql('requests.get.root') }
36
+ end
37
+
38
+ context 'with a nil URL' do
39
+ let(:path) { nil }
40
+ it { should eql('requests.get.root') }
41
+ end
42
+ end
43
+ end
44
+
45
+
46
+ context 'with a fake app' do
47
+ let(:app) { double('Mock Rack App') }
48
+ subject(:middleware) { described_class.new(app) }
49
+
50
+ before :each do
51
+ # Stub out timing by default to and just yield
52
+ statsd.stub(:timing).and_yield
53
+ end
54
+
55
+ describe '#call' do
56
+ let(:env) { {:test => 'rspec'} }
57
+
58
+ before :each do
59
+ # Stub out by default for all tests
60
+ app.stub(:call)
61
+ end
62
+
63
+ it 'should propogate the invocation to the app' do
64
+ app.should_receive(:call).with(env)
65
+ middleware.call(env)
66
+ end
67
+
68
+ it 'should return the result of the propogated app.call' do
69
+ result = double('Mock Rack Response')
70
+ app.should_receive(:call).and_return(result)
71
+ expect(middleware.call({})).to eql(result)
72
+ end
73
+
74
+ it 'should invoke a timer' do
75
+ statsd.should_receive(:timing)
76
+ middleware.call({})
77
+ end
78
+ end
79
+ end
80
+
81
+ context 'with a simple Sinatra app', :type => :integration do
82
+ let(:app) { TestApp }
83
+
84
+ subject(:response) { last_response }
85
+
86
+ context 'with a root request' do
87
+ before :each do
88
+ statsd.should_receive(:timing).with('requests.get.root').and_yield
89
+ get '/'
90
+ end
91
+ its(:status) { should eql(200) }
92
+ end
93
+
94
+ context 'with a request with query params' do
95
+ before :each do
96
+ statsd.should_receive(:timing).with('requests.get.onelevel').and_yield
97
+ get '/onelevel?q=foo'
98
+ end
99
+ its(:status) { should eql(200) }
100
+ end
101
+
102
+ context 'with a PUT request' do
103
+ before :each do
104
+ statsd.should_receive(:timing).with('requests.put.onelevel').and_yield
105
+ put '/onelevel'
106
+ end
107
+ its(:status) { should eql(200) }
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,38 @@
1
+ $LOAD_PATH.unshift(File.expand_path(__FILE__ + '/../lib'))
2
+
3
+ require 'rack/graphite'
4
+ require 'rack/test'
5
+
6
+ unless RUBY_PLATFORM == 'java'
7
+ # Only require the debugger on MRI
8
+ require 'debugger'
9
+ require 'debugger/pry'
10
+ end
11
+
12
+ RSpec.configure do |c|
13
+ c.include(Rack::Test::Methods, :type => :integration)
14
+ c.before(:all, :type => :integration) do
15
+ end
16
+ end
17
+
18
+
19
+ require 'sinatra/base'
20
+ class TestApp < Sinatra::Base
21
+ use Rack::Graphite
22
+
23
+ get '/' do
24
+ 'Hello'
25
+ end
26
+
27
+ put '/onelevel' do
28
+ 'Thanks'
29
+ end
30
+
31
+ get '/onelevel' do
32
+ 'Hello One Level'
33
+ end
34
+
35
+ get '/two/levels' do
36
+ 'Hello Two Levels'
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-graphite
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - R. Tyler Croy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: lookout-statsd
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ description: Simple Rack middleware for logging request counts/timing information
47
+ email:
48
+ - rtyler.croy@lookout.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/rack/graphite.rb
59
+ - rack-graphite.gemspec
60
+ - spec/graphite_spec.rb
61
+ - spec/spec_helper.rb
62
+ homepage: https://github.com/lookout/rack-graphite
63
+ licenses:
64
+ - MIT
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: -1834689501216258839
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ segments:
85
+ - 0
86
+ hash: -1834689501216258839
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.25
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Simple Rack middleware for logging request counts/timing information
93
+ test_files:
94
+ - spec/graphite_spec.rb
95
+ - spec/spec_helper.rb