spring_onion 0.2.0 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/spring_onion.rb +1 -0
- data/lib/spring_onion/config.rb +5 -8
- data/lib/spring_onion/explainer.rb +15 -22
- data/lib/spring_onion/json_logger.rb +25 -0
- data/lib/spring_onion/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b40885dd3b876b902fa6670cde6f312362239a5d65da300297f9240cee05afbc
|
4
|
+
data.tar.gz: 3193d9ab4417169c380bb9fe1bf788752b4f347d9c0bdb34b5237a9f5e2c674f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1f37308e5b4dbc00112b4cc7e12a1060fe4c39a8dbb379bb5d10bf3c689151d590095a39e4f732c236993f19efccd8b624bd5baf7f162c3422646f49a377728
|
7
|
+
data.tar.gz: 73aafec72dad64c8e54daa129dd65bbd419f7dc013b94da830228825211a2e3693627185c5e9f50f02b4217f24a244feec7a324a14267c32f0e063c7149febb2
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -39,7 +39,7 @@ SpringOnion.source_filter_re = //
|
|
39
39
|
|
40
40
|
class Employee < ActiveRecord::Base; end
|
41
41
|
|
42
|
-
Employee.all.to_a.count
|
42
|
+
p Employee.all.to_a.count
|
43
43
|
#=> SpringOnion INFO 2020-07-18 01:53:27 +0900 {"sql":"SELECT `employees`.* FROM `employees`","explain":[{"line":1,"select_type":"SIMPLE","table":"employees","partitions":null,"type":"ALL","possible_keys":null,"key":null,"key_len":null,"ref":null,"rows":298936,"filtered":100.0,"Extra":null}],"warnings":{"line 1":["slow_type"]},"backtrace":["/foo/bar/zoo/baz.rb:18:in `\u003ctop (required)\u003e'"]}
|
44
44
|
#=> 300024
|
45
45
|
```
|
data/lib/spring_onion.rb
CHANGED
data/lib/spring_onion/config.rb
CHANGED
@@ -42,12 +42,9 @@ module SpringOnion
|
|
42
42
|
@ignore_sql_filter_re !~ sql && @sql_filter_re =~ sql
|
43
43
|
end
|
44
44
|
|
45
|
-
@source_filter_re =
|
46
|
-
|
47
|
-
|
48
|
-
ary << Regexp.new(re) if re
|
49
|
-
end
|
50
|
-
)
|
45
|
+
@source_filter_re = ENV['SPRING_ONION_SOURCE_FILTER_RE'].yield_self do |re|
|
46
|
+
re ? Regexp.new(re) : %r{/app/}
|
47
|
+
end
|
51
48
|
|
52
49
|
@ignore_source_filter_re = Regexp.union(
|
53
50
|
[RbConfig::TOPDIR, *Gem.path, '/.rbenv/versions/'].tap do |ary|
|
@@ -63,9 +60,9 @@ module SpringOnion
|
|
63
60
|
end
|
64
61
|
|
65
62
|
@logger = Logger.new($stdout).tap do |logger|
|
66
|
-
logger.formatter = lambda
|
63
|
+
logger.formatter = lambda do |severity, datetime, _progname, msg|
|
67
64
|
"\n#{self}\t#{severity}\t#{datetime}\t#{msg}\n"
|
68
|
-
|
65
|
+
end
|
69
66
|
end
|
70
67
|
|
71
68
|
@trace_len = 3
|
@@ -3,14 +3,16 @@
|
|
3
3
|
module SpringOnion
|
4
4
|
module Explainer
|
5
5
|
def execute(*args)
|
6
|
-
_with_explain(args.first) do
|
6
|
+
_with_explain(sql: args.first) do
|
7
7
|
super
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
private
|
12
|
+
|
13
|
+
def _with_explain(sql:)
|
12
14
|
begin
|
13
|
-
if SpringOnion.enabled && sql =~
|
15
|
+
if SpringOnion.enabled && sql =~ /\A\s*SELECT\b/i && SpringOnion.sql_filter.call(sql)
|
14
16
|
trace = SpringOnion.source_filter.call(caller)
|
15
17
|
|
16
18
|
unless trace.length.zero?
|
@@ -19,7 +21,7 @@ module SpringOnion
|
|
19
21
|
|
20
22
|
exp = conn.query("EXPLAIN #{sql}", as: :hash).to_a
|
21
23
|
exp.each { |r| r.delete('id') }
|
22
|
-
|
24
|
+
_validate_explain(sql: sql, exp: exp, trace: trace)
|
23
25
|
end
|
24
26
|
end
|
25
27
|
rescue StandardError => e
|
@@ -29,35 +31,26 @@ module SpringOnion
|
|
29
31
|
yield
|
30
32
|
end
|
31
33
|
|
32
|
-
def
|
34
|
+
def _validate_explain(sql:, exp:, trace:)
|
33
35
|
warnings = SpringOnion.warnings
|
34
|
-
|
36
|
+
warning_names_by_index = {}
|
35
37
|
|
36
38
|
exp.each_with_index do |row, i|
|
37
39
|
warning_names = warnings.select do |_name, validator|
|
38
40
|
validator.call(row)
|
39
41
|
end.keys
|
40
42
|
|
41
|
-
|
43
|
+
warning_names_by_index[i] = warning_names unless warning_names.empty?
|
42
44
|
end
|
43
45
|
|
44
|
-
return if
|
46
|
+
return if warning_names_by_index.empty?
|
45
47
|
|
46
|
-
|
48
|
+
SpringOnion::JsonLogger.log(
|
47
49
|
sql: sql,
|
48
|
-
explain: exp
|
49
|
-
warnings:
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
line = if SpringOnion.json_pretty
|
54
|
-
JSON.pretty_generate(h)
|
55
|
-
else
|
56
|
-
JSON.dump(h)
|
57
|
-
end
|
58
|
-
|
59
|
-
line = CodeRay.scan(line, :json).terminal if SpringOnion.color
|
60
|
-
SpringOnion.logger.info(line)
|
50
|
+
explain: exp,
|
51
|
+
warnings: warning_names_by_index,
|
52
|
+
trace: trace
|
53
|
+
)
|
61
54
|
end
|
62
55
|
end
|
63
56
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SpringOnion
|
4
|
+
module JsonLogger
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def log(sql:, explain:, warnings:, trace:)
|
8
|
+
h = {
|
9
|
+
sql: sql,
|
10
|
+
explain: explain.each_with_index.map { |r, i| { line: i + 1 }.merge(r) },
|
11
|
+
warnings: warnings.transform_keys { |i| "line #{i + 1}" },
|
12
|
+
backtrace: trace.slice(0, SpringOnion.trace_len),
|
13
|
+
}
|
14
|
+
|
15
|
+
line = if SpringOnion.json_pretty
|
16
|
+
JSON.pretty_generate(h)
|
17
|
+
else
|
18
|
+
JSON.dump(h)
|
19
|
+
end
|
20
|
+
|
21
|
+
line = CodeRay.scan(line, :json).terminal if SpringOnion.color
|
22
|
+
SpringOnion.logger.info(line)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/spring_onion/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spring_onion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- winebarrel
|
@@ -142,6 +142,7 @@ files:
|
|
142
142
|
- lib/spring_onion/config.rb
|
143
143
|
- lib/spring_onion/error.rb
|
144
144
|
- lib/spring_onion/explainer.rb
|
145
|
+
- lib/spring_onion/json_logger.rb
|
145
146
|
- lib/spring_onion/version.rb
|
146
147
|
- spring_onion.gemspec
|
147
148
|
homepage: https://github.com/winebarrel/spring_onion
|