rails_tracepoint_stack 0.2.0 → 0.3.1

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: 874f14c9b66717408755c8bae182634b457f23ae684ec5cc6eb1bb2b07aa5865
4
- data.tar.gz: e23e31fd3558896656efbb58796d9fc689f535b0835e646602eaa3590fba89af
3
+ metadata.gz: 8403ca731eb5b9b0c54af770d35dc805260a49e4a8d6092c3ae09ee83487864c
4
+ data.tar.gz: b518853ab09f13da65fbdee1c49fa6683dbec7e615de3b62e42834dbcb5b0812
5
5
  SHA512:
6
- metadata.gz: 13efe4146269be0aa124022ca0b7fca5eaa8a91611a9a8e1f59af24d82af70f815f9853b0a4b93b5c1dd7de9401af13341e82f59b5ace70a0b47b1298b446c6c
7
- data.tar.gz: 0bdc7578909e62f51dddf52b65e2de7a37d7afc1f8cdf794427bfda3e022ac1396627c3ffc2e5ee117fd322f81f1ec4f54ebad2172ffb2571779e8c890ff8bc3
6
+ metadata.gz: b449b592efe2f0a92dbae19fb5cb218fc3712f8d069a3caedefc286c1e29f533c6cf13fea0833a4a50aeb689cc1ea235e57788bed060221e1caa25b7a3fdde99
7
+ data.tar.gz: e039bd1ad9de1c78b29ab371d80f0a3b82755d96700c7c06c209f4ea9fe67adb60f3ac939cbad42c4341176ced07d3253db1deec2bae0a5fd61926f7361626b5
@@ -1,9 +1,16 @@
1
1
  module RailsTracepointStack
2
2
  class Configuration
3
- attr_accessor :ignore_patterns, :logger
3
+ attr_accessor :file_path_to_filter_patterns,
4
+ :ignore_patterns,
5
+ :log_format,
6
+ :log_external_sources,
7
+ :logger,
4
8
 
5
9
  def initialize
10
+ @file_path_to_filter_patterns = []
6
11
  @ignore_patterns = []
12
+ @log_format = :text
13
+ @log_external_sources = false
7
14
  @logger = nil
8
15
  end
9
16
  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
@@ -1,14 +1,14 @@
1
1
  module RailsTracepointStack
2
2
  class Logger
3
3
  def self.log(msg)
4
- unless RailsTracepointStack.configuration&.logger
5
- File.open('log/rails_tracepoint_stack.log', 'a') do |f|
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|
6
9
  f.puts msg
7
10
  end
8
- else
9
- RailsTracepointStack.configuration.logger.info(msg)
10
11
  end
11
12
  end
12
13
  end
13
14
  end
14
-
@@ -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
@@ -1,31 +1,63 @@
1
+ require 'rails_tracepoint_stack/filter/gem_path'
2
+ require 'rails_tracepoint_stack/filter/rb_config'
3
+
1
4
  module RailsTracepointStack
2
5
  class TraceFilter
3
6
  def self.ignore_trace?(trace:)
