rack-bug 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/History.txt +21 -0
  2. data/README.md +115 -0
  3. data/Rakefile +6 -19
  4. data/Thorfile +109 -0
  5. data/lib/rack/bug.rb +4 -2
  6. data/lib/rack/bug/filtered_backtrace.rb +38 -0
  7. data/lib/rack/bug/options.rb +4 -4
  8. data/lib/rack/bug/panel.rb +12 -12
  9. data/lib/rack/bug/panel_app.rb +8 -8
  10. data/lib/rack/bug/panels/active_record_panel.rb +11 -11
  11. data/lib/rack/bug/panels/active_record_panel/activerecord_extensions.rb +3 -3
  12. data/lib/rack/bug/panels/cache_panel.rb +1 -1
  13. data/lib/rack/bug/panels/cache_panel/memcache_extension.rb +4 -4
  14. data/lib/rack/bug/panels/cache_panel/panel_app.rb +11 -11
  15. data/lib/rack/bug/panels/cache_panel/stats.rb +20 -20
  16. data/lib/rack/bug/panels/log_panel.rb +27 -10
  17. data/lib/rack/bug/panels/log_panel/rails_extension.rb +2 -2
  18. data/lib/rack/bug/panels/memory_panel.rb +8 -8
  19. data/lib/rack/bug/panels/rails_info_panel.rb +5 -5
  20. data/lib/rack/bug/panels/redis_panel.rb +3 -3
  21. data/lib/rack/bug/panels/redis_panel/redis_extension.rb +3 -3
  22. data/lib/rack/bug/panels/redis_panel/stats.rb +17 -13
  23. data/lib/rack/bug/panels/request_variables_panel.rb +7 -7
  24. data/lib/rack/bug/panels/sphinx_panel.rb +44 -0
  25. data/lib/rack/bug/panels/sphinx_panel/sphinx_extension.rb +13 -0
  26. data/lib/rack/bug/panels/sphinx_panel/stats.rb +96 -0
  27. data/lib/rack/bug/panels/sql_panel.rb +1 -1
  28. data/lib/rack/bug/panels/sql_panel/panel_app.rb +7 -7
  29. data/lib/rack/bug/panels/sql_panel/query.rb +13 -23
  30. data/lib/rack/bug/panels/sql_panel/sql_extension.rb +2 -2
  31. data/lib/rack/bug/panels/templates_panel.rb +1 -1
  32. data/lib/rack/bug/panels/templates_panel/actionview_extension.rb +1 -1
  33. data/lib/rack/bug/panels/templates_panel/rendering.rb +14 -14
  34. data/lib/rack/bug/panels/templates_panel/trace.rb +8 -8
  35. data/lib/rack/bug/panels/timer_panel.rb +10 -10
  36. data/lib/rack/bug/params_signature.rb +18 -18
  37. data/lib/rack/bug/public/__rack_bug__/bookmarklet.js +7 -5
  38. data/lib/rack/bug/render.rb +16 -16
  39. data/lib/rack/bug/toolbar.rb +39 -33
  40. data/lib/rack/bug/views/panels/log.html.erb +4 -6
  41. data/lib/rack/bug/views/panels/redis.html.erb +14 -0
  42. data/lib/rack/bug/views/panels/sphinx.html.erb +32 -0
  43. data/rack-bug.gemspec +102 -97
  44. data/spec/fixtures/config.ru +3 -1
  45. data/spec/fixtures/sample_app.rb +7 -6
  46. data/spec/rack/bug/panels/active_record_panel_spec.rb +6 -6
  47. data/spec/rack/bug/panels/cache_panel_spec.rb +46 -46
  48. data/spec/rack/bug/panels/log_panel_spec.rb +8 -7
  49. data/spec/rack/bug/panels/memory_panel_spec.rb +6 -6
  50. data/spec/rack/bug/panels/rails_info_panel_spec.rb +5 -5
  51. data/spec/rack/bug/panels/redis_panel_spec.rb +31 -19
  52. data/spec/rack/bug/panels/sql_panel_spec.rb +45 -45
  53. data/spec/rack/bug/panels/templates_panel_spec.rb +18 -18
  54. data/spec/rack/bug/panels/timer_panel_spec.rb +12 -12
  55. data/spec/rack/toolbar_spec.rb +34 -28
  56. data/spec/spec_helper.rb +19 -9
  57. metadata +44 -13
  58. data/README.rdoc +0 -29
  59. data/VERSION +0 -1
@@ -2,44 +2,44 @@ require "rack/bug/panels/active_record_panel/activerecord_extensions"
2
2
 
3
3
  module Rack
4
4
  module Bug
5
-
5
+
6
6
  class ActiveRecordPanel < Panel
7
-
7
+
8
8
  def self.record(class_name)
9
9
  return unless Rack::Bug.enabled?
10
10
  records[class_name] += 1
11
11
  end
12
-
12
+
13
13
  def self.reset
14
14
  Thread.current["rack.bug.active_records"] = Hash.new { 0 }
15
15
  end
16
-
16
+
17
17
  def self.records
18
18
  Thread.current["rack.bug.active_records"] ||= Hash.new { 0 }
19
19
  end
20
-
20
+
21
21
  def self.total
22
22
  records.inject(0) do |memo, (key, value)|
23
23
  memo + value
24
24
  end
25
25
  end
26
-
26
+
27
27
  def name
28
28
  "active_record"
29
29
  end
30
-
30
+
31
31
  def heading
32
32
  "#{self.class.total} AR Objects"
33
33
  end
34
-
34
+
35
35
  def content
36
36
  records = self.class.records.to_a.sort_by { |key, value| value }.reverse
37
37
  result = render_template "panels/active_record", :records => records
38
38
  self.class.reset
39
39
  result
40
40
  end
41
-
41
+
42
42
  end
43
-
43
+
44
44
  end
45
- end
45
+ end
@@ -6,13 +6,13 @@ if defined?(ActiveRecord)
6
6
  Rack::Bug::ActiveRecordPanel.record(self.class.base_class.name)
7
7
  after_initialize_without_rack_bug
8
8
  end
9
-
9
+
10
10
  alias_method_chain :after_initialize, :rack_bug
11
11
  else
12
12
  def after_initialize
13
13
  Rack::Bug::ActiveRecordPanel.record(self.class.base_class.name)
14
14
  end
15
15
  end
16
-
16
+
17
17
  end
18
- end
18
+ end
@@ -47,4 +47,4 @@ module Rack
47
47
  end
48
48
 
49
49
  end
50
- end
50
+ end
@@ -42,13 +42,13 @@ if defined?(Memcached)
42
42
  prepend_without_rack_bug(key, value)
43
43
  end
44
44
  end
45
-
45
+
46
46
  def delete_with_rack_bug(key)
47
47
  Rack::Bug::CachePanel.record(:delete, key) do
48
48
  delete_without_rack_bug(key)
49
49
  end
50
50
  end
51
-
51
+
52
52
  def get_with_rack_bug(keys, marshal=true)
53
53
  if keys.is_a? Array
54
54
  Rack::Bug::CachePanel.record(:get_multi, *keys) do
@@ -117,7 +117,7 @@ if defined?(MemCache)
117
117
  delete_without_rack_bug(key, expiry)
118
118
  end
119
119
  end
120
-
120
+
121
121
  alias_method_chain :decr, :rack_bug
122
122
  alias_method_chain :get, :rack_bug
123
123
  alias_method_chain :get_multi, :rack_bug
@@ -126,4 +126,4 @@ if defined?(MemCache)
126
126
  alias_method_chain :add, :rack_bug
127
127
  alias_method_chain :delete, :rack_bug
128
128
  end
129
- end
129
+ end
@@ -1,9 +1,9 @@
1
1
  module Rack
2
2
  module Bug
3
3
  class CachePanel
4
-
4
+
5
5
  class PanelApp < ::Rack::Bug::PanelApp
6
-
6
+
7
7
  def dispatch
8
8
  case request.path_info
9
9
  when "/__rack_bug__/view_cache" then view_cache
@@ -12,37 +12,37 @@ module Rack
12
12
  else not_found
13
13
  end
14
14
  end
15
-
15
+
16
16
  def ok
17
17
  Rack::Response.new(["OK"]).to_a
18
18
  end
19
-
19
+
20
20
  def view_cache
