query_tracer 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -18,17 +18,17 @@ Rewrite SQL queries adding small trace as a comment. It will show where long run
18
18
 
19
19
  Add following code to config/initializers/query_tracer.rb:
20
20
 
21
- QueryTracer::Configuration.set do |tracer|
21
+ QueryTracer.configure do |tracer|
22
22
  tracer.enabled = true
23
23
  tracer.colorize = true
24
24
  tracer.show_revision = true
25
25
  tracer.multiline = true
26
- tracer.skip_queries = [%r{FROM sqlite_master}]
26
+ tracer.exclude_queries << %r{FROM sqlite_master}
27
27
  end
28
28
 
29
29
  QueryTracer::Logger.attach_to :active_record
30
30
 
31
- `skip_queries` will merge one/many regexps into default expressions list (QueryTracer::Tracer::EXCLUDE_SQL) for current DB.
31
+ `exclude_queries` will contain default expressions list for currently selected DB.
32
32
 
33
33
  ## Example
34
34
 
@@ -1,6 +1,4 @@
1
1
  require 'active_record/log_subscriber'
2
-
3
- require 'query_tracer/configuration'
4
2
  require 'query_tracer/tracer'
5
3
 
6
4
  module QueryTracer
@@ -10,21 +8,21 @@ module QueryTracer
10
8
  # event.duration
11
9
  # event.payload[:sql]
12
10
  def sql(event)
13
- return unless Configuration.enabled
11
+ return unless QueryTracer.config.enabled
14
12
 
15
13
  sql = event.payload[:sql]
16
14
  # Skip noisy queries
17
15
  trace = Tracer.build_trace(sql)
18
16
  return if trace.blank?
19
17
  # We're done
20
- if Configuration.colorize
18
+ if QueryTracer.config.colorize
21
19
  message = "\e[34m\e[43m^^^^ Called from:\e[0m "
22
20
  indent = "\e[34m\e[43m->\e[0m "
23
21
  else
24
22
  message = '^^^^ Called from: '
25
23
  indent = " "
26
24
  end
27
- logger.send QueryTracer::Configuration.log_level.to_sym, message + trace.join("\n#{indent}")
25
+ logger.send QueryTracer.config.log_level.to_sym, message + trace.join("\n#{indent}")
28
26
  end
29
27
  end
30
28
  end
@@ -4,14 +4,14 @@ module QueryTracer
4
4
  module Revision
5
5
  # Detect the current code revision and memoize it for the future.
6
6
  def self.current
7
- return nil unless QueryTracer::Configuration.show_revision
7
+ return nil unless QueryTracer.config.show_revision
8
8
 
9
9
  begin
10
10
  # Do we have the code revision memoized?
11
11
  unless defined?(@@current_code_revision)
12
- @@current_code_revision = if File.exists?("#{Rails.root}/REVISION")
12
+ @@current_code_revision = if File.exists?("#{QueryTracer.config.root}/REVISION")
13
13
  # Capistrano-deployed application, we know where to get current revision
14
- File.read("#{Rails.root}/REVISION").chomp.strip
14
+ File.read("#{QueryTracer.config.root}/REVISION").chomp.strip
15
15
  else
16
16
  # Try to use git
17
17
  rev = `git rev-parse HEAD 2>/dev/null`.chomp.strip
@@ -1,41 +1,21 @@
1
- require 'query_tracer/configuration'
2
1
  require 'query_tracer/tracer/revision'
3
2
 
4
3
  module QueryTracer
5
4
  module Tracer
6
5
  extend self
7
6
 
8
- INCLUDE_CODEPOINTS = [
9
- %r{^#{Rails.root}/(app/presenters/.*)},
10
- %r{^#{Rails.root}/(app/views/.*)},
11
- %r{^#{Rails.root}/(app/controllers/.*)},
12
- %r{^#{Rails.root}/(app/models/.*)},
13
- %r{^#{Rails.root}/(lib/.*)},
14
- %r{^#{Rails.root}/(spec/.*)},
15
- %r{^#{Rails.root}/(app/.*)},
16
- %r{^#{Rails.root}/(vendor/(?:gems|plugins)/.*)},
17
- %r{^#{Rails.root}/(.*)},
18
- %r{in `(irb)_binding'}
19
- ]
20
- # A regular expression used to skip certain code points (gems that do nothing
21
- # but add noice to the result).
22
- EXCLUDE_CODEPOINT = %r{^(#{Rails.root}/(?:vendor/(?:rails|gems/(?:composite_primary_keys|db-charmer)|plugins/(?:paginating_find|acts_as_sluggable))|config/initializers/mysql_adapter_extensions\.rb|tmp/gems|lib/query_tracer))|\.rvm/|/gems/}
23
- # A regular expression to exclude certain SQL queries from processing (who cares
24
- # where SHOW TABLES was issues from).
25
- EXCLUDE_SQL = []
26
-
27
7
  def build_trace(sql)
28
8
  unless skip_query?(sql)
29
9
  # Skip noisy codepoints
30
10
  lines = caller.inject([]) do |filtered, line|
31
- unless line =~ EXCLUDE_CODEPOINT
32
- filtered << line unless INCLUDE_CODEPOINTS.select{ |expr| line =~ expr }.blank?
11
+ unless line =~ QueryTracer.config.exclude_codepoint
12
+ filtered << line unless QueryTracer.config.include_codepoints.select{ |expr| line =~ expr }.blank?
33
13
  end
34
14
  filtered
35
15
  end
36
16
 
37
17
  unless lines.blank?
38
- lines = lines.first unless QueryTracer::Configuration.multiline
18
+ lines = lines.first unless QueryTracer.config.multiline
39
19
  [QueryTracer::Tracer::Revision.current, lines].flatten
40
20
  end
41
21
 
@@ -43,7 +23,7 @@ module QueryTracer
43
23
  end
44
24
 
45
25
  def skip_query?(sql)
46
- !EXCLUDE_SQL.select { |expr| sql =~ expr }.blank?
26
+ !QueryTracer.config.exclude_sql.select { |expr| sql =~ expr }.blank?
47
27
  end
48
28
 
49
29
  end
data/lib/query_tracer.rb CHANGED
@@ -1,4 +1,48 @@
1
- require 'query_tracer/configuration'
2
1
  require 'query_tracer/tracer'
3
2
  require 'query_tracer/logger'
4
- require 'query_tracer/db'
3
+ require 'query_tracer/db'
4
+
5
+ module QueryTracer
6
+
7
+ def self.configure
8
+ @config = OpenStruct.new({
9
+ :enabled => true,
10
+ :colorize => true,
11
+ :show_revision => true,
12
+ :multiline => true,
13
+ :log_level => :debug,
14
+ :exclude_sql => [],
15
+ :root => Rails.root
16
+ })
17
+
18
+ @config.include_codepoints = [
19
+ %r{^#{@config.root}/(app/presenters/.*)},
20
+ %r{^#{@config.root}/(app/views/.*)},
21
+ %r{^#{@config.root}/(app/controllers/.*)},
22
+ %r{^#{@config.root}/(app/models/.*)},
23
+ %r{^#{@config.root}/(lib/.*)},
24
+ %r{^#{@config.root}/(spec/.*)},
25
+ %r{^#{@config.root}/(app/.*)},
26
+ %r{^#{@config.root}/(vendor/(?:gems|plugins)/.*)},
27
+ %r{^#{@config.root}/(.*)},
28
+ %r{in `(irb)_binding'}
29
+ ]
30
+
31
+ @config.exclude_codepoint = %r{^(#{@config.root}/(?:vendor/(?:rails|gems/(?:composite_primary_keys|db-charmer)|plugins/(?:paginating_find|acts_as_sluggable))|config/initializers/mysql_adapter_extensions\.rb|tmp/gems|lib/query_tracer))|\.rvm/|/gems/}
32
+
33
+ @config.db_adapter = ActiveRecord::Base.connection.adapter_name.capitalize
34
+
35
+ begin
36
+ @config.exclude_sql << QueryTracer::Db.const_get(@config.db_adapter)::SKIP_QUERIES
37
+ @config.exclude_sql.flatten!
38
+ rescue
39
+ end
40
+
41
+ yield @config
42
+ end
43
+
44
+ def self.config
45
+ @config
46
+ end
47
+
48
+ end
data/query_tracer.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "query_tracer"
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Dmitry Shaposhnik", "Dmytro Shteflyuk"]
8
8
  s.email = ["dmitry@shaposhnik.name", "kpumuk@kpumuk.info"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_tracer
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
  - Dmitry Shaposhnik
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-04-18 00:00:00 +03:00
19
+ date: 2011-04-20 00:00:00 +03:00
20
20
  default_executable:
21
21
  dependencies: []
22
22
 
@@ -36,7 +36,6 @@ files:
36
36
  - README.markdown
37
37
  - Rakefile
38
38
  - lib/query_tracer.rb
39
- - lib/query_tracer/configuration.rb
40
39
  - lib/query_tracer/db.rb
41
40
  - lib/query_tracer/db/mysql.rb
42
41
  - lib/query_tracer/db/sqlite.rb
@@ -1,32 +0,0 @@
1
- module QueryTracer
2
- class Configuration
3
-
4
- class << self
5
-
6
- attr_accessor :show_revision, :enabled, :colorize
7
- attr_accessor :multiline, :log_level
8
-
9
- def set
10
- db_adapter = ActiveRecord::Base.connection.adapter_name.capitalize
11
- begin
12
- QueryTracer::Tracer::EXCLUDE_SQL << QueryTracer::Db.const_get(db_adapter)::SKIP_QUERIES
13
- QueryTracer::Tracer::EXCLUDE_SQL.flatten!
14
- rescue
15
- end
16
-
17
- yield self
18
- end
19
-
20
- def log_level
21
- @log_level || :debug
22
- end
23
-
24
- def skip_queries=(val)
25
- QueryTracer::Tracer::EXCLUDE_SQL << val
26
- QueryTracer::Tracer::EXCLUDE_SQL.flatten!
27
- end
28
-
29
- end
30
-
31
- end
32
- end