ruby_method_tracer 0.1.1 → 0.3.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: 673fbc66c79fb1b2466d43fad6be1f738a8978630073283cda616b179beb8c8d
4
- data.tar.gz: 92d557e3061f52f950747fd6a314c22facbd89ff3be790c223f71316ca50a69f
3
+ metadata.gz: e5fe5404bd1497820b14ca1e4bc041f778cfe9bc02402a4456329c88d0096206
4
+ data.tar.gz: cb08942010ab25a16a604d0edb973bd9f889e3c34ba2ae24ead8a0dcceed4ca8
5
5
  SHA512:
6
- metadata.gz: cba38cde9f2ddf19a523abb9b1409832620ad9568b43dfb131bf0243119052134ec7b21e097fa8568d04801d32bf3918747844d87bbff54a4e06a39be3fecc74
7
- data.tar.gz: c2f90be756be60ce89ba83348a38feb797922923bbe5434c2f421a492c890f3d51b8eff32b53506958fb554baafed58d7e15758803bbead740ef53b759390c0f
6
+ metadata.gz: f68e49cde1042577a5ed2f0054c263391070f29574d81bc58f6ec5db6f688f31411572372cce95eef010fbd6fc7cd981f38905cc01615eea914a680393af850a
7
+ data.tar.gz: 54b2ccc817924cd256dfdcfb66011acfa8641b76f66d2cb0ef59dab48e32f4d2bfa9d56d56d403f5bf60b53c627d784731984e0f61fc8ad01714576eba531620
data/CHANGELOG.md CHANGED
@@ -1,6 +1,45 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2025-11-19
4
+
5
+ ### Added
6
+ - **NEW: Hierarchical Call Tree Visualization** - `EnhancedTracer` class for tracking nested method calls
7
+ - `CallTree` class for managing call hierarchy with parent-child relationships
8
+ - `TreeFormatter` for beautiful tree visualization with proper indentation and tree characters
9
+ - Statistics calculation: slowest methods, most called methods, max call depth
10
+ - `print_tree()` method for outputting formatted call trees
11
+ - `format_tree()` method for programmatic access to tree visualization
12
+ - `fetch_enhanced_results()` for combined flat and hierarchical data
13
+ - Thread-safe call stack management
14
+ - Error tracking in call tree with full error messages
15
+ - Color-coded tree output for better readability
16
+ - 24 new comprehensive tests covering call tree functionality
17
+
18
+ ### Changed
19
+ - Main module now auto-loads `EnhancedTracer` and formatting classes
20
+ - README updated with call tree examples and usage guide
21
+ - Documentation expanded with decision guide for choosing between SimpleTracer and EnhancedTracer
22
+
23
+ ## [0.2.0] - 2025-11-19
24
+
25
+ ### Added
26
+ - Memory management with `max_calls` option (default: 1000) to prevent unbounded memory growth
27
+ - `clear_results` public method to manually free stored trace data
28
+ - Configurable logger via `logger` option for custom log destinations and formatting
29
+ - Comprehensive test coverage for memory management and logger configuration
30
+
31
+ ### Fixed
32
+ - Removed unnecessary `logger` gem dependency (now uses Ruby standard library)
33
+ - Fixed gemspec URL casing inconsistencies for GitHub links
34
+
35
+ ### Changed
36
+ - Default behavior now automatically limits stored calls to 1000 entries (oldest removed when exceeded)
37
+ - Documentation updated with new configuration options and advanced usage examples
38
+
39
+ ## [0.1.1] - 2025-09-16
40
+
41
+ - Bug fixes and improvements
42
+
3
43
  ## [0.1.0] - 2025-09-03
4
- ## [0.1.0] - 2025-09-16
5
44
 
6
45
  - Initial release
data/README.md CHANGED
@@ -7,40 +7,44 @@ RubyMethodTracer is a lightweight Ruby mixin for targeted method tracing. It wra
7
7
  ## Highlights
8
8
  - Wrap only the methods you care about; public, protected, and private methods are supported.
9
9
  - Records duration, success/error state, and timestamps with thread-safe storage.
