vizjerai-query_trace 0.2.0

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 +38 -0
  4. data/init.rb +16 -0
  5. data/lib/query_trace.rb +64 -0
  6. metadata +87 -0
data/MIT-LICENSE ADDED
@@ -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.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ GEM_NAME = "vizjerai-query_trace"
5
+ GEM_VERSION = "0.2.0"
6
+ AUTHORS = ["Nathaniel Talbott", "Mike Gunderloy", "Andrew Assarattanakul"]
7
+ EMAIL = "nathaniel@terralien.com"
8
+ HOMEPAGE = "http://github.com/vizjerai/query_trace"
9
+ SUMMARY = "Adds query origin tracing to your logs."
10
+ DESCRIPTION = %q{With query_trace enabled, the Rails logs will contain a partial stack trace for every executed SQL statement, making it easier to find where they are generated in your code.}
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.rubyforge_project = 'query_trace'
14
+ s.name = GEM_NAME
15
+ s.version = GEM_VERSION
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README", "MIT-LICENSE"]
19
+ s.summary = SUMMARY
20
+ s.description = DESCRIPTION
21
+ s.authors = AUTHORS
22
+ s.email = EMAIL
23
+ s.homepage = HOMEPAGE
24
+ s.add_dependency('activerecord')
25
+ s.require_path = 'lib'
26
+ s.files = %w(MIT-LICENSE README Rakefile init.rb) + Dir.glob("{lib}/**/*")
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |pkg|
30
+ pkg.gem_spec = spec
31
+ end
32
+
33
+ desc "Create a gemspec file"
34
+ task :gemspec do
35
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
36
+ file.puts spec.to_ruby
37
+ end
38
+ 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,64 @@
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
+
57
+ def clean_trace(trace)
58
+ Rails.respond_to?(:backtrace_cleaner) ?
59
+ Rails.backtrace_cleaner.clean(trace) :
60
+ trace
61
+ end
62
+ end
63
+
64
+ QueryTrace.enable! if ENV["QUERY_TRACE"]
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vizjerai-query_trace
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Nathaniel Talbott
14
+ - Mike Gunderloy
15
+ - Andrew Assarattanakul
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-02-16 00:00:00 -06:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: activerecord
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 3
32
+ segments:
33
+ - 0
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: With query_trace enabled, the Rails logs will contain a partial stack trace for every executed SQL statement, making it easier to find where they are generated in your code.
38
+ email: nathaniel@terralien.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ - MIT-LICENSE
46
+ files:
47
+ - MIT-LICENSE
48
+ - README
49
+ - Rakefile
50
+ - init.rb
51
+ - lib/query_trace.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/vizjerai/query_trace
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project: query_trace
82
+ rubygems_version: 1.5.2
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Adds query origin tracing to your logs.
86
+ test_files: []
87
+