rails_tracepoint_stack 0.3.4 → 0.4.0
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/README.md +174 -0
- data/changelog.md +132 -0
- data/lib/generators/rails_tracepoint_stack/install/install_generator.rb +29 -0
- data/lib/rails_tracepoint_stack/depth_tracker.rb +54 -0
- data/lib/rails_tracepoint_stack/filter/custom_trace_selector_filter.rb +1 -1
- data/lib/rails_tracepoint_stack/filter/gem_path.rb +20 -1
- data/lib/rails_tracepoint_stack/filter/rb_config.rb +1 -1
- data/lib/rails_tracepoint_stack/limits.rb +41 -0
- data/lib/rails_tracepoint_stack/log_formatter.rb +179 -7
- data/lib/rails_tracepoint_stack/logger.rb +1 -1
- data/lib/rails_tracepoint_stack/renderer/summary.rb +37 -0
- data/lib/rails_tracepoint_stack/renderer/tree.rb +115 -0
- data/lib/rails_tracepoint_stack/sink/collector.rb +69 -0
- data/lib/rails_tracepoint_stack/sink/log.rb +17 -0
- data/lib/rails_tracepoint_stack/skill_installer.rb +39 -0
- data/lib/rails_tracepoint_stack/templates/skill.md +122 -0
- data/lib/rails_tracepoint_stack/trace.rb +31 -4
- data/lib/rails_tracepoint_stack/trace_filter.rb +1 -1
- data/lib/rails_tracepoint_stack/trace_record.rb +30 -0
- data/lib/rails_tracepoint_stack/trace_session.rb +77 -0
- data/lib/rails_tracepoint_stack/tracer.rb +71 -12
- data/lib/rails_tracepoint_stack/truncator.rb +50 -0
- data/lib/rails_tracepoint_stack/version.rb +1 -1
- data/lib/rails_tracepoint_stack.rb +38 -3
- metadata +50 -12
|
@@ -5,7 +5,7 @@ module RailsTracepointStack
|
|
|
5
5
|
RailsTracepointStack.configuration.logger.info(msg)
|
|
6
6
|
else
|
|
7
7
|
# TODO: Add the support to Rails.env for the default filename
|
|
8
|
-
File.open("log/rails_tracepoint_stack.log",
|
|
8
|
+
File.open("log/rails_tracepoint_stack.log", "a") do |f|
|
|
9
9
|
f.puts msg
|
|
10
10
|
end
|
|
11
11
|
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module RailsTracepointStack
|
|
2
|
+
module Renderer
|
|
3
|
+
# Counts what a session holds, so a reader can tell at a glance whether
|
|
4
|
+
# the capture saw what they expected before reading the tree itself.
|
|
5
|
+
module Summary
|
|
6
|
+
def self.call(session)
|
|
7
|
+
{
|
|
8
|
+
calls: count(session, :call),
|
|
9
|
+
returns: count(session, :return),
|
|
10
|
+
raises: count(session, :raise),
|
|
11
|
+
classes: session.traces.map(&:class_name).uniq.size,
|
|
12
|
+
filtered: session.filtered_count,
|
|
13
|
+
truncated: session.truncated?
|
|
14
|
+
}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.line(session)
|
|
18
|
+
counts = call(session)
|
|
19
|
+
|
|
20
|
+
[
|
|
21
|
+
pluralize(counts[:calls], "call"),
|
|
22
|
+
pluralize(counts[:returns], "return"),
|
|
23
|
+
pluralize(counts[:raises], "raise"),
|
|
24
|
+
pluralize(counts[:classes], "class", "classes")
|
|
25
|
+
].join(", ")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.count(session, kind)
|
|
29
|
+
session.traces.count { |record| record.kind == kind }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.pluralize(count, singular, plural = "#{singular}s")
|
|
33
|
+
"#{count} #{(count == 1) ? singular : plural}"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module RailsTracepointStack
|
|
4
|
+
module Renderer
|
|
5
|
+
# Renders a session as an indented call tree.
|
|
6
|
+
#
|
|
7
|
+
# The format is built for reading in a terminal or pasting into a prompt,
|
|
8
|
+
# so it favours short lines: paths are relative to the working directory,
|
|
9
|
+
# values are compact JSON, and a return sits one level under the call it
|
|
10
|
+
# belongs to.
|
|
11
|
+
module Tree
|
|
12
|
+
INDENT = " ".freeze
|
|
13
|
+
|
|
14
|
+
def self.call(session)
|
|
15
|
+
lines = session.traces.map { |record| line_for(record) }
|
|
16
|
+
lines << truncation_notice if session.truncated?
|
|
17
|
+
lines << Summary.line(session)
|
|
18
|
+
lines << empty_notice(session) if session.traces.empty?
|
|
19
|
+
lines.join("\n")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# An empty tree reads like a broken tool. It usually means the block ran
|
|
23
|
+
# entirely inside gems - a bare ActiveRecord query calls no method the
|
|
24
|
+
# developer wrote - so say which of the two happened.
|
|
25
|
+
def self.empty_notice(session)
|
|
26
|
+
return "no code ran inside the block" if session.filtered_count.to_i.zero?
|
|
27
|
+
|
|
28
|
+
"no app code ran: #{session.filtered_count} traces from gems, the framework " \
|
|
29
|
+
"and Ruby itself were filtered out"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.line_for(record)
|
|
33
|
+
case record.kind
|
|
34
|
+
when :call then call_line(record)
|
|
35
|
+
when :return then return_line(record)
|
|
36
|
+
when :raise then raise_line(record)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
SINGLETON_CLASS = /\A#<Class:([A-Z][\w:]*)>\z/
|
|
41
|
+
TEMPLATE_FILE = /\.(erb|haml|slim|builder|jbuilder|rabl)\z/i
|
|
42
|
+
# Everything a template receives besides the locals belongs to the
|
|
43
|
+
# rendering machinery, not to the developer.
|
|
44
|
+
TEMPLATE_LOCALS = "local_assigns".freeze
|
|
45
|
+
|
|
46
|
+
def self.call_line(record)
|
|
47
|
+
return template_line(record) if template?(record)
|
|
48
|
+
|
|
49
|
+
"#{indent(record.depth)}#{qualified_name(record)} " \
|
|
50
|
+
"(#{location(record)}) #{compact(record.params)}"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# A class method is defined on the singleton class, which prints as
|
|
54
|
+
# #<Class:Foo>. Ruby writes that call as Foo.bar, so the tree does too.
|
|
55
|
+
# An anonymous singleton prints as an address and is left alone, since
|
|
56
|
+
# turning that into `0x00007f1234.render` helps nobody.
|
|
57
|
+
def self.qualified_name(record)
|
|
58
|
+
singleton = SINGLETON_CLASS.match(record.class_name.to_s)
|
|
59
|
+
return "#{singleton[1]}.#{record.method_name}" if singleton
|
|
60
|
+
|
|
61
|
+
"#{record.class_name}##{record.method_name}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# A compiled template is a generated method on an anonymous class, named
|
|
65
|
+
# after a hash of the file. None of that is worth showing: the path is.
|
|
66
|
+
def self.template?(record)
|
|
67
|
+
TEMPLATE_FILE.match?(record.file_path.to_s)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def self.template_line(record)
|
|
71
|
+
"#{indent(record.depth)}render #{relative_path(record.file_path)} " \
|
|
72
|
+
"#{compact(template_locals(record))}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.template_locals(record)
|
|
76
|
+
params = record.params
|
|
77
|
+
return {} unless params.is_a?(Hash)
|
|
78
|
+
|
|
79
|
+
params.fetch(TEMPLATE_LOCALS, {})
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.return_line(record)
|
|
83
|
+
"#{indent(record.depth + 1)}-> #{compact(record.return_value)}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def self.raise_line(record)
|
|
87
|
+
"#{indent(record.depth + 1)}!! #{record.exception_class}: #{record.exception_message}"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def self.truncation_notice
|
|
91
|
+
"... truncated: the limit was reached before the block finished"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.indent(depth)
|
|
95
|
+
INDENT * depth
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def self.location(record)
|
|
99
|
+
"#{relative_path(record.file_path)}:#{record.line_number}"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def self.relative_path(file_path)
|
|
103
|
+
return file_path unless file_path.to_s.start_with?("#{Dir.pwd}/")
|
|
104
|
+
|
|
105
|
+
file_path[(Dir.pwd.length + 1)..]
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def self.compact(value)
|
|
109
|
+
JSON.generate(value)
|
|
110
|
+
rescue SystemStackError, StandardError
|
|
111
|
+
value.to_s
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require "rails_tracepoint_stack/limits"
|
|
2
|
+
require "rails_tracepoint_stack/log_formatter"
|
|
3
|
+
require "rails_tracepoint_stack/trace_record"
|
|
4
|
+
require "rails_tracepoint_stack/trace_session"
|
|
5
|
+
require "rails_tracepoint_stack/truncator"
|
|
6
|
+
|
|
7
|
+
module RailsTracepointStack
|
|
8
|
+
module Sink
|
|
9
|
+
# Turns live traces into immutable records held in memory, so a caller can
|
|
10
|
+
# inspect them once the traced block is done.
|
|
11
|
+
class Collector
|
|
12
|
+
attr_reader :session, :limits
|
|
13
|
+
|
|
14
|
+
def initialize(session: RailsTracepointStack::TraceSession.new, limits: RailsTracepointStack::Limits.new)
|
|
15
|
+
@session = session
|
|
16
|
+
@limits = limits
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def record(trace)
|
|
20
|
+
return if limits.too_deep?(trace.depth)
|
|
21
|
+
|
|
22
|
+
unless limits.room_for?(session.traces.size)
|
|
23
|
+
session.truncated = true
|
|
24
|
+
return
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
session.add(build_record(trace))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def build_record(trace)
|
|
33
|
+
exception = trace.exception
|
|
34
|
+
|
|
35
|
+
RailsTracepointStack::TraceRecord.new(
|
|
36
|
+
kind: trace.kind,
|
|
37
|
+
class_name: RailsTracepointStack::LogFormatter.stringify(trace.class_name),
|
|
38
|
+
method_name: trace.method_name,
|
|
39
|
+
file_path: trace.file_path,
|
|
40
|
+
line_number: trace.line_number,
|
|
41
|
+
params: params_for(trace),
|
|
42
|
+
return_value: return_value_for(trace),
|
|
43
|
+
exception_class: exception && exception.class.to_s,
|
|
44
|
+
exception_message: exception && RailsTracepointStack::LogFormatter.stringify(exception.message),
|
|
45
|
+
depth: trace.depth || 0
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def params_for(trace)
|
|
50
|
+
return {} unless limits.capture_params
|
|
51
|
+
|
|
52
|
+
snapshot(trace.params)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def return_value_for(trace)
|
|
56
|
+
return nil unless limits.capture_return
|
|
57
|
+
|
|
58
|
+
snapshot(trace.return_value)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def snapshot(value)
|
|
62
|
+
RailsTracepointStack::Truncator.call(
|
|
63
|
+
RailsTracepointStack::LogFormatter.safe_value(value),
|
|
64
|
+
limits
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require "rails_tracepoint_stack/logger"
|
|
2
|
+
require "rails_tracepoint_stack/log_formatter"
|
|
3
|
+
|
|
4
|
+
module RailsTracepointStack
|
|
5
|
+
module Sink
|
|
6
|
+
# Formats each trace and writes it out, which is what the gem has always
|
|
7
|
+
# done. Kept as the default so enabling the tracer globally behaves the
|
|
8
|
+
# same as before sinks existed.
|
|
9
|
+
class Log
|
|
10
|
+
def record(trace)
|
|
11
|
+
RailsTracepointStack::Logger.log(
|
|
12
|
+
RailsTracepointStack::LogFormatter.message(trace)
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require "fileutils"
|
|
2
|
+
|
|
3
|
+
module RailsTracepointStack
|
|
4
|
+
# Copies the packaged agent skill into the host app.
|
|
5
|
+
#
|
|
6
|
+
# A skill living in this gem's repository only helps someone working on the
|
|
7
|
+
# gem. Agents look for skills inside the project they are working on, so the
|
|
8
|
+
# file has to land there for the gem to ever get picked up on its own.
|
|
9
|
+
class SkillInstaller
|
|
10
|
+
SKILL_PATH = ".claude/skills/debug-with-tracepoint/SKILL.md".freeze
|
|
11
|
+
TEMPLATE_PATH = File.expand_path("templates/skill.md", __dir__).freeze
|
|
12
|
+
|
|
13
|
+
attr_reader :destination, :force
|
|
14
|
+
|
|
15
|
+
def initialize(destination: Dir.pwd, force: false)
|
|
16
|
+
@destination = destination
|
|
17
|
+
@force = force
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Returns the path written, or nil when a skill was already there.
|
|
21
|
+
def install
|
|
22
|
+
return nil if File.exist?(target_path) && !force
|
|
23
|
+
|
|
24
|
+
FileUtils.mkdir_p(File.dirname(target_path))
|
|
25
|
+
File.write(target_path, template)
|
|
26
|
+
target_path
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def target_path
|
|
30
|
+
File.join(destination, SKILL_PATH)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def template
|
|
36
|
+
File.read(TEMPLATE_PATH)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: debug-with-tracepoint
|
|
3
|
+
description: Use when you need to know what a Ruby/Rails code path actually did at runtime - which methods ran, what arguments they got, what each one returned, and where an exception was first raised. Reach for this before adding puts/Rails.logger lines or reading call sites by hand, and whenever a value is wrong but you cannot tell which method produced it.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Debugging with rails_tracepoint_stack
|
|
7
|
+
|
|
8
|
+
Traces one block of code and hands back the call tree: every method your app
|
|
9
|
+
defined that ran, with its arguments, its return value, and any exception,
|
|
10
|
+
nested by call depth. Gems, the framework and stdlib are filtered out.
|
|
11
|
+
|
|
12
|
+
## When this beats the alternatives
|
|
13
|
+
|
|
14
|
+
| Question | Use this |
|
|
15
|
+
|---|---|
|
|
16
|
+
| "Which of these methods is returning nil?" | Yes - return values are the point |
|
|
17
|
+
| "Where did this exception actually start?" | Yes - the raise is tagged in the tree |
|
|
18
|
+
| "What is the real order of calls here?" | Yes - the tree shows nesting |
|
|
19
|
+
| "What is the value of one variable right here?" | No - a `binding.irb` is cheaper |
|
|
20
|
+
| "Is this endpoint slow?" | No - this measures nothing |
|
|
21
|
+
|
|
22
|
+
## Run it
|
|
23
|
+
|
|
24
|
+
One shot, no config file, no server restart, output bounded:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
bin/rails runner '
|
|
28
|
+
session = RailsTracepointStack.capture(max_depth: 4) do
|
|
29
|
+
Order.find(42).recalculate!
|
|
30
|
+
end
|
|
31
|
+
puts session.to_tree
|
|
32
|
+
'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Reading the output:
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
Order#recalculate! (app/models/order.rb:88) {}
|
|
39
|
+
Order#apply_discount (app/models/order.rb:102) {"total":200.0}
|
|
40
|
+
Discount#rate_for (app/models/discount.rb:12) {"order":"#<Order id: 42>"}
|
|
41
|
+
-> null <- rate_for returned nil, which is the bug
|
|
42
|
+
-> 200.0
|
|
43
|
+
-> 200.0
|
|
44
|
+
3 calls, 3 returns, 0 raises, 2 classes
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- indentation is call depth
|
|
48
|
+
- `-> value` is what the method on the line above returned
|
|
49
|
+
- `!! ArgumentError: message` marks where an exception was raised
|
|
50
|
+
- a `-> null` right after a `!!` is the frame unwinding, not a real return
|
|
51
|
+
- `render app/views/...` is a template; the `{...}` after it are its locals
|
|
52
|
+
- the last line is the summary; `... truncated` means a limit cut it short
|
|
53
|
+
|
|
54
|
+
### An empty tree is an answer, not a failure
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
0 calls, 0 returns, 0 raises, 0 classes
|
|
58
|
+
no app code ran: 34025 traces from gems, the framework and Ruby itself were filtered out
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
This means the block ran entirely inside gems. A bare `Order.where(...).map(&:name)`
|
|
62
|
+
calls no method anyone in this app wrote — `name` is generated by ActiveRecord.
|
|
63
|
+
Do not conclude the tool is broken and fall back to `puts`. Either widen the
|
|
64
|
+
capture to include the code that calls into this, or accept that the bug is not
|
|
65
|
+
in app code. `session.filtered_count` is that number if you want it directly.
|
|
66
|
+
|
|
67
|
+
## Keep the output small
|
|
68
|
+
|
|
69
|
+
`capture` is scoped to the calling thread and takes limits. Use them - an
|
|
70
|
+
unbounded capture of a real request is tens of thousands of lines.
|
|
71
|
+
|
|
72
|
+
| Option | Default | Use it to |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| `max_depth:` | none | Stay near the top of the tree |
|
|
75
|
+
| `max_traces:` | 5000 | Cap total lines |
|
|
76
|
+
| `max_string_length:` | 200 | Keep big payloads readable |
|
|
77
|
+
| `max_collection_size:` | 20 | Keep loaded associations readable |
|
|
78
|
+
| `capture_params: false` | on | Show only the flow |
|
|
79
|
+
| `capture_return: false` | on | Show only the calls |
|
|
80
|
+
| `threads: :all` | current only | Include background threads |
|
|
81
|
+
|
|
82
|
+
To narrow by file instead, set patterns before capturing:
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
RailsTracepointStack.configure do |config|
|
|
86
|
+
config.file_path_to_filter_patterns << %r{app/services/}
|
|
87
|
+
config.ignore_patterns << %r{app/models/concerns/}
|
|
88
|
+
end
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Other shapes of the same data
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
session.as_json # structured: one entry per trace
|
|
95
|
+
session.summary # counts only: {calls:, returns:, raises:, classes:, truncated:}
|
|
96
|
+
session.result # what the block itself returned
|
|
97
|
+
session.error # the exception that escaped, if any
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`capture` re-raises whatever the block raised. To keep the traces in that
|
|
101
|
+
case, take the session from the block argument:
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
session = nil
|
|
105
|
+
begin
|
|
106
|
+
RailsTracepointStack.capture { |s| session = s; thing_that_blows_up }
|
|
107
|
+
rescue => error
|
|
108
|
+
puts session.to_tree
|
|
109
|
+
end
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Worth knowing
|
|
113
|
+
|
|
114
|
+
- Requires the gem in the app: `gem "rails_tracepoint_stack"`.
|
|
115
|
+
- TracePoint slows the traced block down noticeably. Fine for a one-off
|
|
116
|
+
investigation, not for anything left running.
|
|
117
|
+
- Only methods defined in the app appear. If a method you expected is
|
|
118
|
+
missing, it is probably in a gem - add its path to
|
|
119
|
+
`file_path_to_filter_patterns` to force it in.
|
|
120
|
+
- `RAILS_TRACEPOINT_STACK_ENABLED=true` traces the whole process to
|
|
121
|
+
`log/rails_tracepoint_stack.log` instead. Prefer `capture` unless you need
|
|
122
|
+
to trace something you cannot wrap in a block, such as boot.
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "forwardable"
|
|
2
2
|
|
|
3
3
|
module RailsTracepointStack
|
|
4
4
|
class Trace
|
|
5
5
|
extend Forwardable
|
|
6
6
|
|
|
7
7
|
attr_reader :params, :trace_point
|
|
8
|
+
attr_accessor :depth
|
|
8
9
|
|
|
9
10
|
def_delegator :@trace_point, :defined_class, :class_name
|
|
10
11
|
def_delegator :@trace_point, :method_id, :method_name
|
|
@@ -15,16 +16,42 @@ module RailsTracepointStack
|
|
|
15
16
|
@trace_point = trace_point
|
|
16
17
|
end
|
|
17
18
|
|
|
19
|
+
def kind
|
|
20
|
+
trace_point.event
|
|
21
|
+
end
|
|
22
|
+
|
|
18
23
|
def params
|
|
24
|
+
return {} unless kind == :call
|
|
25
|
+
|
|
19
26
|
@params ||= fetch_params(trace_point)
|
|
20
27
|
end
|
|
21
28
|
|
|
29
|
+
def return_value
|
|
30
|
+
return nil unless kind == :return
|
|
31
|
+
|
|
32
|
+
trace_point.return_value
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def exception
|
|
36
|
+
return nil unless kind == :raise
|
|
37
|
+
|
|
38
|
+
trace_point.raised_exception
|
|
39
|
+
end
|
|
40
|
+
|
|
22
41
|
private
|
|
23
42
|
|
|
43
|
+
# Every local the method body declares is already in scope at :call time,
|
|
44
|
+
# holding nil. Reading the whole binding would report those as arguments
|
|
45
|
+
# the caller passed as nil, so the parameter list decides what to read.
|
|
24
46
|
def fetch_params(trace_point)
|
|
25
|
-
trace_point.binding
|
|
26
|
-
|
|
27
|
-
|
|
47
|
+
binding = trace_point.binding
|
|
48
|
+
declared = binding.local_variables
|
|
49
|
+
|
|
50
|
+
trace_point.parameters.each_with_object({}) do |(_type, name), params|
|
|
51
|
+
next if name.nil? || !declared.include?(name)
|
|
52
|
+
|
|
53
|
+
params[name] = binding.local_variable_get(name)
|
|
54
|
+
end
|
|
28
55
|
end
|
|
29
56
|
end
|
|
30
57
|
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module RailsTracepointStack
|
|
2
|
+
# An immutable snapshot of a single trace. The TracePoint object is only
|
|
3
|
+
# valid while its event is being handled, so anything worth keeping has to
|
|
4
|
+
# be copied out before the handler returns.
|
|
5
|
+
TraceRecord = Struct.new(
|
|
6
|
+
:kind,
|
|
7
|
+
:class_name,
|
|
8
|
+
:method_name,
|
|
9
|
+
:file_path,
|
|
10
|
+
:line_number,
|
|
11
|
+
:params,
|
|
12
|
+
:return_value,
|
|
13
|
+
:exception_class,
|
|
14
|
+
:exception_message,
|
|
15
|
+
:depth,
|
|
16
|
+
keyword_init: true
|
|
17
|
+
) do
|
|
18
|
+
def call?
|
|
19
|
+
kind == :call
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def return?
|
|
23
|
+
kind == :return
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def raise?
|
|
27
|
+
kind == :raise
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "rails_tracepoint_stack/renderer/summary"
|
|
3
|
+
require "rails_tracepoint_stack/renderer/tree"
|
|
4
|
+
|
|
5
|
+
module RailsTracepointStack
|
|
6
|
+
# The traces gathered by one capture, plus whatever the traced block itself
|
|
7
|
+
# produced.
|
|
8
|
+
class TraceSession
|
|
9
|
+
attr_reader :traces
|
|
10
|
+
attr_accessor :result, :error, :filtered_count
|
|
11
|
+
attr_writer :truncated
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@traces = []
|
|
15
|
+
@truncated = false
|
|
16
|
+
@filtered_count = 0
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def add(record)
|
|
20
|
+
@traces << record
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def empty?
|
|
24
|
+
@traces.empty?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# True when a limit stopped the collection, so the traces are a prefix of
|
|
28
|
+
# what actually ran rather than the whole story.
|
|
29
|
+
def truncated?
|
|
30
|
+
@truncated
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def to_tree
|
|
34
|
+
RailsTracepointStack::Renderer::Tree.call(self)
|
|
35
|
+
end
|
|
36
|
+
alias_method :to_s, :to_tree
|
|
37
|
+
|
|
38
|
+
def summary
|
|
39
|
+
RailsTracepointStack::Renderer::Summary.call(self)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def as_json
|
|
43
|
+
{
|
|
44
|
+
summary: summary,
|
|
45
|
+
traces: traces.map { |record| entry_for(record) }
|
|
46
|
+
}
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def to_json(*args)
|
|
50
|
+
JSON.generate(as_json, *args)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def entry_for(record)
|
|
56
|
+
entry = {
|
|
57
|
+
kind: record.kind,
|
|
58
|
+
depth: record.depth,
|
|
59
|
+
class_name: record.class_name,
|
|
60
|
+
method_name: record.method_name,
|
|
61
|
+
file_path: "#{RailsTracepointStack::Renderer::Tree.relative_path(record.file_path)}:#{record.line_number}",
|
|
62
|
+
params: record.params
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# A method that returned nil is often the answer being looked for, so
|
|
66
|
+
# the key stays even when there is nothing in it.
|
|
67
|
+
entry[:return_value] = record.return_value if record.return?
|
|
68
|
+
|
|
69
|
+
if record.raise?
|
|
70
|
+
entry[:exception_class] = record.exception_class
|
|
71
|
+
entry[:exception_message] = record.exception_message
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
entry
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -1,23 +1,82 @@
|
|
|
1
|
-
#TODO: Move to a loader file
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
1
|
+
# TODO: Move to a loader file
|
|
2
|
+
require "rails_tracepoint_stack/logger"
|
|
3
|
+
require "rails_tracepoint_stack/trace_filter"
|
|
4
|
+
require "rails_tracepoint_stack/trace"
|
|
5
|
+
require "rails_tracepoint_stack/log_formatter"
|
|
6
|
+
require "rails_tracepoint_stack/sink/log"
|
|
7
|
+
require "rails_tracepoint_stack/depth_tracker"
|
|
6
8
|
|
|
7
9
|
module RailsTracepointStack
|
|
8
10
|
class Tracer
|
|
9
11
|
include RailsTracepointStack::TraceFilter
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
extend Forwardable
|
|
13
|
+
|
|
14
|
+
DEFAULT_EVENTS = [:call].freeze
|
|
15
|
+
|
|
16
|
+
def_delegators :@tracer, :enable, :disable
|
|
17
|
+
|
|
18
|
+
# How many traces the filters dropped. A capture that keeps nothing is
|
|
19
|
+
# ambiguous on its own: this tells the difference between code that never
|
|
20
|
+
# ran and code that ran entirely inside gems.
|
|
21
|
+
attr_reader :filtered_count
|
|
22
|
+
|
|
23
|
+
# A nil thread watches every thread in the process, which is what the
|
|
24
|
+
# global tracer wants. Passing one confines tracing to it, so a capture
|
|
25
|
+
# running inside a threaded server does not pick up other requests.
|
|
26
|
+
def initialize(sink: RailsTracepointStack::Sink::Log.new, events: DEFAULT_EVENTS, thread: nil)
|
|
27
|
+
@sink = sink
|
|
28
|
+
@events = events
|
|
29
|
+
@thread = thread
|
|
30
|
+
@filtered_count = 0
|
|
31
|
+
generate_tracer
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def generate_tracer
|
|
37
|
+
@tracer ||= TracePoint.new(*@events) do |tracepoint|
|
|
38
|
+
next if out_of_scope_thread?
|
|
39
|
+
|
|
13
40
|
trace = RailsTracepointStack::Trace.new(trace_point: tracepoint)
|
|
14
41
|
|
|
15
|
-
|
|
42
|
+
if ignore_trace?(trace: trace)
|
|
43
|
+
@filtered_count += 1
|
|
44
|
+
next
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
trace.depth = depth_for(trace)
|
|
48
|
+
@sink.record(trace)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def out_of_scope_thread?
|
|
53
|
+
!@thread.nil? && Thread.current != @thread
|
|
54
|
+
end
|
|
16
55
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
56
|
+
# Only kept traces pay for reading the stack position, so the cost stays
|
|
57
|
+
# proportional to the app code being traced rather than to everything the
|
|
58
|
+
# VM runs underneath it.
|
|
59
|
+
def depth_for(trace)
|
|
60
|
+
tracker = depth_tracker
|
|
61
|
+
raw_position = raw_stack_position
|
|
62
|
+
|
|
63
|
+
case trace.kind
|
|
64
|
+
when :call then tracker.enter(raw_position)
|
|
65
|
+
when :return then tracker.leave(raw_position)
|
|
66
|
+
when :raise then tracker.raised(raw_position)
|
|
20
67
|
end
|
|
21
68
|
end
|
|
69
|
+
|
|
70
|
+
def raw_stack_position
|
|
71
|
+
caller_locations(1)&.length || 0
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def depth_tracker
|
|
75
|
+
Thread.current[depth_tracker_key] ||= RailsTracepointStack::DepthTracker.new
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def depth_tracker_key
|
|
79
|
+
@depth_tracker_key ||= :"rails_tracepoint_stack_depth_#{object_id}"
|
|
80
|
+
end
|
|
22
81
|
end
|
|
23
82
|
end
|