21
21
  validate_params
22
22
  render_template "panels/view_cache", :key => params["key"], :value => Rails.cache.read(params["key"])
23
23
  end
24
-
24
+
25
25
  def delete_cache
26
26
  validate_params
27
27
  raise "Rails not found... can't delete key" unless defined?(Rails)
28
28
  Rails.cache.delete(params["key"])
29
29
  ok
30
30
  end
31
-
31
+
32
32
  def delete_cache_list
33
33
  validate_params
34
34
  raise "Rails not found... can't delete key" unless defined?(Rails)
35
-
35
+
36
36
  params.each do |key, value|
37
37
  next unless key =~ /^keys_/
38
38
  Rails.cache.delete(value)
39
39
  end
40
-
40
+
41
41
  ok
42
42
  end
43
-
43
+
44
44
  end
45
-
45
+
46
46
  end
47
47
  end
48
- end
48
+ end
@@ -1,22 +1,22 @@
1
1
  module Rack
2
2
  module Bug
3
3
  class CachePanel
4
-
4
+
5
5
  class Stats
6
6
  class Query
7
7
  attr_reader :method, :time, :hit, :keys
8
-
8
+
9
9
  def initialize(method, time, hit, keys)
10
10
  @method = method
11
11
  @time = time
12
12
  @hit = hit
13
13
  @keys = keys
14
14
  end
15
-
15
+
16
16
  def display_time
17
17
  "%.2fms" % time
18
18
  end
19
-
19
+
20
20
  def display_keys
21
21
  if keys.size == 1
22
22
  keys.first
@@ -25,64 +25,64 @@ module Rack
25
25
  end
26
26
  end
27
27
  end
28
-
28
+
29
29
  attr_reader :calls
30
30
  attr_reader :keys
31
31
  attr_reader :queries
32
-
32
+
33
33
  def initialize
34
34
  @queries = []
35
- @misses =
35
+ @misses =
36
36
  @calls = 0
37
37
  @time = 0.0
38
38
  @keys = []
39
39
  end
40
-
40
+
41
41
  def record_call(method, time, hit, *keys)
42
42
  @queries << Query.new(method, time, hit, keys)
43
43
  @calls += 1
44
44
  @time += time
45
45
  @keys += keys
46
46
  end
47
-
47
+
48
48
  def display_time
49
49
  "%.2fms" % time
50
50
  end
51
-
51
+
52
52
  def time
53
53
  @queries.inject(0) do |memo, query|
54
54
  memo + query.time
55
55
  end
56
56
  end
57
-
57
+
58
58
  def gets
59
59
  count_queries(:get)
60
60
  end
61
-
61
+
62
62
  def sets
63
63
  count_queries(:set)
64
64
  end
65
-
65
+
66
66
  def deletes
67
67
  count_queries(:delete)
68
68
  end
69
-
69
+
70
70
  def get_multis
71
71
  count_queries(:get_multi)
72
72
  end
73
-
73
+
74
74
  def hits
75
75
  @queries.select { |q| [:get, :get_multi].include?(q.method) && q.hit }.size
76
76
  end
77
-
77
+
78
78
  def misses
79
79
  @queries.select { |q| [:get, :get_multi].include?(q.method) && !q.hit }.size
80
80
  end
81
-
81
+
82
82
  def count_queries(method)
83
83
  @queries.select { |q| q.method == method }.size
84
84
  end
85
-
85
+
86
86
  def queries_to_param
87
87
  params = {}
88
88
  @queries.each_with_index do |query, index|
@@ -91,7 +91,7 @@ module Rack
91
91
  params
92
92
  end
93
93
  end
94
-
94
+
95
95
  end
96
96
  end
97
- end
97
+ end
@@ -2,27 +2,44 @@ require "rack/bug/panels/log_panel/rails_extension"
2
2
 
3
3
  module Rack
4
4
  module Bug
5
-
5
+
6
6
  class LogPanel < Panel
