rails_tracepoint_stack 0.1.4 → 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: 8fcdfb6d987f930a294785da2decd147c1f9f7c1e95ca5815f5f4aba38b0d81b
4
- data.tar.gz: f02ced7d9f7413d101f16945737c0a3b8dabc3028859c787ccb63c2922d88da4
3
+ metadata.gz: a6e61976b25735790c540866ecebe19e2107f14ca9f2434fc0c176de91953303
4
+ data.tar.gz: 5df99b05328e5158e8763469e2f5f24ffd34fb914c251d88ef0bcb7f3ee96e49
5
5
  SHA512:
6
- metadata.gz: 8adede1acb4f3391aa435070d2b0f1a8447c282bf85f35236ce25459401074e3b5d99beb66b3d6053d86b0d14a03060dcc209b30e0e5a0fca3b5a793432037e0
7
- data.tar.gz: ba27381d62e176251babd86041f7362e44552987359dd66950a82b47b24a88d8026a0aa191b582c04076df05c6eefc650748766425ceaba4b6d1e70ccb746a0d
6
+ metadata.gz: 867a2672fd90e65f0244632cc2ba9f189c865d7d8ae81ea0004e2a8461770c61dabb0b9fc45a32c9e244218292cf6b8ca34beae31a965a7508fd1d90e4327283
7
+ data.tar.gz: 043e6d052f87535dccd6acaeaa224fe94337e2305ffbcdca392c946fbf3cfab4645648cc4b9d80252835018954376af16d8df76fd017e7693ec22d9d36e6f53b
@@ -0,0 +1,12 @@
1
+ module RailsTracepointStack
2
+ class Configuration
3
+ attr_accessor :ignore_patterns, :logger, :log_format, :file_path_to_filter_patterns
4
+
5
+ def initialize
6
+ @ignore_patterns = []
7
+ @file_path_to_filter_patterns = []
8
+ @logger = nil
9
+ @log_format = :text
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module RailsTracepointStack
2
+ module Filter
3
+ class GemPath
4
+ def self.full_gem_path
5
+ @full_gem_path ||= Bundler.load.specs.map(&:full_gem_path)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module RailsTracepointStack
2
+ module Filter
3
+ class RbConfig
4
+ def self.ruby_lib_path
5
+ @ruby_lib_path ||= ::RbConfig::CONFIG['rubylibdir']
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ module RailsTracepointStack
2
+ module LogFormatter
3
+ def self.message(trace)
4
+ case RailsTracepointStack.configuration&.log_format
5
+ when :json
6
+ json trace
7
+ else
8
+ text trace
9
+ end
10
+ end
11
+
12
+ def self.text(trace)
13
+ "called: #{trace.class_name}##{trace.method_name} in #{trace.file_path}:#{trace.line_number} with params: #{trace.params}"
14
+ end
15
+
16
+ def self.json(trace)
17
+ {
18
+ class: trace.class_name,
19
+ method_name: trace.method_name,
20
+ path: trace.file_path,
21
+ line: trace.line_number,
22
+ params: trace.params
23
+ }.to_json
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ module RailsTracepointStack
2
+ class Logger
3
+ def self.log(msg)
4
+ if RailsTracepointStack.configuration&.logger
5
+ RailsTracepointStack.configuration.logger.info(msg)
6
+ else
7
+ # TODO: Add the support to Rails.env for the default filename
8
+ File.open("log/rails_tracepoint_stack.log", 'a') do |f|
9
+ f.puts msg
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,30 @@
1
+ require 'forwardable'
2
+
3
+ module RailsTracepointStack
4
+ class Trace
5
+ extend Forwardable
6
+
7
+ attr_reader :params, :trace_point
8
+
9
+ def_delegator :@trace_point, :defined_class, :class_name
10
+ def_delegator :@trace_point, :method_id, :method_name
11
+ def_delegator :@trace_point, :path, :file_path
12
+ def_delegator :@trace_point, :lineno, :line_number
13
+
14
+ def initialize(trace_point:)
15
+ @trace_point = trace_point
16
+ end
17
+
18
+ def params
19
+ @params ||= fetch_params(trace_point)
20
+ end
21
+
22
+ private
23
+
24
+ def fetch_params(trace_point)
25
+ trace_point.binding.local_variables.map { |var|
26
+ [var, trace_point.binding.local_variable_get(var)]
27
+ }.to_h
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,60 @@
1
+ require 'rails_tracepoint_stack/filter/gem_path'
2
+ require 'rails_tracepoint_stack/filter/rb_config'
3
+
4
+ module RailsTracepointStack
5
+ class TraceFilter
6
+ def self.ignore_trace?(trace:)
7
+ if defined_file_path_to_filter_patterns?
8
+ not_matches_file_path_to_filter_patterns?(trace)
9
+ else
10
+ contains_to_ignore_strings?(trace) || from_gempath_or_lib_path?(trace) || is_a_to_ignore_pattern?(trace)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def self.contains_to_ignore_strings?(trace)
17
+ trace.file_path.start_with?('<internal:') || trace.file_path == '(eval)'
18
+ end
19
+
20
+ def self.from_gempath_or_lib_path?(trace)
21
+ file_path_starts_with_gem_path?(trace) ||
22
+ file_path_starts_with_ruby_lib_path?(trace) ||
23
+ file_path_starts_with_bundler_path?(trace)
24
+ end
25
+
26
+ def self.is_a_to_ignore_pattern?(trace)
27
+ RailsTracepointStack.configuration&.ignore_patterns&.any? { |pattern| trace.file_path.match?(pattern) }
28
+ end
29
+
30
+ def self.file_path_starts_with_gem_path?(trace)
31
+ gem_paths.any? { |path| trace.file_path.start_with?(path) }
32
+ end
33
+
34
+ def self.file_path_starts_with_ruby_lib_path?(trace)
35
+ trace.file_path.start_with?(ruby_lib_path)
36
+ end
37
+
38
+ def self.file_path_starts_with_bundler_path?(trace)
39
+ trace.file_path.include?("gems/bundler")
40
+ end
41
+
42
+ def self.gem_paths
43
+ @gem_paths ||= RailsTracepointStack::Filter::GemPath.full_gem_path
44
+ end
45
+
46
+ def self.ruby_lib_path
47
+ @ruby_lib_path ||= RailsTracepointStack::Filter::RbConfig.ruby_lib_path
48
+ end
49
+
50
+ def self.defined_file_path_to_filter_patterns?
51
+ RailsTracepointStack.configuration&.file_path_to_filter_patterns&.any?
52
+ end
53
+
54
+ def self.not_matches_file_path_to_filter_patterns?(trace)
55
+ !RailsTracepointStack.configuration.file_path_to_filter_patterns.any? do |pattern|
56
+ trace.file_path.match?(pattern)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,22 @@
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
+
7
+ module RailsTracepointStack
8
+ class Tracer
9
+ # TODO: Tracer.new shoud return the tracer. Is weird to call Tracer.new.tracer
10
+ def tracer
11
+ @tracer ||= TracePoint.new(:call) do |tracepoint|
12
+ trace = RailsTracepointStack::Trace.new(trace_point: tracepoint)
13
+
14
+ next if RailsTracepointStack::TraceFilter.ignore_trace?(trace: trace)
15
+
16
+ # TODO: Use proper OO
17
+ message = RailsTracepointStack::LogFormatter.message trace
18
+ RailsTracepointStack::Logger.log message
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module RailsTracepointStack
2
+ VERSION = "0.3.0"
3
+ end
@@ -1,43 +1,33 @@
1
- $rails_tracer_rtps = nil
2
-
3
- class RailsTracepointStack
4
- def initialize
5
- @gem_paths = Bundler.load.specs.map(&:full_gem_path)
6
- @ruby_lib_path = RbConfig::CONFIG['rubylibdir']
7
- end
1
+ require 'json'
2
+ require 'rails_tracepoint_stack/configuration'
3
+ require 'rails_tracepoint_stack/log_formatter'
4
+ require 'rails_tracepoint_stack/tracer'
8
5
 