10
+ - **NEW: Hierarchical call tree visualization** to understand nested method calls and dependencies.
10
11
  - Configurable threshold to ignore fast calls and optional log streaming via `Logger`.
11
12
  - Zero dependencies beyond the Ruby standard library, keeping overhead minimal.
12
13
 
13
14
  ## Installation
14
15
 
15
- This project has not yet been published to RubyGems.
16
-
17
- Add it to your Gemfile directly from GitHub:
16
+ Add this line to your application's Gemfile:
18
17
 
19
18
  ```ruby
20
- # Gemfile
21
- gem "ruby_method_tracer", github: "Seunadex/ruby_method_tracer"
19
+ gem "ruby_method_tracer"
22
20
  ```
23
21
 
24
- Then install:
22
+ And then execute:
25
23
 
26
24
  ```bash
27
25
  bundle install
28
26
  ```
29
27
 
28
+ Or install it yourself as:
29
+
30
+ ```bash
31
+ gem install ruby_method_tracer
32
+ ```
33
+
30
34
  For local development or experimentation:
31
35
 
32
36
  ```bash
33
37
  git clone https://github.com/Seunadex/ruby_method_tracer.git
34
- cd method_tracer
38
+ cd ruby_method_tracer
35
39
  bundle exec rake install
36
40
  ```
37
41
 
38
42
  ## Usage
39
43
 
44
+ ### Example 1
40
45
  Include `RubyMethodTracer` in any class whose instance methods you want to observe. Register the target methods with optional settings.
41
46
 
42
47
  ```ruby
43
- require "ruby_method_tracer"
44
48
 
45
49
  class Worker
46
50
  include RubyMethodTracer
@@ -83,12 +87,165 @@ pp tracer.fetch_results
83
87
  # { method_name: "Worker#perform", execution_time: 0.0063, status: :success, ... }
84
88
  # ]
85
89
  # }
90
+
91
+ # Clear results when needed to free memory
92
+ tracer.clear_results
86
93
  ```
87
94
 
