rails_tracepoint_stack 0.3.5 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f67c982828a7ad060513cbb50daf9f88399229a534a43cf12a674571b6ae11ae
4
- data.tar.gz: fd80c178bf9de58408e769742d7640b0ccdbcc99a4722ec9ccbd6e68d115ff51
3
+ metadata.gz: 7853cf9027a5db8f962c4cf6753ae98cac10e3cdd0344b618fac59a112837e6d
4
+ data.tar.gz: 29beeae6b2e5eacbca3121a90bef1670e759477b5cfc48aa837dba6ebcb48fe0
5
5
  SHA512:
6
- metadata.gz: 57b7125a7a242bf59f15096fffeef7de3c8a7543162fa7851566a07256d2ed790d39ff9c9dfcd1f3c3f8ca54714b797ed5f5015f2eb0d6f0adbe57e3ad582412
7
- data.tar.gz: cd33cf3e53fe35fe62ba3867cfe8ff56047cceae59e2139c55898fcb0ed0e44d8dccf57a0b66954f1543797c1cb1070b6c68f479f76ce80f0c15282518c50110
6
+ metadata.gz: 49af2952c9312ea180cbf2afde354b1d9c68a0a4f6d94a991534e8f19b6fed4f477afda01cdb4d93e766751a9195f183f12f93c0fdd51705b3c984127271cb7c
7
+ data.tar.gz: 79ff4005947c9a73f08dc03261894e6044653c3e8e1979043546e56425d0fb718587562707957c868c55930ad02606fca3abe5edd29d19b9d1b57d0b4b69938c
data/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # rails_tracepoint_stack
2
+
3
+ **[carlosdanielpohlod.github.io/rails_tracepoint_stack](https://carlosdanielpohlod.github.io/rails_tracepoint_stack/)**
4
+
5
+ See what your Rails code actually did at runtime: which of *your* methods ran,
6
+ what arguments they got, what each one returned, and where an exception was
7
+ first raised — as a call tree, with gems, the framework and stdlib filtered out.
8
+
9
+ ```ruby
10
+ session = RailsTracepointStack.capture(max_depth: 4) do
11
+ Order.find(42).recalculate!
12
+ end
13
+
14
+ puts session.to_tree
15
+ ```
16
+
17
+ ```
18
+ Order#recalculate! (app/models/order.rb:88) {}
19
+ Order#apply_discount (app/models/order.rb:102) {"total":200.0}
20
+ Discount#rate_for (app/models/discount.rb:12) {"order":"#<Order id: 42>"}
21
+ -> null
22
+ -> 200.0
23
+ -> 200.0
24
+ 3 calls, 3 returns, 0 raises, 2 classes
25
+ ```
26
+
27
+ `rate_for` returned nil. No `puts`, no log file, no restart.
28
+
29
+ A whole Rails request comes out about this long, because everything the
30
+ framework runs underneath is filtered away:
31
+
32
+ ```
33
+ RealEstateAgenciesController#index (app/controllers/real_estate_agencies_controller.rb:14) {}
34
+ IpLocation.guess_state_uf_and_city_name (app/services/ip_location.rb:15) {"ip":"127.0.0.1"}
35
+ -> [null,null]
36
+ -> null
37
+ render app/views/real_estate_agencies/index.html.erb {}
38
+ -> "<!-- BEGIN app/views/real_estate_agencies/index.html.erb\n-->… (2507 chars)"
39
+ 4 calls, 4 returns, 0 raises, 3 classes
40
+ ```
41
+
42
+ That is 4 kept traces out of 13869 the tracer saw.
43
+
44
+ ## Install
45
+
46
+ ```ruby
47
+ # Gemfile
48
+ gem "rails_tracepoint_stack"
49
+ ```
50
+
51
+ ## Debugging with an AI agent
52
+
53
+ Agents reach for `puts` and log lines because they do not know this exists.
54
+ Install the packaged skill into your app and yours will use the gem instead:
55
+
56
+ ```bash
57
+ bin/rails generate rails_tracepoint_stack:install
58
+ ```
59
+
60
+ That writes `.claude/skills/debug-with-tracepoint/SKILL.md`, which tells the
61
+ agent when tracing beats reading code, how to keep the output inside its
62
+ context window, and how to read the tree. Pass `--force` to overwrite an
63
+ existing copy.
64
+
65
+ ## Capturing
66
+
67
+ `capture` traces the block, returns a session and re-raises anything the block
68
+ raised. It watches only the calling thread, so it stays clean under Puma.
69
+
70
+ ```ruby
71
+ session = RailsTracepointStack.capture { Order.find(42).recalculate! }
72
+
73
+ session.to_tree # the indented call tree above
74
+ session.as_json # the same data, structured, one entry per trace
75
+ session.summary # {calls:, returns:, raises:, classes:, truncated:}
76
+ session.result # what the block returned
77
+ session.error # the exception that escaped, if any
78
+ session.traces # the raw TraceRecord list
79
+ ```
80
+
81
+ To keep the traces when the block blows up, take the session from the block
82
+ argument:
83
+
84
+ ```ruby
85
+ session = nil
86
+ begin
87
+ RailsTracepointStack.capture { |s| session = s; thing_that_blows_up }
88
+ rescue => error
89
+ puts session.to_tree
90
+ end
91
+ ```
92
+
93
+ ### Keeping the output small
94
+
95
+ A real request produces tens of thousands of traces, so captures are bounded.
96
+
97
+ | Option | Default | What it does |
98
+ |---|---|---|
99
+ | `max_depth` | none | Drops traces nested deeper than this |
100
+ | `max_traces` | 5000 | Stops collecting; `session.truncated?` becomes true |
101
+ | `max_string_length` | 200 | Shortens long strings |
102
+ | `max_collection_size` | 20 | Shortens long arrays and hashes |
103
+ | `max_value_length` | 1000 | Replaces any single value larger than this with its size |
104
+ | `capture_params` | `true` | Set false to show only the flow |
105
+ | `capture_return` | `true` | Set false to show only the calls |
106
+ | `threads` | `:current` | `:all` also records background threads |
107
+ | `blocks` | `false` | `true` also traces blocks — see below |
108
+
109
+ ```ruby
110
+ RailsTracepointStack.capture(max_depth: 3, capture_return: false) { ... }
111
+ ```
112
+
113
+ ### Blocks and scopes
114
+
115
+ A Rails scope is a lambda, so the logic inside it is not a method call and does
116
+ not appear by default. `blocks: true` traces block entry and exit as well:
117
+
118
+ ```ruby
119
+ RailsTracepointStack.capture(blocks: true) { News.latest(User.current) }
120
+ ```
121
+
122
+ ```
123
+ News.latest (app/models/news.rb:88) {"user":"Anonymous"}
124
+ block { } (app/models/news.rb:46) {"user":"Anonymous"}
125
+ Project.allowed_to_condition (app/models/project.rb:189) {"permission":"view_news"}
126
+ ↻ 40× AccessControl.permission { } (lib/redmine/access_control.rb:37) {"p":"#<Permission …>"}
127
+ -> false
128
+ ```
129
+
130
+ It is off by default because a block written inside a loop runs once per
131
+ element: in Redmine, one call produced 48 block events and 43 of them came from
132
+ a single `detect` predicate. Runs of the same block at the same depth collapse
133
+ into one line with a `↻ N×` count, which keeps only the first run's arguments —
134
+ if you need every iteration, leave blocks off and read the loop instead.
135
+
136
+ Blocks are named after the method they were written in. A block with no
137
+ enclosing method — a scope, a lambda held in a constant — has neither a class
138
+ nor a method name to show, so it renders as `block { }` plus its location.
139
+ ```
140
+
141
+ ## Tracing the whole process
142
+
143
+ For code you cannot wrap in a block — boot, a rake task, a request handled by a
144
+ running server — enable the tracer globally instead. Output goes to
145
+ `log/rails_tracepoint_stack.log`.
146
+
147
+ ```bash
148
+ RAILS_TRACEPOINT_STACK_ENABLED=true bin/rails server
149
+ ```
150
+
151
+ ```
152
+ called: Bar#perform in /path/to/app/services/bar.rb:12 with params: {}
153
+ ```
154
+
155
+ `RailsTracepointStack.enable_trace { ... }` does the same for one block, also
156
+ writing to the log rather than returning a session.
157
+
158
+ ## Configuration
159
+
160
+ ```ruby
161
+ # config/initializers/rails_tracepoint_stack.rb
162
+
163
+ RailsTracepointStack.configure do |config|
164
+ config.file_path_to_filter_patterns << %r{app/services/}
165
+ config.ignore_patterns << %r{app/models/concerns/}
166
+ config.log_format = :json
167
+ config.log_external_sources = false
168
+ config.logger = Rails.logger
169
+ end
170
+ ```
171
+
172
+ | Configuration | Description |
173
+ |---|---|
174
+ | `file_path_to_filter_patterns` | Trace **only** files whose path matches one of these patterns |
175
+ | `ignore_patterns` | Skip traces whose file path matches one of these patterns |
176
+ | `log_format` | `:text` (default) or `:json`. Applies to the log, not to `capture` |
177
+ | `log_external_sources` | Include gems, bundler and stdlib. Default `false` |
178
+ | `logger` | Your own logger. Defaults to `log/rails_tracepoint_stack.log` |
179
+
180
+ A method missing from a trace is usually one the filters dropped as external —
181
+ add its path to `file_path_to_filter_patterns` to force it in.
182
+
183
+ ## Worth knowing
184
+
185
+ - TracePoint makes the traced code noticeably slower — around 8x on a real
186
+ Rails request. Fine for an investigation, not for something left running in
187
+ production.
188
+ - Ruby >= 3.0.
189
+ - A `-> null` line directly under a `!!` line is the frame unwinding, not a
190
+ method that returned nil.
191
+ - An empty tree says `no app code ran` along with how much was filtered. That
192
+ usually means the block ran entirely inside gems — `Order.where(...).map(&:name)`
193
+ calls no method you wrote, since `name` is generated by ActiveRecord.
194
+
195
+ ## Links
196
+
197
+ - [Website](https://carlosdanielpohlod.github.io/rails_tracepoint_stack/) — the
198
+ same story with real captured output, including a whole Rails request
199
+ - [RubyGems](https://rubygems.org/gems/rails_tracepoint_stack)
200
+ - [Changelog](changelog.md)
201
+
202
+ ## License
203
+
204
+ MIT
data/changelog.md ADDED
@@ -0,0 +1,156 @@
1
+ # Changelog
2
+
3
+ ## 0.5.0
4
+
5
+ **Features**
6
+
7
+ - `capture(blocks: true)` traces block entry and exit, which is what makes a
8
+ Rails scope, a lambda held in a constant, or any yielded block visible.
9
+ Off by default: a block inside a loop runs once per element. Runs of the
10
+ same block at the same depth collapse into one record with a `↻ N×` count
11
+ - `max_value_length` (default 1000) caps any single value, whatever its shape
12
+
13
+ **BugFix**
14
+
15
+ - `to_tree` raised `Encoding::CompatibilityError` whenever a traced value held
16
+ bytes that are not text — a file read in binary mode, a digest, a response
17
+ body. Strings are now converted to UTF-8 with invalid bytes replaced
18
+ - A class method on an ActiveRecord model rendered as its singleton class,
19
+ which prints the model's entire schema for a single call. The class name now
20
+ comes from the module's own name
21
+ - Per-string and per-collection limits still let one value through at thousands
22
+ of characters, since twenty items truncated to two hundred each is four
23
+ thousand
24
+ - The block handed to `capture` was traced as part of the result and consumed a
25
+ level of depth, indenting everything under it
26
+
27
+ ## 0.4.0
28
+
29
+ **Features**
30
+
31
+ - `RailsTracepointStack.capture` traces a block and returns the traces in
32
+ memory, with no env var, log file or restart involved
33
+ - Traces now carry return values and raised exceptions, not just calls and params
34
+ - Each trace knows its call depth, so a session renders as a call tree
35
+ - `session.to_tree`, `session.as_json` and `session.summary` render a capture
36
+ - Captures are bounded by `max_depth`, `max_traces`, `max_string_length` and
37
+ `max_collection_size`, and report `truncated?` when a limit cut them short
38
+ - Captures watch only the calling thread by default; `threads: :all` opts out
39
+ - `rails g rails_tracepoint_stack:install` writes an agent skill into the app,
40
+ so AI coding agents know when and how to trace it
41
+
42
+ **BugFix**
43
+
44
+ - Params reported every local the method body declared, not just the arguments
45
+ it received, so unassigned locals showed up as arguments passed as nil.
46
+ `TracePoint#parameters` now decides what is read from the binding
47
+ - `GemPath` referenced `Bundler` without requiring it, raising `NameError` on
48
+ the first trace outside a bundle
49
+ - A raise coming out of a C method landed one level too deep in the tree
50
+
51
+ **Changes**
52
+
53
+ - The tracer writes to a sink; the default one keeps the previous logging
54
+ behaviour, so global tracing is unchanged
55
+ - Class methods render as `Foo.bar` rather than `#<Class:Foo>#bar`
56
+ - Compiled templates render as `render app/views/…` with their locals, instead
57
+ of the generated method name and the rendering internals
58
+ - An empty capture reports how many traces the filters dropped, so "nothing
59
+ ran" is distinguishable from "nothing of yours ran"
60
+ - Serialized string values are frozen copies, so a snapshot no longer follows
61
+ later mutations of the traced object
62
+
63
+ ## 0.3.5
64
+
65
+ **BugFix**
66
+
67
+ - Sanitize traced params before formatting so JSON logging does not overflow on recursive or problematic objects [Issue #37](https://github.com/carlosdanielpohlod/rails_tracepoint_stack/issues/37)
68
+ - Stabilize text log param rendering across Ruby versions
69
+
70
+ ## 0.3.4
71
+
72
+ **Changes**
73
+
74
+ - Fix flaky tests by @danielmbrasil [PR #27](https://github.com/carlosdanielpohlod/rails_tracepoint_stack/pull/27)
75
+
76
+ - Huge refactor of filters class, separating in modules by filter type [PR #18](https://github.com/carlosdanielpohlod/rails_tracepoint_stack/pull/18)
77
+
78
+ - Some other code refactors
79
+
80
+ ## 0.3.3
81
+
82
+ **Changes**
83
+
84
+ - Add autoload of all lib files on test_helper
85
+ - Fix some tests
86
+
87
+ **BugFix**
88
+
89
+ - Fixed the Configuration module not loading the default value for the configuration attributes
90
+
91
+ ## 0.3.1
92
+
93
+ **Changes:**
94
+
95
+ - Add the ability to include the external sources to the log using `config.log_external_sources = true`
96
+
97
+ ## 0.3.0
98
+
99
+ **Changes:**
100
+
101
+ - Refactor classes, formatting a trace using a value object class `RailsTracepointStack::Trace`
102
+ - include configuration `log_format` option, allowing choose an output as `text` or `json`
103
+ - Include configuration `file_path_to_filter_patterns`, allowing filter traces only when the origin file path matches a pattern
104
+ - Improve test coverage
105
+
106
+ ## 0.2.1
107
+
108
+ **Changes:**
109
+
110
+ - Update the ENV enable to be more semantic
111
+ - Add the VERSION constant module
112
+ - Sorted the files inside of gemspec
113
+ - Fix the depencies on the gemspec
114
+ - Add the "log_format" configuration support for text and json formats
115
+
116
+ ## 0.2.0
117
+
118
+ **Changes:**
119
+
120
+ - Refactor by separating Logger and Filter into their own classes.
121
+
122
+ - Introduce `RailsTracepointStack.configure`, which allows ignoring traces with a custom pattern and customizing the logs output. Example:
123
+
124
+ ```ruby
125
+ RailsTracepointStack.configure do |config|
126
+ config.ignore_patterns << /services\/foo.rb/
127
+ config.logger = YourLogger
128
+ end
129
+ ```
130
+
131
+ The default log destination is a file located on `log/rails_tracepoint_stack.log`
132
+
133
+ - Add The possibility of enable the tracer locally, by calling:
134
+
135
+ ```ruby
136
+ class Foo
137
+ def bar
138
+ RailsTracepointStack.enable_trace do
139
+ p "your code"
140
+ end
141
+ end
142
+ end
143
+ ```
144
+
145
+ - Add Rspec and Rake development dependencies, and add partial test coverage.
146
+
147
+ ## 0.1.4
148
+
149
+ **Changes:**
150
+
151
+ - Ignore logs containing `gems/bundler`.
152
+ - Require ruby >= 3.0.
153
+
154
+ **Breaking Changes:**
155
+
156
+ - To enable logs catch, it is necessary to set `RAILS_TRACEPOINT_STACK` as `true`.
@@ -0,0 +1,29 @@
1
+ require "rails/generators/base"
2
+ require "rails_tracepoint_stack/skill_installer"
3
+
4
+ module RailsTracepointStack
5
+ module Generators
6
+ # Thin wrapper over SkillInstaller so the behaviour stays testable without
7
+ # booting Rails.
8
+ class InstallGenerator < Rails::Generators::Base
9
+ desc "Installs the debug-with-tracepoint skill so agents working in " \
10
+ "this app know how to trace it"
11
+
12
+ def install_agent_skill
13
+ installer = RailsTracepointStack::SkillInstaller.new(
14
+ destination: destination_root,
15
+ force: options[:force]
16
+ )
17
+ written = installer.install
18
+
19
+ if written
20
+ say_status :create, relative_to_original_destination_root(written), :green
21
+ else
22
+ say_status :skip,
23
+ "#{relative_to_original_destination_root(installer.target_path)} already exists (--force to replace)",
24
+ :yellow
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,54 @@
1
+ module RailsTracepointStack
2
+ # Turns raw stack positions into app-level nesting.
3
+ #
4
+ # Counting :call and :return events would not work here, for two reasons.
5
+ # Most frames between two traced methods belong to gems and get filtered out,
6
+ # so an event counter would indent app code by the depth of the framework
7
+ # underneath it. And the tracer does not always watch :return at all - the
8
+ # global tracer only subscribes to :call - so nothing would ever pop.
9
+ #
10
+ # Reading the real stack position of each kept trace avoids both. A frame
11
+ # recorded at an equal or deeper position has necessarily finished, whether
12
+ # it returned, raised, or was abandoned, so it gets dropped on the next
13
+ # event rather than waiting for one that may never arrive.
14
+ class DepthTracker
15
+ def initialize
16
+ @stack = []
17
+ end
18
+
19
+ def enter(raw_position)
20
+ drop_finished_frames(raw_position, inclusive: true)
21
+ depth = @stack.size
22
+ @stack.push(raw_position)
23
+ depth
24
+ end
25
+
26
+ def leave(raw_position)
27
+ drop_finished_frames(raw_position, inclusive: false)
28
+ return @stack.size unless @stack.last == raw_position
29
+
30
+ @stack.pop
31
+ @stack.size
32
+ end
33
+
34
+ # A raise belongs to the innermost frame being tracked. It may report a
35
+ # position deeper than that frame when the exception comes out of a C
36
+ # method the app called, such as an arithmetic coercion, and there is no
37
+ # tracked frame down there to attribute it to.
38
+ def raised(raw_position)
39
+ drop_finished_frames(raw_position, inclusive: false)
40
+
41
+ [@stack.size - 1, 0].max
42
+ end
43
+
44
+ private
45
+
46
+ def drop_finished_frames(raw_position, inclusive:)
47
+ if inclusive
48
+ @stack.pop while @stack.last && @stack.last >= raw_position
49
+ else
50
+ @stack.pop while @stack.last && @stack.last > raw_position
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,8 +1,27 @@
1
1
  module RailsTracepointStack
2
2
  module Filter
3
3
  class GemPath
4
+ # Bundler gives the tightest answer, but it is not always there: a plain
5
+ # `ruby -e` or an irb session against an installed gem has no bundle, and
6
+ # asking Bundler then raises instead of filtering. Falling back to every
7
+ # installed gem is wider than needed and still filters out the code the
8
+ # developer did not write.
4
9
  def self.full_gem_path
5
- @full_gem_path ||= Bundler.load.specs.map(&:full_gem_path)
10
+ @full_gem_path ||= bundled_gem_paths || gem_install_roots
11
+ end
12
+
13
+ def self.bundled_gem_paths
14
+ return nil unless defined?(::Bundler)
15
+
16
+ ::Bundler.load.specs.map(&:full_gem_path)
17
+ rescue
18
+ nil
19
+ end
20
+
21
+ # Without a bundle there is no resolved list to ask for, but everything
22
+ # installed lives under these roots, which is all the filter needs.
23
+ def self.gem_install_roots
24
+ Gem.path.map { |dir| File.join(dir, "gems") }
6
25
  end
7
26
  end
8
27
  end
@@ -0,0 +1,45 @@
1
+ module RailsTracepointStack
2
+ # How much a capture is allowed to keep. The defaults aim at an output a
3
+ # person or an agent can read in one go rather than at completeness: a
4
+ # single Rails request can easily produce tens of thousands of traces.
5
+ class Limits
6
+ DEFAULT_MAX_TRACES = 5_000
7
+ DEFAULT_MAX_STRING_LENGTH = 200
8
+ DEFAULT_MAX_COLLECTION_SIZE = 20
9
+ DEFAULT_MAX_VALUE_LENGTH = 1_000
10
+
11
+ attr_reader :max_depth,
12
+ :max_traces,
13
+ :max_string_length,
14
+ :max_collection_size,
15
+ :max_value_length,
16
+ :capture_params,
17
+ :capture_return
18
+
19
+ def initialize(
20
+ max_depth: nil,
21
+ max_traces: DEFAULT_MAX_TRACES,
22
+ max_string_length: DEFAULT_MAX_STRING_LENGTH,
23
+ max_collection_size: DEFAULT_MAX_COLLECTION_SIZE,
24
+ max_value_length: DEFAULT_MAX_VALUE_LENGTH,
25
+ capture_params: true,
26
+ capture_return: true
27
+ )
28
+ @max_depth = max_depth
29
+ @max_traces = max_traces
30
+ @max_string_length = max_string_length
31
+ @max_collection_size = max_collection_size
32
+ @max_value_length = max_value_length
33
+ @capture_params = capture_params
34
+ @capture_return = capture_return
35
+ end
36
+
37
+ def too_deep?(depth)
38
+ !max_depth.nil? && !depth.nil? && depth > max_depth
39
+ end
40
+
41
+ def room_for?(count)
42
+ max_traces.nil? || count < max_traces
43
+ end
44
+ end
45
+ end
@@ -25,18 +25,44 @@ module RailsTracepointStack
25
25
  )
26
26
  end
27
27
 
28
+ REPLACEMENT = "�".freeze
29
+
28
30
  def self.stringify(value)
29
31
  return nil if value.nil?
30
32
 
31
- value.to_s
33
+ utf8(value.to_s)
32
34
  rescue SystemStackError, StandardError => error
33
35
  inspect_fallback(value, error)
34
36
  end
35
37
 
38
+ # Traced code holds bytes that are not text: a file read in binary mode, a
39
+ # digest, a response body. Left as they are, those strings poison every
40
+ # consumer downstream - JSON refuses to encode them, and joining them with
41
+ # the rest of the output raises Encoding::CompatibilityError. Normalizing
42
+ # here keeps the damage to the one value that caused it.
43
+ def self.utf8(value)
44
+ return value unless value.is_a?(String)
45
+
46
+ converted =
47
+ if value.encoding == Encoding::UTF_8
48
+ value
49
+ else
50
+ value.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: REPLACEMENT)
51
+ end
52
+
53
+ converted.valid_encoding? ? converted : converted.scrub(REPLACEMENT)
54
+ rescue SystemStackError, StandardError
55
+ value.scrub(REPLACEMENT)
56
+ end
57
+
36
58
  def self.safe_value(value, ancestry = {})
37
59
  case value
38
- when nil, true, false, Numeric, String
60
+ when nil, true, false, Numeric
39
61
  value
62
+ when String
63
+ # Callers may keep the result around after the traced code moved on,
64
+ # so a live reference to a mutable string would drift.
65
+ utf8(value).dup.freeze
40
66
  when Symbol
41
67
  value.to_s
42
68
  when Array
@@ -121,7 +147,7 @@ module RailsTracepointStack
121
147
  end
122
148
 
123
149
  def self.safe_object_string(value)
124
- value.inspect
150
+ utf8(value.inspect)
125
151
  rescue SystemStackError, StandardError => error
126
152
  inspect_fallback(value, error)
127
153
  end
@@ -142,7 +168,7 @@ module RailsTracepointStack
142
168
  when true, false, Numeric
143
169
  value.to_s
144
170
  when String
145
- value.inspect
171
+ utf8(value).inspect
146
172
  when Symbol
147
173
  ":#{value}"
148
174
  when Array
@@ -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