7
-
8
- def self.record(message)
7
+ class LogEntry
8
+ attr_reader :level, :time, :message
9
+ LEVELS = ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']
10
+
11
+ def initialize(level, time, message)
12
+ @level = LEVELS[level]
13
+ @time = time
14
+ @message = message
15
+ end
16
+
17
+ def cleaned_message
18
+ @message.to_s.gsub(/\e\[[;\d]+m/, "")
19
+ end
20
+ end
21
+
22
+ def self.record(message, log_level)
9
23
  return unless Rack::Bug.enabled?
10
24
  return unless message
11
- logs << message.to_s
25
+ Thread.current["rack.bug.logs.start"] ||= Time.now
26
+ timestamp = ((Time.now - Thread.current["rack.bug.logs.start"]) * 1000).to_i
27
+ logs << LogEntry.new(log_level, timestamp, message)
12
28
  end
13
-
29
+
14
30
  def self.reset
15
31
  Thread.current["rack.bug.logs"] = []
32
+ Thread.current["rack.bug.logs.start"] = nil
16
33
  end
17
-
34
+
18
35
  def self.logs
19
36
  Thread.current["rack.bug.logs"] ||= []
20
37
  end
21
-
38
+
22
39
  def name
23
40
  "log"
24
41
  end
25
-
42
+
26
43
  def heading
27
44
  "Log"
28
45
  end
@@ -32,8 +49,8 @@ module Rack
32
49
  self.class.reset
33
50
  return result
34
51
  end
35
-
52
+
36
53
  end
37
-
54
+
38
55
  end
39
56
  end
@@ -2,10 +2,10 @@ if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
2
2
  module LoggingExtensions
3
3
  def add(*args, &block)
4
4
  logged_message = super
5
- Rack::Bug::LogPanel.record(logged_message)
5
+ Rack::Bug::LogPanel.record(logged_message, args[0])
6
6
  return logged_message
7
7
  end
8
8
  end
9
9
 
10
10
  Rails.logger.extend LoggingExtensions
11
- end
11
+ end
@@ -1,18 +1,18 @@
1
- #
1
+ #
2
2
  module Rack
3
3
  module Bug
4
-
4
+
5
5
  class MemoryPanel < Panel
6
-
6
+
7
7
  def before(env)
8
8
  @original_memory = `ps -o rss= -p #{$$}`.to_i
9
9
  end
10
-
10
+
11
11
  def after(env, status, headers, body)
12
12
  @total_memory = `ps -o rss= -p #{$$}`.to_i
13
13
  @memory_increase = @total_memory - @original_memory
14
14
  end
15
-
15
+
16
16
  def heading
17
17
  "#{@memory_increase} KB &#916;, #{@total_memory} KB total"
18
18
  end
@@ -20,8 +20,8 @@ module Rack
20
20
  def has_content?
21
21
  false
22
22
  end
23
-
23
+
24
24
  end
25
-
25
+
26
26
  end
27
- end
27
+ end
@@ -1,12 +1,12 @@
1
1
  module Rack
2
2
  module Bug
3
-
3
+
4
4
  class RailsInfoPanel < Panel
5
-
5
+
6
6
  def name
7
7
  "rails_info"
8
8
  end
9
-
9
+
10
10
  def heading
11
11
  return unless (defined?(Rails) && defined?(Rails::Info))
12
12
  "Rails #{Rails.version}"
@@ -16,8 +16,8 @@ module Rack
16
16
  return unless (defined?(Rails) && defined?(Rails::Info))
17
17
  render_template "panels/rails_info"
18
18
  end
19
-
19
+
20
20
  end
21
-
21
+
22
22
  end
23
23
  end
@@ -6,13 +6,13 @@ module Rack
6
6
 
7
7
  autoload :Stats, "rack/bug/panels/redis_panel/stats"
8
8
 
9
- def self.record(*redis_command_args, &block)
9
+ def self.record(redis_command_args, backtrace, &block)
10
10
  return block.call unless Rack::Bug.enabled?
11
11
 
12
12
  start_time = Time.now
13
13
  result = block.call
14
14
  total_time = Time.now - start_time
15
- stats.record_call(total_time * 1_000, redis_command_args)
15
+ stats.record_call(total_time * 1_000, redis_command_args, backtrace)
16
16
  return result
17
17
  end
18
18
 
@@ -41,4 +41,4 @@ module Rack
41
41
  end
42
42
 
43
43
  end
44
- end
44
+ end