88
- ### Options
95
+ ### Example 2
96
+
97
+ ```ruby
98
+ class OrderProcessor
99
+ include RubyMethodTracer
100
+
101
+ def process_order(order)
102
+ # ... perform work ...
103
+ end
104
+
105
+ trace_methods :process_order, auto_output: true
106
+ end
107
+ ```
108
+
109
+ ### Example 3: Advanced Configuration
110
+
111
+ ```ruby
112
+ # Use a custom logger to write traces to a file
113
+ custom_logger = Logger.new('trace.log')
114
+ custom_logger.level = Logger::INFO
115
+
116
+ tracer = RubyMethodTracer::SimpleTracer.new(
117
+ MyService,
118
+ threshold: 0.01, # Only record calls over 10ms
119
+ auto_output: true, # Log each call
120
+ max_calls: 500, # Keep only last 500 calls in memory
121
+ logger: custom_logger # Use custom logger
122
+ )
123
+ tracer.trace_method(:expensive_operation)
124
+
125
+ # Later, clear results to free memory
126
+ tracer.clear_results
127
+ ```
128
+
129
+ ### Example 4: Call Tree Visualization (NEW!)
130
+
131
+ Visualize hierarchical method call relationships with the `EnhancedTracer`:
132
+
133
+ ```ruby
134
+ class OrderProcessor
135
+ def process_order(order)
136
+ validate_order(order)
137
+ charge_payment(order)
138
+ send_confirmation(order)
139
+ end
140
+
141
+ def validate_order(order)
142
+ check_inventory(order.items)
143
+ end
144
+
145
+ def charge_payment(order)
146
+ # Payment processing...
147
+ end
148
+
149
+ def send_confirmation(order)
150
+ # Email sending...
151
+ end
152
+
153
+ private
154
+
155
+ def check_inventory(items)
156
+ # Inventory check...
157
+ end
158
+ end
159
+
160
+ # Use EnhancedTracer for call tree tracking
161
+ tracer = RubyMethodTracer::EnhancedTracer.new(OrderProcessor, threshold: 0.0)
162
+ tracer.trace_method(:process_order)
163
+ tracer.trace_method(:validate_order)
164
+ tracer.trace_method(:charge_payment)
165
+ tracer.trace_method(:send_confirmation)
166
+ tracer.trace_method(:check_inventory)
167
+
168
+ processor = OrderProcessor.new
169
+ processor.process_order(order)
170
+
171
+ # Print beautiful call tree visualization
172
+ tracer.print_tree
173
+ ```
174
+
175
+ This outputs a hierarchical tree showing nested calls:
176
+
177
+ ```
178
+ METHOD CALL TREE
179
+ ============================================================
180
+ └── OrderProcessor#process_order (125.3ms)
181
+ ├── OrderProcessor#validate_order (15.2ms)
182
+ │ └── OrderProcessor#check_inventory (12.1ms)
183
+ ├── OrderProcessor#charge_payment (85.4ms)
184
+ └── OrderProcessor#send_confirmation (24.7ms)
185
+ ============================================================
186
+
187
+ STATISTICS
188
+ ------------------------------------------------------------
189
+ Total Calls: 5
190
+ Total Time: 250.6ms
191
+ Unique Methods: 5
192
+ Max Depth: 2
193
+
194
+ Slowest Methods (by average time):
195
+ 1. OrderProcessor#process_order - 125.3ms
196
+ 2. OrderProcessor#charge_payment - 85.4ms
197
+ 3. OrderProcessor#send_confirmation - 24.7ms
198
+ 4. OrderProcessor#validate_order - 15.2ms
199
+ 5. OrderProcessor#check_inventory - 12.1ms
200
+
201
+ Most Called Methods:
202
+ 1. OrderProcessor#process_order - 1 calls
203
+ 2. OrderProcessor#validate_order - 1 calls
204
+ ...
205
+ ```
206
+
207
+ **Call Tree Features:**
208
+ - Shows parent-child relationships between methods
209
+ - Visual tree structure with proper indentation
210
+ - Execution times for each method call
211
+ - Statistics summary (slowest methods, most called, max depth)
212
+ - Error indicators with full error messages
213
+ - Color-coded output for better readability
214
+
215
+
216
+ ### Options (SimpleTracer)
89
217
 
90
218
  - `threshold` (Float, default `0.001`): minimum duration (in seconds) to record.
91
219
  - `auto_output` (Boolean, default `false`): emit a log line using `Logger` for each recorded call.
220
+ - `max_calls` (Integer, default `1000`): maximum number of calls to store in memory. When exceeded, the oldest calls are automatically removed to prevent memory leaks.
221
+ - `logger` (Logger, default `Logger.new($stdout)`): custom logger instance for output. Useful for directing logs to files or custom log handlers.
222
+
223
+ ### Options (EnhancedTracer)
224
+
225
+ EnhancedTracer supports all SimpleTracer options plus:
226
+
227
+ - `track_hierarchy` (Boolean, default `true`): enable call tree tracking. Set to `false` to use EnhancedTracer like SimpleTracer.
228
+
229
+ ### API Methods (EnhancedTracer)
230
+
231
+ - `print_tree(options = {})` - Print formatted call tree to stdout
232
+ - Options: `colorize: true/false`, `show_errors: true/false`
233
+ - `format_tree(options = {})` - Get formatted call tree as string
234
+ - `fetch_enhanced_results` - Get hash with `:flat_calls`, `:call_hierarchy`, and `:statistics`
235
+ - `clear_results` - Clear both flat results and call tree
236
+
237
+ ## Choosing Between SimpleTracer and EnhancedTracer
238
+
239
+ **Use SimpleTracer when:**
240
+ - You only need flat timing data
241
+ - You want minimal overhead
242
+ - You're tracing independent methods
243
+
244
+ **Use EnhancedTracer when:**
245
+ - You need to understand call hierarchies
246
+ - You want to visualize nested method calls
247
+ - You're debugging complex call flows
248
+ - You need statistics on method relationships
92
249
 
93
250
  ## Development
94
251
 
@@ -12,6 +12,8 @@ module RubyMethodTracer
12
12
  # Options:
13
13
  # - :threshold (Float): Minimum duration in seconds to record; defaults to 0.001 (1ms).
14
14
  # - :auto_output (Boolean): When true, prints each call summary; defaults to false.
15
+ # - :max_calls (Integer): Maximum number of calls to store; defaults to 1000. When exceeded, oldest calls are removed.
16
+ # - :logger (Logger): Custom logger instance; defaults to Logger.new($stdout).
15
17
  #
16
18
  # Usage:
17
19
  # tracer = RubyMethodTracer::SimpleTracer.new(MyClass, threshold: 0.005)
@@ -24,7 +26,7 @@ module RubyMethodTracer
24
26
  @calls = []
25
27
  @lock = Mutex.new # Mutex to make writes to @calls thread safe.
26
28
  @wrapped_methods = Set.new
27
- @logger = Logger.new($stdout)
29
+ @logger = @options[:logger] || Logger.new($stdout)
28
30
  end
29
31
 
30
32
  def trace_method(name)
@@ -56,7 +58,11 @@ module RubyMethodTracer
56
58
  timestamp: Time.now
57
59
  }
58
60
 
59
- @lock.synchronize { @calls << call_details } # Thread safe append to shared results arrray.
61
+ @lock.synchronize do
62
+ @calls << call_details
63
+ # Enforce max_calls limit by removing oldest entries
64
+ @calls.shift if @calls.size > @options[:max_calls]
65
+ end
60
66
 
61
67
  output_call(call_details) if @options[:auto_output]
62
68
  end
@@ -72,12 +78,18 @@ module RubyMethodTracer
72
78
  }
73
79
  end
74
80
 
81
+ def clear_results
82
+ @lock.synchronize { @calls.clear }
83
+ end
84
+
75
85
  private
76
86
 
77
87
  def default_options
78
88
  {
79
89
  threshold: 0.001,
80
- auto_output: false
90
+ auto_output: false,
91
+ max_calls: 1000,
92
+ logger: nil
81
93
  }
82
94
  end
83
95
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyMethodTracer
4
- VERSION = "0.1.1"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -2,6 +2,10 @@
2
2
 
3
3
  require_relative "ruby_method_tracer/version"
4
4
  require_relative "ruby_method_tracer/simple_tracer"
5
+ require_relative "ruby_method_tracer/call_tree"
6
+ require_relative "ruby_method_tracer/enhanced_tracer"
7
+ require_relative "ruby_method_tracer/formatters/base_formatter"
8
+ require_relative "ruby_method_tracer/formatters/tree_formatter"
5
9
 
6
10
  # Public: Mixin that adds lightweight method tracing to classes.
7
11
  #
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_method_tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seun Adekunle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-10-12 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: logger
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 1.7.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 1.7.0
11
+ date: 2025-11-19 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: A developer-friendly gem for tracing method calls, execution times, with
28
14
  minimal overhead.
29
15
  email:
@@ -43,14 +29,14 @@ files:
43
29
  - lib/ruby_method_tracer/simple_tracer.rb
44
30
  - lib/ruby_method_tracer/version.rb
45
31
  - sig/ruby_method_tracer.rbs
46
- homepage: https://github.com/seunadex/ruby_method_tracer
32
+ homepage: https://github.com/Seunadex/ruby_method_tracer
47
33
  licenses:
48
34
  - MIT
49
35
  metadata:
50
36
  allowed_push_host: https://rubygems.org
51
37
  homepage_uri: https://github.com/Seunadex/ruby_method_tracer/blob/main/README.md
52
- source_code_uri: https://github.com/seunadex/ruby_method_tracer
53
- changelog_uri: https://github.com/seunadex/ruby_method_tracer/blob/main/CHANGELOG.md
38
+ source_code_uri: https://github.com/Seunadex/ruby_method_tracer
39
+ changelog_uri: https://github.com/Seunadex/ruby_method_tracer/blob/main/CHANGELOG.md
54
40
  rubygems_mfa_required: 'true'
55
41
  post_install_message:
56
42
  rdoc_options: []