logical-insight 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. data/History.txt +45 -0
  2. data/MIT-LICENSE.txt +19 -0
  3. data/README.md +123 -0
  4. data/Rakefile +24 -0
  5. data/Thorfile +113 -0
  6. data/lib/insight.rb +17 -0
  7. data/lib/insight/app.rb +189 -0
  8. data/lib/insight/database.rb +186 -0
  9. data/lib/insight/enable-button.rb +43 -0
  10. data/lib/insight/filtered_backtrace.rb +45 -0
  11. data/lib/insight/instrumentation.rb +9 -0
  12. data/lib/insight/instrumentation/backstage.rb +10 -0
  13. data/lib/insight/instrumentation/client.rb +20 -0
  14. data/lib/insight/instrumentation/instrument.rb +109 -0
  15. data/lib/insight/instrumentation/package-definition.rb +58 -0
  16. data/lib/insight/instrumentation/probe-definition.rb +20 -0
  17. data/lib/insight/instrumentation/probe.rb +199 -0
  18. data/lib/insight/instrumentation/setup.rb +32 -0
  19. data/lib/insight/logger.rb +55 -0
  20. data/lib/insight/options.rb +102 -0
  21. data/lib/insight/panel.rb +119 -0
  22. data/lib/insight/panel_app.rb +31 -0
  23. data/lib/insight/panels-content.rb +22 -0
  24. data/lib/insight/panels-header.rb +18 -0
  25. data/lib/insight/panels/active_record_panel.rb +46 -0
  26. data/lib/insight/panels/cache_panel.rb +69 -0
  27. data/lib/insight/panels/cache_panel/panel_app.rb +46 -0
  28. data/lib/insight/panels/cache_panel/stats.rb +98 -0
  29. data/lib/insight/panels/log_panel.rb +54 -0
  30. data/lib/insight/panels/memory_panel.rb +32 -0
  31. data/lib/insight/panels/rails_info_panel.rb +19 -0
  32. data/lib/insight/panels/redis_panel.rb +42 -0
  33. data/lib/insight/panels/redis_panel/redis_extension.rb +23 -0
  34. data/lib/insight/panels/redis_panel/stats.rb +50 -0
  35. data/lib/insight/panels/request_variables_panel.rb +70 -0
  36. data/lib/insight/panels/speedtracer_panel.rb +89 -0
  37. data/lib/insight/panels/speedtracer_panel/trace-app.rb +52 -0
  38. data/lib/insight/panels/speedtracer_panel/tracer.rb +212 -0
  39. data/lib/insight/panels/sql_panel.rb +53 -0
  40. data/lib/insight/panels/sql_panel/panel_app.rb +37 -0
  41. data/lib/insight/panels/sql_panel/query.rb +94 -0
  42. data/lib/insight/panels/templates_panel.rb +58 -0
  43. data/lib/insight/panels/templates_panel/rendering.rb +81 -0
  44. data/lib/insight/panels/timer_panel.rb +40 -0
  45. data/lib/insight/params_signature.rb +61 -0
  46. data/lib/insight/public/__insight__/bookmarklet.html +10 -0
  47. data/lib/insight/public/__insight__/bookmarklet.js +223 -0
  48. data/lib/insight/public/__insight__/insight.css +235 -0
  49. data/lib/insight/public/__insight__/insight.js +123 -0
  50. data/lib/insight/public/__insight__/jquery-1.3.2.js +4376 -0
  51. data/lib/insight/public/__insight__/jquery.tablesorter.min.js +1 -0
  52. data/lib/insight/public/__insight__/spinner.gif +0 -0
  53. data/lib/insight/rack_static_bug_avoider.rb +16 -0
  54. data/lib/insight/redirect_interceptor.rb +25 -0
  55. data/lib/insight/render.rb +72 -0
  56. data/lib/insight/request-recorder.rb +23 -0
  57. data/lib/insight/toolbar.rb +63 -0
  58. data/lib/insight/views/enable-button.html.erb +1 -0
  59. data/lib/insight/views/error.html.erb +17 -0
  60. data/lib/insight/views/headers_fragment.html.erb +20 -0
  61. data/lib/insight/views/panels/active_record.html.erb +17 -0
  62. data/lib/insight/views/panels/cache.html.erb +93 -0
  63. data/lib/insight/views/panels/execute_sql.html.erb +32 -0
  64. data/lib/insight/views/panels/explain_sql.html.erb +32 -0
  65. data/lib/insight/views/panels/log.html.erb +21 -0
  66. data/lib/insight/views/panels/profile_sql.html.erb +32 -0
  67. data/lib/insight/views/panels/rails_info.html.erb +19 -0
  68. data/lib/insight/views/panels/redis.html.erb +46 -0
  69. data/lib/insight/views/panels/request_variables.html.erb +25 -0
  70. data/lib/insight/views/panels/speedtracer/serverevent.html.erb +10 -0
  71. data/lib/insight/views/panels/speedtracer/servertrace.html.erb +12 -0
  72. data/lib/insight/views/panels/speedtracer/traces.html.erb +18 -0
  73. data/lib/insight/views/panels/sql.html.erb +43 -0
  74. data/lib/insight/views/panels/templates.html.erb +6 -0
  75. data/lib/insight/views/panels/timer.html.erb +19 -0
  76. data/lib/insight/views/panels/view_cache.html.erb +19 -0
  77. data/lib/insight/views/redirect.html.erb +16 -0
  78. data/lib/insight/views/request_fragment.html.erb +25 -0
  79. data/lib/insight/views/toolbar.html.erb +29 -0
  80. data/lib/logical-insight.rb +1 -0
  81. data/spec/custom_matchers.rb +31 -0
  82. data/spec/fixtures/config.ru +8 -0
  83. data/spec/fixtures/dummy_panel.rb +2 -0
  84. data/spec/fixtures/sample_app.rb +72 -0
  85. data/spec/insight/panels/active_record_panel_spec.rb +42 -0
  86. data/spec/insight/panels/cache_panel_spec.rb +176 -0
  87. data/spec/insight/panels/log_panel_spec.rb +44 -0
  88. data/spec/insight/panels/memory_panel_spec.rb +19 -0
  89. data/spec/insight/panels/mongo_panel_spec_pending.rb +50 -0
  90. data/spec/insight/panels/rails_info_panel_spec.rb +27 -0
  91. data/spec/insight/panels/redis_panel_spec.rb +66 -0
  92. data/spec/insight/panels/sql_panel_spec.rb +145 -0
  93. data/spec/insight/panels/templates_panel_spec.rb +84 -0
  94. data/spec/insight/panels/timer_panel_spec.rb +36 -0
  95. data/spec/insight_spec.rb +141 -0
  96. data/spec/instrumentation_spec.rb +188 -0
  97. data/spec/rcov.opts +1 -0
  98. data/spec/spec.opts +1 -0
  99. data/spec/spec_helper.rb +93 -0
  100. metadata +187 -0