4
- start_with_to_ignore_prefixes?(trace) || from_gempath_or_lib_path?(trace) || is_a_to_ignore_pattern?(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
5
12
  end
6
13
 
7
14
  private
8
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
+ !RailsTracepointStack.configuration&.log_external_sources &&
22
+ (
23
+ file_path_starts_with_gem_path?(trace) ||
24
+ file_path_starts_with_ruby_lib_path?(trace) ||
25
+ file_path_starts_with_bundler_path?(trace)
26
+ )
27
+ end
28
+
29
+ def self.is_a_to_ignore_pattern?(trace)
30
+ RailsTracepointStack.configuration&.ignore_patterns&.any? { |pattern| trace.file_path.match?(pattern) }
31
+ end
32
+
33
+ def self.file_path_starts_with_gem_path?(trace)
34
+ gem_paths.any? { |path| trace.file_path.start_with?(path) }
35
+ end
36
+
37
+ def self.file_path_starts_with_ruby_lib_path?(trace)
38
+ trace.file_path.start_with?(ruby_lib_path)
39
+ end
40
+
41
+ def self.file_path_starts_with_bundler_path?(trace)
42
+ trace.file_path.include?("gems/bundler")
43
+ end
44
+
9
45
  def self.gem_paths
10
- @gem_paths ||= Bundler.load.specs.map(&:full_gem_path)
46
+ @gem_paths ||= RailsTracepointStack::Filter::GemPath.full_gem_path
11
47
  end
12
48
 
13
49
  def self.ruby_lib_path
14
- @ruby_lib_path ||= RbConfig::CONFIG['rubylibdir']
15
- end
16
-
17
- def self.start_with_to_ignore_prefixes?(trace)
18
- trace.path.start_with?('<internal:') || trace.path == '(eval)'
50
+ @ruby_lib_path ||= RailsTracepointStack::Filter::RbConfig.ruby_lib_path
19
51
  end
20
52
 
21
- def self.from_gempath_or_lib_path?(trace)
22
- gem_paths.any? { |path| trace.path.start_with?(path) } ||
23
- trace.path.start_with?(ruby_lib_path) ||
24
- trace.path.include?("gems/bundler")
53
+ def self.defined_file_path_to_filter_patterns?
54
+ RailsTracepointStack.configuration&.file_path_to_filter_patterns&.any?
25
55
  end
26
56
 
27
- def self.is_a_to_ignore_pattern?(trace)
28
- RailsTracepointStack.configuration&.ignore_patterns&.any? { |pattern| trace.path.match?(pattern) }
57
+ def self.not_matches_file_path_to_filter_patterns?(trace)
58
+ !RailsTracepointStack.configuration.file_path_to_filter_patterns.any? do |pattern|
59
+ trace.file_path.match?(pattern)
60
+ end
29
61
  end
30
62
  end
31
63
  end
@@ -1,25 +1,22 @@
1
+ #TODO: Move to a loader file
1
2
  require 'rails_tracepoint_stack/logger'
2
3
  require 'rails_tracepoint_stack/trace_filter'
4
+ require 'rails_tracepoint_stack/trace'
5
+ require 'rails_tracepoint_stack/log_formatter'
3
6
 
4
7
  module RailsTracepointStack
5
8
  class Tracer
9
+ # TODO: Tracer.new shoud return the tracer. Is weird to call Tracer.new.tracer
6
10
  def tracer
7
- @trace ||= TracePoint.new(:call) do |tp|
8
- next if RailsTracepointStack::TraceFilter.ignore_trace?(trace: tp)
11
+ @tracer ||= TracePoint.new(:call) do |tracepoint|
12
+ trace = RailsTracepointStack::Trace.new(trace_point: tracepoint)
9
13
 
10
- params = fetch_params(tp)
11
-
12
- RailsTracepointStack::Logger.log "called: #{tp.defined_class}##{tp.method_id} in #{tp.path}:#{tp.lineno} with params: #{params}"
13
- end
14
- end
14
+ next if RailsTracepointStack::TraceFilter.ignore_trace?(trace: trace)
15
15
 
16
- private
17
- attr_reader :gem_paths, :ruby_lib_path
18
-
19
- def fetch_params(tp)
20
- tp.binding.local_variables.map { |var|
21
- [var, tp.binding.local_variable_get(var)]
22
- }.to_h
16
+ # TODO: Use proper OO
17
+ message = RailsTracepointStack::LogFormatter.message trace
18
+ RailsTracepointStack::Logger.log message
19
+ end
23
20
  end
24
21
  end
25
22
  end
@@ -0,0 +1,3 @@
1
+ module RailsTracepointStack
2
+ VERSION = "0.3.1"
3
+ end
@@ -1,4 +1,6 @@
1
+ require 'json'
1
2
  require 'rails_tracepoint_stack/configuration'
3
+ require 'rails_tracepoint_stack/log_formatter'
2
4
  require 'rails_tracepoint_stack/tracer'
3
5
 
4
6
  $rails_tracer_rtps = nil
@@ -24,7 +26,7 @@ module RailsTracepointStack
24
26
  end
25
27
  end
26
28
 
27
- if ENV.fetch('RAILS_TRACEPOINT_STACK', 'false') == 'true'
29
+ if ENV.fetch('RAILS_TRACEPOINT_STACK_ENABLED', 'false') == 'true'
28
30
  $rails_tracer_rtps = RailsTracepointStack::Tracer.new.tracer
29
31
  $rails_tracer_rtps.enable
30
32
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_tracepoint_stack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
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-19 00:00:00.000000000 Z
11
+ date: 2024-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -16,40 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '6.0'
19
+ version: '3.0'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 6.0.0
22
+ version: 3.0.0
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '6.0'
29
+ version: '3.0'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 6.0.0
32
+ version: 3.0.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rake
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '12.0'
39
+ version: '13.0'
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: 12.0.0
42
+ version: 13.0.0
43
43
  type: :development
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - "~>"
48
48
  - !ruby/object:Gem::Version
49
- version: '12.0'
49
+ version: '13.0'
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: 12.0.0
52
+ version: 13.0.0
53
53
  description: A formatted output of all methods called in your rails application of
54
54
  code created by the developer, with the complete path to the class/module, including
55
55
  passed params.
@@ -60,9 +60,14 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - lib/rails_tracepoint_stack.rb
62
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
63
66
  - lib/rails_tracepoint_stack/logger.rb
67
+ - lib/rails_tracepoint_stack/trace.rb
64
68
  - lib/rails_tracepoint_stack/trace_filter.rb
65
69
  - lib/rails_tracepoint_stack/tracer.rb
70
+ - lib/rails_tracepoint_stack/version.rb
66
71
  homepage: https://github.com/carlosdanielpohlod/rails_tracepoint_stack/
67
72
  licenses:
68
73
  - MIT