metriks-middleware 0.0.1

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 :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ metriks-middleware (0.0.1)
5
+ metriks (~> 0.9.9)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ atomic (1.0.1)
11
+ avl_tree (1.1.3)
12
+ hitimes (1.1.1)
13
+ metriks (0.9.9)
14
+ atomic (~> 1.0)
15
+ avl_tree (~> 1.1.2)
16
+ hitimes (~> 1.1)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ metriks-middleware!
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2012 Larry Marburger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ # Rack Middleware for [Metriks](https://github.com/eric/metriks)
2
+
3
+ Rack middleware to track throughput and response time with metriks.
@@ -0,0 +1,161 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def lib_path
16
+ "lib/#{ name.gsub('-', '/') }.rb"
17
+ end
18
+
19
+ def version
20
+ line = File.read(lib_path)[/^\s*VERSION\s*=\s*.*/]
21
+ line.match(/.*ERSION\s*=\s*['"](.*)['"]/)[1]
22
+ end
23
+
24
+ def date
25
+ Date.today.to_s
26
+ end
27
+
28
+ def rubyforge_project
29
+ name
30
+ end
31
+
32
+ def gemspec_file
33
+ "#{name}.gemspec"
34
+ end
35
+
36
+ def gem_file
37
+ "#{name}-#{version}.gem"
38
+ end
39
+
40
+ def replace_header(head, header_name)
41
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
42
+ end
43
+
44
+ #############################################################################
45
+ #
46
+ # Standard tasks
47
+ #
48
+ #############################################################################
49
+
50
+ task :default => :test
51
+
52
+ require 'rake/testtask'
53
+ Rake::TestTask.new(:test) do |test|
54
+ test.libs << 'lib' << 'test'
55
+ test.pattern = 'test/**/test_*.rb'
56
+ test.verbose = true
57
+ end
58
+
59
+ desc "Generate RCov test coverage and open in your browser"
60
+ task :coverage do
61
+ require 'rcov'
62
+ sh "rm -fr coverage"
63
+ sh "rcov test/test_*.rb"
64
+ sh "open coverage/index.html"
65
+ end
66
+
67
+ require 'rdoc/task'
68
+ Rake::RDocTask.new do |rdoc|
69
+ rdoc.rdoc_dir = 'rdoc'
70
+ rdoc.title = "#{name} #{version}"
71
+ rdoc.rdoc_files.include('README*')
72
+ rdoc.rdoc_files.include('lib/**/*.rb')
73
+ end
74
+
75
+ desc "Open an irb session preloaded with this library"
76
+ task :console do
77
+ p "irb -rubygems -r ./#{lib_path}"
78
+ sh "irb -rubygems -r ./#{lib_path}"
79
+ end
80
+
81
+ #############################################################################
82
+ #
83
+ # Custom tasks (add your own tasks here)
84
+ #
85
+ #############################################################################
86
+
87
+
88
+
89
+ #############################################################################
90
+ #
91
+ # Packaging tasks
92
+ #
93
+ #############################################################################
94
+
95
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
96
+ task :release => :build do
97
+ unless `git branch` =~ /^\* master$/
98
+ puts "You must be on the master branch to release!"
99
+ exit!
100
+ end
101
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
102
+ sh "git tag v#{version}"
103
+ sh "git push origin master"
104
+ sh "git push origin v#{version}"
105
+ sh "gem push pkg/#{name}-#{version}.gem"
106
+ end
107
+
108
+ desc "Build #{gem_file} into the pkg directory"
109
+ task :build => [:gemspec, :update_bundle] do
110
+ sh "mkdir -p pkg"
111
+ sh "gem build #{gemspec_file}"
112
+ sh "mv #{gem_file} pkg"
113
+ end
114
+
115
+ desc "Generate #{gemspec_file}"
116
+ task :gemspec => :validate do
117
+ # read spec file and split out manifest section
118
+ spec = File.read(gemspec_file)
119
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
120
+
121
+ # replace name version and date
122
+ replace_header(head, :name)
123
+ replace_header(head, :version)
124
+ replace_header(head, :date)
125
+ #comment this out if your rubyforge_project has a different name
126
+ replace_header(head, :rubyforge_project)
127
+
128
+ # determine file list from git ls-files
129
+ files = `git ls-files`.
130
+ split("\n").
131
+ sort.
132
+ reject { |file| file =~ /^\./ }.
133
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
134
+ map { |file| " #{file}" }.
135
+ join("\n")
136
+
137
+ # piece file back together and write
138
+ manifest = " s.files = %w[\n#{files}\n ]\n"
139
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
140
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
141
+ puts "Updated #{gemspec_file}"
142
+ end
143
+
144
+ desc "Update #{name} in bundle"
145
+ task :update_bundle => :validate do
146
+ `bundle update #{name}`
147
+ puts "Bundled #{name} version #{version}"
148
+ end
149
+
150
+ desc "Validate #{gemspec_file}"
151
+ task :validate do
152
+ libfiles = Dir['lib/*'] + Dir['lib/*/*'] - [lib_path, File.dirname(lib_path)]
153
+ unless libfiles.empty?
154
+ puts "Directory `lib` should only contain a `#{lib_path}` file and `#{name}` dir."
155
+ exit!
156
+ end
157
+ unless Dir['VERSION*'].empty?
158
+ puts "A `VERSION` file at root level violates Gem best practices."
159
+ exit!
160
+ end
161
+ end
@@ -0,0 +1,49 @@
1
+ require 'metriks'
2
+
3
+ module Metriks
4
+ class Middleware
5
+ VERSION = '0.0.1'
6
+
7
+ def initialize(app, options)
8
+ @app = app
9
+ @metric_prefix = options.fetch :metric_prefix, 'app'
10
+ end
11
+
12
+ def call(env)
13
+ time_response(env) do
14
+ record_heroku_status env
15
+ call_downstream env
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def time_response(env, &block)
22
+ if env.has_key? 'async.close'
23
+ context = response_timer.time
24
+ env['async.close'].callback do context.stop end
25
+ block.call
26
+ else
27
+ response_timer.time &block
28
+ end
29
+ end
30
+
31
+ def record_heroku_status(env)
32
+ queue_wait = env['HTTP_X_HEROKU_QUEUE_WAIT_TIME']
33
+ queue_depth = env['HTTP_X_HEROKU_QUEUE_DEPTH']
34
+ dynos_in_use = env['HTTP_X_HEROKU_DYNOS_IN_USE']
35
+
36
+ Metriks.histogram("#{ @metric_prefix }.queue.wait") .update(queue_wait.to_i) if queue_wait
37
+ Metriks.histogram("#{ @metric_prefix }.queue.depth").update(queue_depth.to_i) if queue_depth
38
+ Metriks.histogram("#{ @metric_prefix }.dynos") .update(dynos_in_use.to_i) if dynos_in_use
39
+ end
40
+
41
+ def call_downstream(env)
42
+ @app.call env
43
+ end
44
+
45
+ def response_timer
46
+ Metriks.timer @metric_prefix
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,65 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'metriks-middleware'
16
+ s.version = '0.0.1'
17
+ s.date = '2012-06-30'
18
+
19
+ ## Make sure your summary is short. The description may be as long
20
+ ## as you like.
21
+ s.summary = "Rack middleware for metriks"
22
+ s.description = "Rack middleware to track throughput and response time with metriks."
23
+
24
+ ## List the primary authors. If there are a bunch of authors, it's probably
25
+ ## better to set the email to an email list or something. If you don't have
26
+ ## a custom homepage, consider using your GitHub URL or the like.
27
+ s.authors = ["Larry Marburger"]
28
+ s.email = 'larry@marburger.cc'
29
+ s.homepage = 'https://github.com/lmarburger/metriks-middleware'
30
+
31
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
32
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
33
+ s.require_paths = %w[lib]
34
+
35
+ ## Specify any RDoc options here. You'll want to add your README and
36
+ ## LICENSE files to the extra_rdoc_files list.
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.extra_rdoc_files = %w[README.md LICENSE]
39
+
40
+ ## List your runtime dependencies here. Runtime dependencies are those
41
+ ## that are needed for an end user to actually USE your code.
42
+ s.add_dependency 'metriks', '~> 0.9.9'
43
+
44
+ ## List your development dependencies here. Development dependencies are
45
+ ## those that are only needed during development
46
+
47
+ ## Leave this section as-is. It will be automatically generated from the
48
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
49
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
50
+ # = MANIFEST =
51
+ s.files = %w[
52
+ Gemfile
53
+ Gemfile.lock
54
+ LICENSE
55
+ README.md
56
+ Rakefile
57
+ lib/metriks/middleware.rb
58
+ metriks-middleware.gemspec
59
+ ]
60
+ # = MANIFEST =
61
+
62
+ ## Test files will be grabbed from the file list. Make sure the path glob
63
+ ## matches what you actually use.
64
+ # s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
65
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metriks-middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Larry Marburger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: metriks
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.9
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.9.9
30
+ description: Rack middleware to track throughput and response time with metriks.
31
+ email: larry@marburger.cc
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files:
35
+ - README.md
36
+ - LICENSE
37
+ files:
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/metriks/middleware.rb
44
+ - metriks-middleware.gemspec
45
+ homepage: https://github.com/lmarburger/metriks-middleware
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.23
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Rack middleware for metriks
70
+ test_files: []