rack_timer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack_timer.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,46 @@
1
+ Rack Timer provides timing output around each of your Rails rack-based middleware classes. It will log output to your Rails application log.
2
+
3
+ To use, simply add rack_timer to your Gemfile:
4
+
5
+ gem "rack_timer"
6
+
7
+ By default the gem will only log Middleware that takes longer than the default Log Threshold of 1.0 ms. Set the RACK_TIMER_LOG_THRESHOLD environment variable to tweak this.
8
+
9
+ If you are tracking request queuing time via New Relic's suggested header (HTTP_X_REQUEST_START or HTTP_X_QUEUE_START), then Rack Timer will output how much time elapsed between when this header was set and when the first piece of middleware executes. See New Relic's documentation for more info: https://newrelic.com/docs/features/tracking-front-end-time
10
+
11
+
12
+ Example output of Rack Timer with RACK_TIMER_LOG_THRESHOLD = 0.0:
13
+
14
+ Rack Timer -- Queuing time: 1203 microseconds
15
+ Rack Timer -- Ngin::MiddlewareStart: 0.02288818359375 ms
16
+ Rack Timer -- Rack::Lock: 0.0150203704833984 ms
17
+ Rack Timer -- ActiveSupport::Cache::Strategy::LocalCache: 0.0181198120117188 ms
18
+ Rack Timer -- Rack::Runtime: 0.0121593475341797 ms
19
+
20
+
21
+ Started GET "/page/show/358660-history" for 75.146.189.233 at Mon Mar 05 13:37:30 -0800 2012
22
+ Rack Timer -- Rails::Rack::Logger: 0.110149383544922 ms
23
+ Rack Timer -- ActionDispatch::ShowExceptions: 0.00905990600585938 ms
24
+ Rack Timer -- ActionDispatch::RemoteIp: 0.0131130218505859 ms
25
+ Rack Timer -- Rack::Sendfile: 0.00691413879394531 ms
26
+ Rack Timer -- ActionDispatch::Callbacks: 0.0200271606445312 ms
27
+ Rack Timer -- ActiveRecord::ConnectionAdapters::ConnectionManagement: 0.00691413879394531 ms
28
+ Rack Timer -- ActiveRecord::QueryCache: 0.0360012054443359 ms
29
+ Rack Timer -- ActionDispatch::Cookies: 0.00691413879394531 ms
30
+ Rack Timer -- ActionDispatch::Session::CookieStore: 0.0231266021728516 ms
31
+ Rack Timer -- ActionDispatch::Flash: 1.24096870422363 ms
32
+ Rack Timer -- ActionDispatch::ParamsParser: 0.0169277191162109 ms
33
+ Rack Timer -- Rack::MethodOverride: 0.00786781311035156 ms
34
+ Rack Timer -- ActionDispatch::Head: 0.00810623168945312 ms
35
+ Rack Timer -- ActionDispatch::BestStandardsSupport: 0.00691413879394531 ms
36
+ Rack Timer -- Warden::Manager: 0.0591278076171875 ms
37
+ Rack Timer -- NewRelic::Rack::BrowserMonitoring: 0.00715255737304688 ms
38
+ Processing by PageController#show as HTML
39
+
40
+
41
+ The Queuing time shows that with this request, 1203 microseconds (1.2 milliseconds) elapsed from the time Nginx set the HTTP_X_REQUEST_START header and the start of the first piece of middleware. Each subsequent Rack Timer output line shows how long each piece of middleware took to execute in milliseconds. Turns out that all of the standard Rails middleware classes are generally fast, which is a good thing!
42
+
43
+
44
+
45
+
46
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,44 @@
1
+ module ActionDispatch
2
+ class MiddlewareStack < Array
3
+
4
+ # this class will wrap around each Rack-based middleware and take timing snapshots of how long
5
+ # each middleware takes to execute
6
+ class RackTimer
7
+
8
+ LogThreshold = ENV.has_key?('RACK_TIMER_LOG_THRESHOLD') ? ENV['RACK_TIMER_LOG_THRESHOLD'].to_f : 1.0 # millisecond
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ if env.has_key?("MIDDLEWARE_TIMESTAMP") # skip over the first middleware
16
+ elapsed_time = (Time.now.to_f - env["MIDDLEWARE_TIMESTAMP"][1].to_f) * 1000
17
+ if elapsed_time > LogThreshold # only log if took greater than LogThreshold
18
+ Rails.logger.info "Rack Timer -- #{env["MIDDLEWARE_TIMESTAMP"][0]}: #{elapsed_time} ms"
19
+ end
20
+ elsif env.has_key?("HTTP_X_REQUEST_START") or env.has_key?("HTTP_X_QUEUE_START")
21
+ # if we are tracking request queuing time via New Relic's suggested header(s),
22
+ # then lets see how much time was spent in the request queue by taking the difference
23
+ # between Time.now from the start of the first piece of middleware
24
+ # prefer HTTP_X_QUEUE_START over HTTP_X_REQUEST_START in case both exist
25
+ queue_start_time = (env["HTTP_X_QUEUE_START"] || env["HTTP_X_REQUEST_START"]).gsub("t=", "").to_i
26
+ Rails.logger.info "Rack Timer -- Queuing time: #{(Time.now.to_f * 1000000).to_i - queue_start_time} microseconds"
27
+ end
28
+ env["MIDDLEWARE_TIMESTAMP"] = [@app.class.to_s, Time.now]
29
+ @app.call env
30
+ end
31
+
32
+ end
33
+
34
+ class Middleware
35
+
36
+ # overrding the built-in Middleware.build and adding a RackTimer wrapper class
37
+ def build(app)
38
+ RackTimer.new(klass.new(app, *args, &block))
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module RackTimer
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rack_timer.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "rack_timer/version"
2
+
3
+ module RackTimer
4
+ require "rack_timer/stack"
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack_timer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack_timer"
7
+ s.version = RackTimer::VERSION
8
+ s.authors = ["lukeludwig"]
9
+ s.email = ["luke.ludwig@tstmedia.com"]
10
+ s.homepage = "http://www.github.com/tstmedia/rack_timer"
11
+ s.summary = %q{Provides timing output around each of your Rails rack-based middleware classes.}
12
+ s.description = %q{Provides timing output around each of your Rails rack-based middleware classes.}
13
+
14
+ s.rubyforge_project = "rack_timer"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_timer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - lukeludwig
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-05 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Provides timing output around each of your Rails rack-based middleware classes.
22
+ email:
23
+ - luke.ludwig@tstmedia.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README
34
+ - Rakefile
35
+ - lib/rack_timer.rb
36
+ - lib/rack_timer/stack.rb
37
+ - lib/rack_timer/version.rb
38
+ - rack_timer.gemspec
39
+ homepage: http://www.github.com/tstmedia/rack_timer
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: rack_timer
68
+ rubygems_version: 1.8.10
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Provides timing output around each of your Rails rack-based middleware classes.
72
+ test_files: []
73
+