9
- def tracer
10
- @trace ||= TracePoint.new(:call) do |tp|
11
- next if start_with_to_ignore_prefixes?(tp) || from_gempath_or_lib_path?(tp)
6
+ $rails_tracer_rtps = nil
12
7
 
13
- params = fetch_params(tp)
14
-
15
- puts "called: #{tp.defined_class}##{tp.method_id} in #{tp.path}:#{tp.lineno} with params: #{params}"
16
- end
8
+ module RailsTracepointStack
9
+ class << self
10
+ attr_accessor :configuration, :logger
17
11
  end
18
12
 
19
- private
20
- attr_reader :gem_paths, :ruby_lib_path
21
-
22
- def fetch_params(tp)
23
- tp.binding.local_variables.map { |var|
24
- [var, tp.binding.local_variable_get(var)]
25
- }.to_h
13
+ def self.configure
14
+ self.configuration ||= RailsTracepointStack::Configuration.new
15
+ yield(configuration)
26
16
  end
27
17
 
28
- def start_with_to_ignore_prefixes?(tp)
29
- tp.path.start_with?('<internal:') || tp.path == '(eval)'
30
- end
18
+ def self.enable_trace
19
+ raise ArgumentError, "Block not given to #enable_trace" unless block_given?
31
20
 
32
- def from_gempath_or_lib_path?(tp)
33
- gem_paths.any? { |path| tp.path.start_with?(path) } ||
34
- tp.path.start_with?(ruby_lib_path) ||
35
- tp.path.include?("gems/bundler")
21
+ tracer = RailsTracepointStack::Tracer.new.tracer
22
+ tracer.enable
23
+ yield
24
+ ensure
25
+ tracer.disable
36
26
  end
37
27
  end
38
28
 
39
- if ENV.fetch('RAILS_TRACEPOINT_STACK', 'false') == 'true'
40
- $rails_tracer_rtps = RailsTracepointStack.new.tracer
29
+ if ENV.fetch('RAILS_TRACEPOINT_STACK_ENABLED', 'false') == 'true'
30
+ $rails_tracer_rtps = RailsTracepointStack::Tracer.new.tracer
41
31
  $rails_tracer_rtps.enable
42
32
 
43
33
  at_exit do
metadata CHANGED
@@ -1,15 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_tracepoint_stack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Daniel Pohlod
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-16 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '13.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 13.0.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '13.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 13.0.0
13
53
  description: A formatted output of all methods called in your rails application of
14
54
  code created by the developer, with the complete path to the class/module, including
15
55
  passed params.
@@ -19,11 +59,21 @@ extensions: []
19
59
  extra_rdoc_files: []
20
60
  files:
21
61
  - lib/rails_tracepoint_stack.rb
62
+ - lib/rails_tracepoint_stack/configuration.rb
63
+ - lib/rails_tracepoint_stack/filter/gem_path.rb
64
+ - lib/rails_tracepoint_stack/filter/rb_config.rb
65
+ - lib/rails_tracepoint_stack/log_formatter.rb
66
+ - lib/rails_tracepoint_stack/logger.rb
67
+ - lib/rails_tracepoint_stack/trace.rb
68
+ - lib/rails_tracepoint_stack/trace_filter.rb
69
+ - lib/rails_tracepoint_stack/tracer.rb
70
+ - lib/rails_tracepoint_stack/version.rb
22
71
  homepage: https://github.com/carlosdanielpohlod/rails_tracepoint_stack/
23
72
  licenses:
24
73
  - MIT
25
74
  metadata:
26
75
  documentation_uri: https://github.com/carlosdanielpohlod/rails_tracepoint_stack/
76
+ changelog_uri: https://github.com/carlosdanielpohlod/rails_tracepoint_stack/blob/main/changelog.md
27
77
  post_install_message:
28
78
  rdoc_options: []
29
79
  require_paths: