query_trace-rails-3 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +112 -0
  3. data/Rakefile +37 -0
  4. data/init.rb +16 -0
  5. data/lib/query_trace.rb +58 -0
  6. metadata +65 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Nathaniel Talbott. All Rights Reserved.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,112 @@
1
+ = QueryTrace plugin for Rails
2
+
3
+ It's nice that ActiveRecord logs the queries that are performed when your actions are executed,
4
+ since it makes it easy to see when you have serious inefficiencies in your application. The next
5
+ question, though, is always, "OK, so where are those being run from?" Before QueryTrace, that
6
+ question could be a real pain to answer, since you'd have to go trawling through your code looking
7
+ for the culprit. Once you have QueryTrace installed, though, your logs won't just tell you that you
8
+ have a problem, they will pinpoint the location of that problem for you.
9
+
10
+ == Usage
11
+
12
+ Either install the QueryTrace as a plugin or as a gem. You can enable QueryTrace by setting the
13
+ QUERY_TRACE environment variable like so:
14
+
15
+ QUERY_TRACE=true
16
+
17
+ If you want QueryTrace to always be enabled, create a file in config/initializers, and add the
18
+ following line:
19
+
20
+ QueryTrace.enable!
21
+
22
+ You can also turn QueryTrace back off at any time:
23
+
24
+ QueryTrace.disable!
25
+
26
+ QueryTrace only outputs traces at the at the DEBUG log level, and honors your log colorization
27
+ settings. By default, it outputs up to 20 lines of stack trace. You can adjust this with the depth
28
+ accessor:
29
+
30
+ QueryTrace.depth = 5
31
+
32
+ == Runtime Toggling of QueryTrace
33
+
34
+ Starting with version 0.1.0 of QueryTrace, you can toggle tracing on or off without restarting the
35
+ server (thanks to Doug Barth for implementing this). To do so, include QueryTrace in your environment so that
36
+ it loads at server startup; you can use an initializer to set its initial state (on or off). Then just
37
+ send SIGQUIT (CTRL-\) to your server to switch from enabled to disabled and vice versa.
38
+
39
+ Important note: initializers load after environment.rb, but before environment-specific configuration
40
+ files like development.rb. So make sure you have the QueryTrace gem loaded in environment.rb or
41
+ the runtime toggling will not work. You can use an initializer to selectively turn it on in
42
+ development:
43
+
44
+ QueryTrace.enable! if Rails.env.development?
45
+
46
+ == Example
47
+
48
+ Before:
49
+
50
+ Schedule Load (0.023687) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
51
+ Resource Load (0.001076) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
52
+ Schedule Load (0.011488) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
53
+ Resource Load (0.022471) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
54
+
55
+
56
+ After:
57
+
58
+ Schedule Load (0.023687) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
59
+ app/models/available_work.rb:50:in `study_method'
60
+ app/helpers/plan_helper.rb:4:in `work_description'
61
+ app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
62
+ app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
63
+ app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
64
+ app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
65
+ app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
66
+ app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
67
+ vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
68
+ Resource Load (0.001076) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
69
+ app/models/available_work.rb:54:in `div_type'
70
+ app/helpers/plan_helper.rb:6:in `work_description'
71
+ app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
72
+ app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
73
+ app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
74
+ app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
75
+ app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
76
+ app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
77
+ vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
78
+ Schedule Load (0.011488) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
79
+ app/models/available_work.rb:50:in `study_method'
80
+ app/helpers/plan_helper.rb:4:in `work_description'
81
+ app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
82
+ app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
83
+ app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
84
+ app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
85
+ app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
86
+ app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
87
+ vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
88
+ Resource Load (0.022471) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
89
+ app/models/available_work.rb:54:in `div_type'
90
+ app/helpers/plan_helper.rb:6:in `work_description'
91
+ app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
92
+ app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
93
+ app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
94
+ app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
95
+ app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
96
+ app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
97
+ vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
98
+
99
+
100
+ == Additional Info
101
+
102
+ Original Author: Nathaniel Talbott (for Terralien)
103
+ Contact: nathaniel@talbott.ws
104
+ License: MIT
105
+ Home: http://github.com/ntalbott/query_trace/
106
+ Subversion mirror: https://terralien.devguard.com/svn/projects/plugins/query_trace
107
+
108
+ Current Author: Mike Gunderloy
109
+ Contact: MikeG1@larkfarm.com
110
+ Home: http://github.com/ffmike/query_trace, http://codaset.com/ffmike/query_trace
111
+
112
+ Copyright (c) 2006 Nathaniel Talbott. All Rights Reserved.
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ GEM_NAME = "query_trace"
5
+ GEM_VERSION = "0.0.1"
6
+ AUTHOR = "Nathaniel Talbott"
7
+ EMAIL = "nathaniel@terralien.com"
8
+ HOMEPAGE = "http://github.com/ntalbott/query_trace/tree/master"
9
+ SUMMARY = "Adds query origin tracing to your logs."
10
+
11
+ spec = Gem::Specification.new do |s|
12
+ s.rubyforge_project = 'query_trace'
13
+ s.name = GEM_NAME
14
+ s.version = GEM_VERSION
15
+ s.platform = Gem::Platform::RUBY
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ["README", "MIT-LICENSE"]
18
+ s.summary = SUMMARY
19
+ s.description = s.summary
20
+ s.author = AUTHOR
21
+ s.email = EMAIL
22
+ s.homepage = HOMEPAGE
23
+ s.add_dependency('activerecord')
24
+ s.require_path = 'lib'
25
+ s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib}/**/*")
26
+ end
27
+
28
+ Rake::GemPackageTask.new(spec) do |pkg|
29
+ pkg.gem_spec = spec
30
+ end
31
+
32
+ desc "Create a gemspec file"
33
+ task :gemspec do
34
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
35
+ file.puts spec.to_ruby
36
+ end
37
+ end
data/init.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'query_trace'
2
+
3
+ def status
4
+ QueryTrace.enabled? ? "enabled" : "disabled"
5
+ end
6
+
7
+ puts "=> QueryTrace #{status}; CTRL-\\ to toggle"
8
+
9
+ trap("QUIT") do
10
+ # Sending 2 backspace characters removes the ^\ that is
11
+ # printed to the console.
12
+ rm_noise = "\b\b"
13
+
14
+ QueryTrace.toggle!
15
+ puts "#{rm_noise}=> QueryTrace #{status}"
16
+ end
@@ -0,0 +1,58 @@
1
+ module QueryTrace
2
+ mattr_accessor :depth
3
+ self.depth = 20
4
+
5
+ def self.enabled?
6
+ defined?(@@trace_queries) && @@trace_queries
7
+ end
8
+
9
+ def self.enable!
10
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, QueryTrace) unless defined?(@@trace_queries)
11
+ @@trace_queries = true
12
+ end
13
+
14
+ def self.disable!
15
+ @@trace_queries = false
16
+ end
17
+
18
+ # Toggles query tracing on and off and returns a boolean indicating the new
19
+ # state of query tracing (true for enabled, false for disabled).
20
+ def self.toggle!
21
+ enabled? ? disable! : enable!
22
+ enabled?
23
+ end
24
+
25
+ def self.append_features(klass)
26
+ super
27
+ klass.class_eval do
28
+ unless method_defined?(:log_without_trace)
29
+ alias_method :log_without_trace, :log
30
+ alias_method :log, :log_with_trace
31
+ end
32
+ end
33
+ end
34
+
35
+ def log_with_trace(sql, name, &block)
36
+ result = log_without_trace(sql, name, &block)
37
+
38
+ return result unless @@trace_queries
39
+
40
+ return result unless ActiveRecord::Base.logger and ActiveRecord::Base.logger.debug?
41
+ return result if / Columns$/ =~ name
42
+
43
+ ActiveRecord::Base.logger.debug(format_trace(Rails.backtrace_cleaner.clean(caller)[0..self.depth]))
44
+
45
+ result
46
+ end
47
+
48
+ def format_trace(trace)
49
+ if (defined?(ActiveRecord::LogSubscriber) ? ActiveRecord::LogSubscriber : ActiveRecord::Base).colorize_logging
50
+ message_color = "35;2"
51
+ trace.collect{|t| " \e[#{message_color}m#{t}\e[0m"}.join("\n")
52
+ else
53
+ trace.join("\n ")
54
+ end
55
+ end
56
+ end
57
+
58
+ QueryTrace.enable! if ENV["QUERY_TRACE"]
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_trace-rails-3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathaniel Talbott
9
+ - Mike Gunderloy
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2009-11-27 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ requirement: &70169663363540 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70169663363540
26
+ description: With query_trace enabled, the Rails logs will contain a partial stack
27
+ trace for every executed SQL statement, making it easier to find where they are
28
+ generated in your code.
29
+ email: nathaniel@terralien.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - README
34
+ - MIT-LICENSE
35
+ files:
36
+ - MIT-LICENSE
37
+ - README
38
+ - Rakefile
39
+ - lib/query_trace.rb
40
+ - init.rb
41
+ homepage: http://github.com/ffmike/query_trace/tree/master
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
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
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.10
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: Adds query origin tracing to your logs.
65
+ test_files: []