data/History.txt ADDED
@@ -0,0 +1,45 @@
1
+ == HEAD
2
+
3
+ * New features
4
+
5
+ * Can use LoggerPanel on ruby stdlib Logger in non-rails app (Tim Connor)
6
+
7
+ * Bug fixes
8
+
9
+ * Fix profile, explain and select in the queries tab, fixes issue #22 (ebertech)
10
+
11
+ * Minor fixes
12
+
13
+ * Explicitly require 'digest/sha1' (Jérémy Lecour)
14
+ * Eliminate unreachable code in params signature validation (Tim Connor)
15
+
16
+ * Compatibilty
17
+
18
+ * Make Redis panel compatible with latest redis-rb gem, without breaking older redis-rb versions (Luke Melia)
19
+
20
+ * Other
21
+
22
+ * Refactoring and code cleanup (Tim Connor)
23
+ * Testing cleanup - better isolation of Rails vs. non-Rails in tests (Tim Connor)
24
+
25
+ == 0.3.0 / 2010-05-28
26
+
27
+ * New features
28
+
29
+ * Log panel includes log level and timestamp (Tim Connor)
30
+ * Sphinx panel (George Chatzigeorgiou)
31
+ * Backtraces for Redis panel (Luke Melia & Joey Aghion)
32
+
33
+ * Minor fixes
34
+
35
+ * Don't "enable" rack bug if you hit cancel on the bookmarklet prompt (Mischa Fierer)
36
+
37
+ * Compatibilty
38
+
39
+ * backtrace filtering now supports more than just Rails (Alex Chaffee)
40
+ * compatibility with current rack-test (Luke Melia & Joey Aghion)
41
+ * update Sinatra sample app (Tim Conner)
42
+
43
+ == 0.2.1
44
+
45
+ * The beginning of recorded history
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Bryan Helmkamp
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ Rack::Bug (Logical Reality Design Fork)
2
+ =========
3
+
4
+ LRDesign fork of Rack::Bug. We maintain our own fork because the base (by brynary)
5
+ seems focussed on Rails 2, and we want to improve Rails 3 compatibility and add features.
6
+
7
+ * Repository: [http://github.com/lrdesign/rack-bug](http://github.com/lrdesign/rack-bug)
8
+
9
+ * Forked From: [http://github.com/brynary/rack-bug](http://github.com/brynary/rack-bug)
10
+
11
+ Description
12
+ -----------
13
+
14
+ Rack::Bug adds a diagnostics toolbar to Rack apps. When enabled, it injects a floating div
15
+ allowing exploration of logging, database queries, template rendering times, etc.
16
+
17
+ Features
18
+ --------
19
+
20
+ * Password-based security
21
+ * IP-based security
22
+ * Rack::Bug instrumentation/reporting is broken up into panels.
23
+ * Panels in default configuration:
24
+ * Rails Info
25
+ * Timer
26
+ * Request Variables
27
+ * SQL
28
+ * Active Record
29
+ * Cache
30
+ * Templates
31
+ * Log
32
+ * Memory
33
+ * Other bundled panels:
34
+ * Redis
35
+ * Sphinx
36
+ * The API for adding your own panels is simple and powerful
37
+
38
+ Rails quick start
39
+ ---------------------------
40
+
41
+ script/plugin install git://github.com/brynary/rack-bug.git
42
+
43
+ In config/environments/development.rb, add:
44
+
45
+ config.middleware.use "Rack::Bug",
46
+ :secret_key => "someverylongandveryhardtoguesspreferablyrandomstring"
47
+
48
+ Add the bookmarklet to your browser:
49
+
50
+ open http://RAILS_APP/__rack_bug__/bookmarklet.html
51
+
52
+ Using with non-Rails Rack apps
53
+ ------------------------------
54
+
55
+ Just 'use Rack::Bug' as any other middleware. See the SampleApp in the spec/fixtures folder for an example Sinatra app.
56
+
57
+ If you wish to use the logger panel define the LOGGER constant that is a ruby Logger or ActiveSupport::BufferedLogger
58
+
59
+ Configuring custom panels
60
+ -------------------------
61
+
62
+ Specify the set of panels you want, in the order you want them to appear:
63
+
64
+ require "rack/bug"
65
+
66
+ ActionController::Dispatcher.middleware.use Rack::Bug,
67
+ :secret_key => "someverylongandveryhardtoguesspreferablyrandomstring",
68
+ :panel_classes => [
69
+ Rack::Bug::TimerPanel,
70
+ Rack::Bug::RequestVariablesPanel,
71
+ Rack::Bug::RedisPanel,
72
+ Rack::Bug::TemplatesPanel,
73
+ Rack::Bug::LogPanel,
74
+ Rack::Bug::MemoryPanel
75
+ ]
76
+
77
+
78
+ Running Rack::Bug in staging or production
79
+ ------------------------------------------
80
+
81
+ We have have found that Rack::Bug is fast enough to run in production for specific troubleshooting efforts.
82
+
83
+ ### Configuration ####
84
+
85
+ Add the middleware configuration to an initializer or the appropriate environment files, taking the rest of this section into consideration.
86
+
87
+ ### Security ####
88
+
89
+ Restrict access to particular IP addresses:
90
+
91
+ require "ipaddr"
92
+
93
+ ActionController::Dispatcher.middleware.use "Rack::Bug"
94
+ :secret_key => "someverylongandveryhardtoguesspreferablyrandomstring",
95
+ :ip_masks => [IPAddr.new("2.2.2.2/0")]
96
+
97
+ Restrict access using a password:
98
+
99
+ ActionController::Dispatcher.middleware.use "Rack::Bug",
100
+ :secret_key => "someverylongandveryhardtoguesspreferablyrandomstring",
101
+ :password => "yourpassword"
102
+
103
+
104
+ Authors
105
+ -------
106
+
107
+ - Maintained by [Evan Dorn](mailto:bryan@brynary.com)
108
+ - Contributions from Luke Melia, Joey Aghion, Tim Connor, and more
109
+
110
+ Thanks
111
+ ------
112
+ Inspiration for Rack::Bug is primarily from the Django debug toolbar. Additional ideas from Rails footnotes, Rack's ShowException middleware, Oink, and Rack::Cache
113
+
114
+ Thanks to Weplay.com for supporting the development of Rack::Bug
115
+
116
+ Development
117
+ -----------
118
+ For development, you'll need to install the following gems: rspec, rack-test, webrat, sinatra
119
+
120
+ License
121
+ -------
122
+
123
+ See MIT-LICENSE.txt in this directory.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'corundum'
2
+ require 'corundum/tasklibs'
3
+
4
+ module Corundum
5
+ tk = Toolkit.new do |tk|
6
+ end
7
+
8
+ tk.in_namespace do
9
+ sanity = GemspecSanity.new(tk)
10
+ rspec = RSpec.new(tk)
11
+ cov = SimpleCov.new(tk, rspec) do |cov|
12
+ cov.threshold = 90
13
+ end
14
+ gem = GemBuilding.new(tk)
15
+ cutter = GemCutter.new(tk,gem)
16
+ email = Email.new(tk)
17
+ vc = Git.new(tk) do |vc|
18
+ vc.branch = "master"
19
+ end
20
+ task tk.finished_files.build => vc["is_checked_in"]
21
+ docs = YARDoc.new(tk)
22
+ end
23
+ end
24
+
data/Thorfile ADDED
@@ -0,0 +1,113 @@
1
+ module GemHelpers
2
+
3
+ def generate_gemspec
4
+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "lib")))
5
+ require "rack/bug"
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "rack-bug"
9
+ s.version = Rack::Bug::VERSION
10
+ s.author = "Bryan Helmkamp"
11
+ s.email = "bryan@brynary.com"
12
+ s.homepage = "http://github.com/brynary/rack-bug"
13
+ s.summary = "Debugging toolbar for Rack applications implemented as middleware"
14
+ # s.description = "TODO"
15
+ s.rubyforge_project = "rack-bug"
16
+
17
+ require "git"
18
+ repo = Git.open(".")
19
+
20
+ s.files = normalize_files(repo.ls_files.keys - repo.lib.ignored_files)
21
+ s.test_files = normalize_files(Dir['spec/**/*.rb'] - repo.lib.ignored_files)
22
+
23
+ s.has_rdoc = true
24
+ s.extra_rdoc_files = %w[README.md MIT-LICENSE.txt]
25
+
26
+ s.add_dependency "rack", ">= 1.0"
27
+ s.add_development_dependency "webrat"
28
+ s.add_development_dependency "rspec"
29
+ s.add_development_dependency "sinatra"
30
+ s.add_development_dependency "git"
31
+ end
32
+ end
33
+
34
+ def normalize_files(array)
35
+ # only keep files, no directories, and sort
36
+ array.select do |path|
37
+ File.file?(path)
38
+ end.sort
39
+ end
40
+
41
+ # Adds extra space when outputting an array. This helps create better version
42
+ # control diffs, because otherwise it is all on the same line.
43
+ def prettyify_array(gemspec_ruby, array_name)
44
+ gemspec_ruby.gsub(/s\.#{array_name.to_s} = \[.+?\]/) do |match|
45
+ leadin, files = match[0..-2].split("[")
46
+ leadin + "[\n #{files.split(",").join(",\n ")}\n ]"
47
+ end
48
+ end
49
+
50
+ def read_gemspec
51
+ @read_gemspec ||= eval(File.read("rack-bug.gemspec"))
52
+ end
53
+
54
+ def sh(command)
55
+ puts command
56
+ system command
57
+ end
58
+ end
59
+
60
+ class Default < Thor
61
+ include GemHelpers
62
+
63
+ desc "gemspec", "Regenerate rack-bug.gemspec"
64
+ def gemspec
65
+ File.open("rack-bug.gemspec", "w") do |file|
66
+ gemspec_ruby = generate_gemspec.to_ruby
67
+ gemspec_ruby = prettyify_array(gemspec_ruby, :files)
68
+ gemspec_ruby = prettyify_array(gemspec_ruby, :test_files)
69
+ gemspec_ruby = prettyify_array(gemspec_ruby, :extra_rdoc_files)
70
+
71
+ file.write gemspec_ruby
72
+ end
73
+
74
+ puts "Wrote gemspec to rack-bug.gemspec"
75
+ read_gemspec.validate
76
+ end
77
+
78
+ desc "build", "Build a rack-bug gem"
79
+ def build
80
+ sh "gem build rack-bug.gemspec"
81
+ FileUtils.mkdir_p "pkg"
82
+ FileUtils.mv read_gemspec.file_name, "pkg"
83
+ end
84
+
85
+ desc "install", "Install the latest built gem"
86
+ def install
87
+ sh "gem install --local pkg/#{read_gemspec.file_name}"
88
+ end
89
+
90
+ desc "release", "Release the current branch to GitHub and Gemcutter"
91
+ def release
92
+ gemspec
93
+ build
94
+ Release.new.tag
95
+ Release.new.gem
96
+ end
97
+ end
98
+
99
+ class Release < Thor
100
+ include GemHelpers
101
+
102
+ desc "tag", "Tag the gem on the origin server"
103
+ def tag
104
+ release_tag = "v#{read_gemspec.version}"
105
+ sh "git tag -a #{release_tag} -m 'Tagging #{release_tag}'"
106
+ sh "git push origin #{release_tag}"
107
+ end
108
+
109
+ desc "gem", "Push the gem to Gemcutter"
110
+ def gem
111
+ sh "gem push pkg/#{read_gemspec.file_name}"
112
+ end
113
+ end
data/lib/insight.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Insight
2
+ class << self
3
+ def enable
4
+ Thread.current["insight.enabled"] = true
5
+ end
6
+
7
+ def disable
8
+ Thread.current["insight.enabled"] = false
9
+ end
10
+
11
+ def enabled?
12
+ Thread.current["insight.enabled"] == true
13
+ end
14
+ end
15
+ end
16
+
17
+ require 'insight/app'
@@ -0,0 +1,189 @@
1
+ require "rack"
2
+ require "digest/sha1"
3
+ require "insight/filtered_backtrace"
4
+ require "insight/options"
5
+ require "insight/logger"
6
+ require "insight/panel"
7
+ require "insight/panel_app"
8
+ require "insight/params_signature"
9
+ require "insight/rack_static_bug_avoider"
10
+ require "insight/redirect_interceptor"
11
+ require "insight/render"
12
+ require "insight/toolbar"
13
+ require "insight/enable-button"
14
+ require 'insight/logger'
15
+ require 'insight/request-recorder'
16
+ require 'insight/instrumentation/setup'
17
+ require 'insight/panels-content'
18
+ require 'insight/panels-header'
19
+
20
+ module Insight
21
+ class App
22
+ include Options
23
+ INSIGHT_ROOT = "/__insight__"
24
+ INSIGHT_REGEX = %r{^#{INSIGHT_ROOT}}
25
+
26
+ VERSION = "0.3.1"
27
+
28
+ class SecurityError < StandardError
29
+ end
30
+
31
+ def initialize(app, options = {}, &block)
32
+ initialize_options options
33
+ @base_app = app
34
+ @panels = []
35
+ instance_eval(&block) if block_given?
36
+
37
+ @logger = Logger.new(read_option(:log_level), read_option(:log_path))
38
+ Thread.current['insight.logger'] = @logger
39
+ build_normal_stack
40
+ build_debug_stack
41
+ if options[:on_initialize]
42
+ options[:on_initialize].call(self)
43
+ end
44
+ end
45
+ attr_reader :logger
46
+ attr_accessor :panels
47
+
48
+ def call(env)
49
+ env.replace @default_options.merge(env)
50
+ @env = env
51
+ @original_request = Rack::Request.new(@env)
52
+ env['insight.logger'] = @logger
53
+ Thread.current['insight.logger'] = @logger
54
+
55
+ if insight_active?
56
+ Insight.enable
57
+ env["insight.panels"] = []
58
+ result = @debug_stack.call(env)
59
+ result
60
+ else
61
+ @normal_stack.call(env)
62
+ end
63
+ end
64
+
65
+
66
+ def reset(new_options=nil)
67
+ initialize_options(new_options)
68
+
69
+ Insight::Instrumentation::ClassProbe::all_probes.each do |probe|
70
+ probe.clear_collectors
71
+ end
72
+ Insight::Instrumentation::InstanceProbe::all_probes.each do |probe|
73
+ probe.clear_collectors
74
+ end
75
+ Insight::Instrumentation::PackageDefinition.clear_collectors
76
+
77
+ build_debug_stack
78
+ end
79
+
80
+ private
81
+
82
+ def insight_active?
83
+ return (toolbar_requested? && ip_authorized? && password_authorized?)
84
+ end
85
+
86
+ def build_normal_stack
87
+ builder = Rack::Builder.new
88
+ builder.use EnableButton, self
89
+ builder.run Rack::Cascade.new([ asset_mapped(Rack::Builder.new), @base_app ])
90
+ @normal_stack = builder.to_app
91
+ end
92
+
93
+ def build_debug_stack
94
+ @panels.clear
95
+ builder = Rack::Builder.new
96
+ builder.use Toolbar, self
97
+ builder.run Rack::Cascade.new([panel_mappings, collection_stack(@base_app)])
98
+ @debug_stack = builder.to_app
99
+ end
100
+
101
+ def panel_mappings
102
+ classes = read_option(:panel_classes)
103
+ root = INSIGHT_ROOT
104
+ insight = self
105
+ builder = Rack::Builder.new do
106
+ classes.each do |panel_class|
107
+ panel_class.panel_mappings.each do |path, app|
108
+ map [root, path].join("/") do
109
+ run app
110
+ end
111
+ end
112
+ end
113
+ map root + "/panels_content" do
114
+ run PanelsContent.new(insight)
115
+ end
116
+ map root + "/panels_header" do
117
+ run PanelsHeader.new(insight)
118
+ end
119
+ end
120
+ return asset_mapped(builder)
121
+ end
122
+
123
+
124
+ def collection_stack(app)
125
+ classes = read_option(:panel_classes)
126
+ insight_id = self.object_id
127
+ panels = self.panels
128
+ Rack::Builder.app do
129
+ use RequestRecorder
130
+ classes.each do |panel_class|
131
+ run(lambda do |app|
132
+ panel = panel_class.new(app)
133
+ panels << panel
134
+ panel
135
+ end)
136
+ end
137
+ use RedirectInterceptor
138
+ use Instrumentation::Setup
139
+ run app
140
+ end
141
+ end
142
+
143
+ def asset_mapped(builder)
144
+ path = public_path
145
+ builder.map INSIGHT_ROOT do
146
+ run Rack::File.new(path)
147
+ end
148
+ builder.to_app
149
+ end
150
+
151
+ def public_path
152
+ ::File.expand_path("../../insight/public/__insight__", __FILE__)
153
+ end
154
+
155
+ def toolbar_requested?
156
+ @original_request.cookies["insight_enabled"]
157
+ end
158
+
159
+ def ip_authorized?
160
+ return true unless options["insight.ip_masks"]
161
+
162
+ logger.debug{ "Checking #{@original_request.ip} against ip_masks" }
163
+ ip = IPAddr.new(@original_request.ip)
164
+
165
+ mask = options["insight.ip_masks"].find do |ip_mask|
166
+ ip_mask.include?(ip)
167
+ end
168
+ if mask
169
+ logger.debug{ "Matched #{mask}" }
170
+ return true
171
+ else
172
+ logger.debug{ "Matched no masks" }
173
+ return false
174
+ end
175
+ end
176
+
177
+ def password_authorized?
178
+ return true unless options["insight.password"]
179
+
180
+ logger.debug{"Checking password"}
181
+
182
+ expected_sha = Digest::SHA1.hexdigest ["insight", options["insight.password"]].join(":")
183
+ actual_sha = @original_request.cookies["insight_password"]
184
+
185
+ logger.debug{"Password result: #{actual_sha == expected_sha}"}
186
+ actual_sha == expected_sha
187
+ end
188
+ end
189
+ end