sql_trace 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +55 -0
- data/lib/sql_trace.rb +2 -0
- data/lib/sql_trace/logger.rb +46 -0
- data/lib/sql_trace/railtie.rb +9 -0
- data/lib/sql_trace/version.rb +3 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 28b2a6f7fa62baecb65365d1daabebbd4d3d604d
|
4
|
+
data.tar.gz: ee441e97cfd90675adbc0ae4eedaa2d4b773528b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 811dba3302d997b8cba7a0b61f24d52b535bcdb778a5be5f51eeccf07e756d7cbb0be62cb8e20bf8597f20ee5cd49da66e131759af8fc29b9167c08700b2e9b1
|
7
|
+
data.tar.gz: 90c265b578628b01c272862c2e4b93ef97a23f1230f50fbdb3de106f17c5f64672a7b8c45fff665230c3d2e62178c36b91f88ec4ae970670c03f2e0573c755af
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# sql_trace
|
2
|
+
|
3
|
+
Ever wondered where do those SQL calls come from in your Rails app?
|
4
|
+
|
5
|
+
**sql_trace** allows to trace code that makes ActiveRecord SQL queries that
|
6
|
+
appear in development server's log. It also allows to filter them in order
|
7
|
+
to trace heavy ones or ones with matching SQL.
|
8
|
+
|
9
|
+
This makes it very useful during app-db optimizations.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add to `Gemfile`:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'sql_trace', group: :development
|
17
|
+
```
|
18
|
+
|
19
|
+
Then run:
|
20
|
+
|
21
|
+
bundle install
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Start development server like this:
|
26
|
+
|
27
|
+
SQL_TRACE=1 rails server
|
28
|
+
|
29
|
+
This way you'll get 5-line traces for all SQL queries. You can customize
|
30
|
+
that using options described below.
|
31
|
+
|
32
|
+
## Options
|
33
|
+
|
34
|
+
You can write options when invoking Rails server or store them in *.env*
|
35
|
+
file and use `dotenv` or `foreman` gems to load them.
|
36
|
+
|
37
|
+
Following options are available:
|
38
|
+
|
39
|
+
##### SQL_TRACE=N
|
40
|
+
|
41
|
+
Changes the stack size to N (must be at least 2, defaults to 5).
|
42
|
+
|
43
|
+
##### SQL_TRACE_COLOR=cyan
|
44
|
+
|
45
|
+
Uses [colorize](https://github.com/fazibear/colorize) gem to make traces
|
46
|
+
look distinct within server log. The gem must be installed separately.
|
47
|
+
|
48
|
+
##### SQL_TRACE_MIN_DURATION=250
|
49
|
+
|
50
|
+
Only prints traces for queries that take at least specified duration.
|
51
|
+
Duration is in **ms**.
|
52
|
+
|
53
|
+
##### SQL_TRACE_MATCH='FROM "users"'
|
54
|
+
|
55
|
+
Only prints traces for queries whose SQL match given string or /regex/
|
data/lib/sql_trace.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module SqlTrace
|
2
|
+
class Logger
|
3
|
+
class << self
|
4
|
+
def start
|
5
|
+
if (sql_trace = ENV['SQL_TRACE'].to_i) > 0
|
6
|
+
stack_size = sql_trace > 1 ? sql_trace : 5
|
7
|
+
stack_root = Rails.root.to_s
|
8
|
+
min_duration = ENV['SQL_TRACE_MIN_DURATION'].to_i
|
9
|
+
|
10
|
+
match = if (raw_match = ENV['SQL_TRACE_MATCH']).present?
|
11
|
+
raw_match.match(/^\/.*\/$/) ? Regexp.compile(raw_match[1..-2]) : Regexp.quote(raw_match)
|
12
|
+
end
|
13
|
+
|
14
|
+
if color = ENV['SQL_TRACE_COLOR']
|
15
|
+
require 'colorize'
|
16
|
+
end
|
17
|
+
|
18
|
+
ActiveSupport::Notifications.subscribe("sql.active_record") do |n, s, e, i, payload|
|
19
|
+
name = payload[:name]
|
20
|
+
sql = payload[:sql]
|
21
|
+
duration = ActiveSupport::Notifications::Event.new(n, s, e, i, payload).duration
|
22
|
+
|
23
|
+
unless name.in?(%w{CACHE SCHEMA}) ||
|
24
|
+
(min_duration > 0 && duration < min_duration) ||
|
25
|
+
(match && !sql.match(match))
|
26
|
+
stack = caller.select do |line|
|
27
|
+
line.start_with?(stack_root)
|
28
|
+
end[0..stack_size - 1].map do |line|
|
29
|
+
line.sub("#{stack_root}/", '')
|
30
|
+
end
|
31
|
+
|
32
|
+
if stack.present?
|
33
|
+
stack.each do |line|
|
34
|
+
line = " #{line}"
|
35
|
+
line = line.send(color) if color
|
36
|
+
|
37
|
+
puts line
|
38
|
+
end && puts
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sql_trace
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Karol Słuszniak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Trace ActiveRecord calls in development console
|
56
|
+
email: karol@cloudless.pl
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files:
|
60
|
+
- README.md
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- lib/sql_trace.rb
|
64
|
+
- lib/sql_trace/logger.rb
|
65
|
+
- lib/sql_trace/railtie.rb
|
66
|
+
- lib/sql_trace/version.rb
|
67
|
+
homepage: http://github.com/karolsluszniak/sql-trace
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.4.6
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Trace ActiveRecord calls in development console
|
91
|
+
test_files: []
|