sql_tracker 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1760dc0e6fbb610de5ed9dc40d2c50087901f5c0
4
- data.tar.gz: 282699e984535d03896c19d7aaaf5c10fa66e6bb
3
+ metadata.gz: 29226162337ee889c358d175c76c9aea25a6e7ec
4
+ data.tar.gz: 51c36a794abd3c99398247a8884cfc5bd472a982
5
5
  SHA512:
6
- metadata.gz: 381fbceeb15cfedf0ca60955e962577287ceebcdc5c674689512419552ed463c87d4dfc443e823ecff4a01ece2800914dae59c847c58b71723faabb2f8b731e3
7
- data.tar.gz: ab777cb8498dbdf20da9eb702bf843f665d32b7e55e7502e1c11fc7fd9b2960fa7897fc15f2e7d26d8450959be31382c6fadf958b6b2700986eec9e5eb395eba
6
+ metadata.gz: 8d1d5d91215717b8ab3a928a0a2568683fa26e2fb4d782b166b4ac5218bab710963fa1778717dfe7fa1679e9859ea216214636233db36b132d12cc3af666cca2
7
+ data.tar.gz: e87a892f671de2faedce163085e4ec89326581ab572550a856ba6628517ccb445c7bc200392701e462106f556e1a3142a40451131333668909403438a1a04ecb
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
+ cache: bundler
3
4
  rvm:
4
5
  - 2.3.0
5
- before_install: gem install bundler -v 1.12.4
6
+ before_install:
7
+ - gem install bundler
8
+ - gem update bundler
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Rails SQL Query Tracker
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/steventen/sql_tracker/badges/gpa.svg)](https://codeclimate.com/github/steventen/sql_tracker)
4
+ [![Build Status](https://travis-ci.org/steventen/sql_tracker.svg?branch=master)](https://travis-ci.org/steventen/sql_tracker)
5
+
3
6
  `sql_tracker` tracks SQL queries by subscribing to Rails' `sql.active_record` event notifications.
4
7
 
5
8
  It then aggregates and generates report to give you insights about all the sql queries happened in your Rails application.
@@ -34,6 +37,16 @@ To generate report, run
34
37
  sql_tracker tmp/sql_tracker-*.json
35
38
  ```
36
39
 
40
+ ## Configurations
41
+
42
+ All the configurable variables and their defaults are list below:
43
+ ```ruby
44
+ SqlTracker::Config.enabled = true
45
+ SqlTracker::Config.tracked_paths = %w(app lib)
46
+ SqlTracker::Config.tracked_sql_command = %w(SELECT INSERT UPDATE DELETE)
47
+ SqlTracker::Config.output_path = File.join(Rails.root.to_s, 'tmp')
48
+ ```
49
+
37
50
  ## License
38
51
 
39
52
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -10,7 +10,13 @@ module SqlTracker
10
10
  self.enabled = enabled.nil? ? true : enabled
11
11
  self.tracked_paths ||= %w(app lib)
12
12
  self.tracked_sql_command ||= %w(SELECT INSERT UPDATE DELETE)
13
- self.output_path ||= File.join(Rails.root.to_s, 'tmp')
13
+ self.output_path ||= begin
14
+ if defined?(::Rails) && ::Rails.root
15
+ File.join(::Rails.root.to_s, 'tmp')
16
+ else
17
+ 'tmp'
18
+ end
19
+ end
14
20
  self
15
21
  end
16
22
  end
@@ -4,24 +4,26 @@ require 'fileutils'
4
4
 
5
5
  module SqlTracker
6
6
  class Handler
7
+ attr_reader :data
8
+
7
9
  def initialize(config)
8
10
  @config = config
9
11
  @started_at = Time.now.to_s
10
12
  @data = {} # {key: {sql:, count:, duration, source: []}, ...}
11
13
  end
12
14
 
13
- def call(name, started, finished, id, payload)
15
+ def call(_name, started, finished, _id, payload)
14
16
  return unless @config.enabled
15
17
 
16
18
  sql = payload[:sql]
17
- return unless sql.start_with?(*@config.tracked_sql_command)
19
+ return unless track?(sql)
18
20
 
19
21
  cleaned_trace = clean_trace(caller)
20
22
  return if cleaned_trace.empty?
21
23
 
22
24
  sql = clean_sql_query(sql)
23
25
  duration = 1000.0 * (finished - started) # in milliseconds
24
- sql_key = Digest::MD5.hexdigest(sql)
26
+ sql_key = Digest::MD5.hexdigest(sql.downcase)
25
27
 
26
28
  if @data.key?(sql_key)
27
29
  update_data(sql_key, cleaned_trace, duration)
@@ -30,23 +32,42 @@ module SqlTracker
30
32
  end
31
33
  end
32
34
 
35
+ def track?(sql)
36
+ return true unless @config.tracked_sql_command.respond_to?(:join)
37
+ tracked_sql_matcher =~ sql
38
+ end
39
+
40
+ def tracked_sql_matcher
41
+ @tracked_sql_matcher ||= /\A#{@config.tracked_sql_command.join('|')}/i
42
+ end
43
+
44
+ def trace_path_matcher
45
+ @trace_path_matcher ||= %r{^(#{@config.tracked_paths.join('|')})\/}
46
+ end
47
+
33
48
  def clean_sql_query(query)
34
49
  query.squish!
35
50
  query.gsub!(/(\s(=|>|<|>=|<=|<>|!=)\s)('[^']+'|\w+)/, '\1xxx')
36
- query.gsub!(/(\sIN\s)\([^\(\)]+\)/, '\1(xxx)')
37
- query.gsub!(/BETWEEN ('[^']+'|\w+) AND ('[^']+'|\w+)/, 'BETWEEN xxx AND xxx')
51
+ query.gsub!(/(\sIN\s)\([^\(\)]+\)/i, '\1(xxx)')
52
+ query.gsub!(/(\sBETWEEN\s)('[^']+'|\w+)(\sAND\s)('[^']+'|\w+)/i, '\1xxx\3xxx')
38
53
  query
39
54
  end
40
55
 
41
56
  def clean_trace(trace)
57
+ return trace unless defined?(::Rails)
58
+
42
59
  if Rails.backtrace_cleaner.instance_variable_get(:@root) == '/'
43
60
  Rails.backtrace_cleaner.instance_variable_set :@root, Rails.root.to_s
44
61
  end
45
62
 
46
63
  Rails.backtrace_cleaner.remove_silencers!
47
- Rails.backtrace_cleaner.add_silencer do |line|
48
- line !~ %r{^(#{@config.tracked_paths.join('|')})\/}
64
+
65
+ if config.tracked_paths.respond_to?(:join)
66
+ Rails.backtrace_cleaner.add_silencer do |line|
67
+ line !~ trace_path_matcher
68
+ end
49
69
  end
70
+
50
71
  Rails.backtrace_cleaner.clean(trace)
51
72
  end
52
73
 
@@ -1,3 +1,3 @@
1
1
  module SqlTracker
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_tracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Yue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-30 00:00:00.000000000 Z
11
+ date: 2016-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler