spring_onion 0.1.3 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -3
- data/lib/spring_onion.rb +1 -0
- data/lib/spring_onion/config.rb +11 -12
- data/lib/spring_onion/explainer.rb +14 -21
- data/lib/spring_onion/json_logger.rb +25 -0
- data/lib/spring_onion/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72b6f835a238acda75a1f45b10f30e79af2ea57f1c65cce762579efb2696aaa3
|
4
|
+
data.tar.gz: 8980630d0a4c270a5aaed49a1b1b47ec44b6ebf94f9bce8b3ee734427329a4ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d8c45b21bd448d30149367efbc6e43cdefd58790f403b216c40ff02eae1642fb4b0bf69838591061f12c8db1618e4f2aeeeb08d8c6359db8a0973930a95e94d
|
7
|
+
data.tar.gz: 2847712ab07bcd40b4115e4efc0190dec21a1a75b500de3b0293f72ba0e121c48c24fe8d6c2f8227be2453d0430af61f8ecd253fa8135b8eec82da2304328616
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -39,8 +39,8 @@ SpringOnion.source_filter_re = //
|
|
39
39
|
|
40
40
|
class Employee < ActiveRecord::Base; end
|
41
41
|
|
42
|
-
Employee.all.to_a.count
|
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}],"
|
42
|
+
p Employee.all.to_a.count
|
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
|
```
|
46
46
|
|
@@ -65,7 +65,7 @@ Employee.all.to_a.count
|
|
65
65
|
"Extra": null
|
66
66
|
}
|
67
67
|
],
|
68
|
-
"
|
68
|
+
"warnings": {
|
69
69
|
"line 1": [
|
70
70
|
"slow_type"
|
71
71
|
]
|
data/lib/spring_onion.rb
CHANGED
data/lib/spring_onion/config.rb
CHANGED
@@ -17,7 +17,7 @@ module SpringOnion
|
|
17
17
|
'Using temporary'
|
18
18
|
)
|
19
19
|
|
20
|
-
@
|
20
|
+
@warnings = {
|
21
21
|
slow_select_type: ->(exp) { SLOW_SELECT_TYPE_RE =~ exp['select_type'] },
|
22
22
|
slow_type: ->(exp) { SLOW_TYPE_RE =~ exp['type'] },
|
23
23
|
slow_possible_keys: ->(exp) { SLOW_POSSIBLE_KEYS_RE =~ exp['possible_keys'] },
|
@@ -42,28 +42,27 @@ 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
|
-
[RbConfig::TOPDIR, *Gem.path].tap do |ary|
|
50
|
+
[RbConfig::TOPDIR, *Gem.path, '/.rbenv/versions/'].tap do |ary|
|
54
51
|
re = ENV['SPRING_ONION_IGNORE_SOURCE_FILTER_RE']
|
55
52
|
ary << Regexp.new(re) if re
|
56
53
|
end
|
57
54
|
)
|
58
55
|
|
59
56
|
@source_filter = lambda do |backtrace_lines|
|
60
|
-
backtrace_lines.grep_v(@ignore_source_filter_re)
|
57
|
+
backtrace_lines = backtrace_lines.grep_v(@ignore_source_filter_re)
|
58
|
+
idx = backtrace_lines.index { |l| @source_filter_re =~ l }
|
59
|
+
idx ? backtrace_lines.slice(idx..-1) : []
|
61
60
|
end
|
62
61
|
|
63
62
|
@logger = Logger.new($stdout).tap do |logger|
|
64
|
-
logger.formatter = lambda
|
63
|
+
logger.formatter = lambda do |severity, datetime, _progname, msg|
|
65
64
|
"\n#{self}\t#{severity}\t#{datetime}\t#{msg}\n"
|
66
|
-
|
65
|
+
end
|
67
66
|
end
|
68
67
|
|
69
68
|
@trace_len = 3
|
@@ -73,7 +72,7 @@ module SpringOnion
|
|
73
72
|
class << self
|
74
73
|
attr_accessor :enabled,
|
75
74
|
:connection,
|
76
|
-
:
|
75
|
+
:warnings,
|
77
76
|
:sql_filter_re, :ignore_sql_filter_re, :sql_filter,
|
78
77
|
:source_filter_re, :ignore_source_filter_re, :source_filter,
|
79
78
|
:logger,
|
@@ -8,6 +8,8 @@ module SpringOnion
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
+
private
|
12
|
+
|
11
13
|
def _with_explain(sql)
|
12
14
|
begin
|
13
15
|
if SpringOnion.enabled && sql =~ /^SELECT\b/ && SpringOnion.sql_filter.call(sql)
|
@@ -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(exp, sql, 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
|
33
|
-
|
34
|
-
|
34
|
+
def _validate_explain(exp, sql, trace)
|
35
|
+
warnings = SpringOnion.warnings
|
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
|
-
|
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,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spring_onion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- winebarrel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -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
|