rack_timer 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,7 @@ module ActionDispatch
5
5
  # each middleware takes to execute
6
6
  class RackTimer
7
7
 
8
+ # modify this environment variable to see more or less output
8
9
  LogThreshold = ENV.has_key?('RACK_TIMER_LOG_THRESHOLD') ? ENV['RACK_TIMER_LOG_THRESHOLD'].to_f : 1.0 # millisecond
9
10
 
10
11
  def initialize(app)
@@ -12,10 +13,17 @@ module ActionDispatch
12
13
  end
13
14
 
14
15
  def call(env)
16
+ env = incoming_timestamp(env)
17
+ status, headers, body = @app.call env
18
+ env = outgoing_timestamp(env)
19
+ [status, headers, body]
20
+ end
21
+
22
+ def incoming_timestamp(env)
15
23
  if env.has_key?("MIDDLEWARE_TIMESTAMP") # skip over the first middleware
16
24
  elapsed_time = (Time.now.to_f - env["MIDDLEWARE_TIMESTAMP"][1].to_f) * 1000
17
25
  if elapsed_time > LogThreshold # only log if took greater than LogThreshold
18
- Rails.logger.info "Rack Timer -- #{env["MIDDLEWARE_TIMESTAMP"][0]}: #{elapsed_time} ms"
26
+ Rails.logger.info "Rack Timer (incoming) -- #{env["MIDDLEWARE_TIMESTAMP"][0]}: #{elapsed_time} ms"
19
27
  end
20
28
  elsif env.has_key?("HTTP_X_REQUEST_START") or env.has_key?("HTTP_X_QUEUE_START")
21
29
  # if we are tracking request queuing time via New Relic's suggested header(s),
@@ -26,7 +34,24 @@ module ActionDispatch
26
34
  Rails.logger.info "Rack Timer -- Queuing time: #{(Time.now.to_f * 1000000).to_i - queue_start_time} microseconds"
27
35
  end
28
36
  env["MIDDLEWARE_TIMESTAMP"] = [@app.class.to_s, Time.now]
29
- @app.call env
37
+ env
38
+ end
39
+
40
+ def outgoing_timestamp(env)
41
+ if env.has_key?("MIDDLEWARE_TIMESTAMP")
42
+ elapsed_time = (Time.now.to_f - env["MIDDLEWARE_TIMESTAMP"][1].to_f) * 1000
43
+ if elapsed_time > LogThreshold # only log if took greater than LogThreshold
44
+ if env["MIDDLEWARE_TIMESTAMP"][0] and env["MIDDLEWARE_TIMESTAMP"][0] == @app.class.to_s
45
+ # this is the actual elapsed time of the final piece of Middleware (typically routing) AND the actual
46
+ # application's action
47
+ Rails.logger.info "Rack Timer (Application Action) -- #{@app.class.to_s}: #{elapsed_time} ms"
48
+ else
49
+ Rails.logger.info "Rack Timer (outgoing) -- #{@app.class.to_s}: #{elapsed_time} ms"
50
+ end
51
+ end
52
+ end
53
+ env["MIDDLEWARE_TIMESTAMP"] = [nil, Time.now]
54
+ env
30
55
  end
31
56
 
32
57
  end
@@ -39,6 +64,14 @@ module ActionDispatch
39
64
  end
40
65
 
41
66
  end
42
-
67
+
68
+ # overriding this in order to wrap the incoming app in a RackTimer, which gives us timing on the final
69
+ # piece of Middleware, which for Rails is the routing plus the actual Application action
70
+ def build(app = nil, &block)
71
+ app ||= block
72
+ raise "MiddlewareStack#build requires an app" unless app
73
+ reverse.inject(RackTimer.new(app)) { |a, e| e.build(a) }
74
+ end
75
+
43
76
  end
44
77
  end
@@ -1,3 +1,3 @@
1
1
  module RackTimer
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack_timer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - lukeludwig
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-03-07 00:00:00 Z
18
+ date: 2012-03-13 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Provides timing output around each of your Rails rack-based middleware classes.