tapping_device 0.4.9 → 0.5.2
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/.DS_Store +0 -0
- data/.github/workflows/ruby.yml +11 -4
- data/.ruby-version +1 -0
- data/Gemfile.lock +21 -23
- data/README.md +136 -353
- data/images/print_calls - single entry.png +0 -0
- data/images/print_calls.png +0 -0
- data/images/print_mutations.png +0 -0
- data/images/print_traces.png +0 -0
- data/lib/tapping_device.rb +108 -112
- data/lib/tapping_device/exceptions.rb +12 -0
- data/lib/tapping_device/output_payload.rb +173 -0
- data/lib/tapping_device/payload.rb +3 -44
- data/lib/tapping_device/trackable.rb +56 -17
- data/lib/tapping_device/trackers/association_call_tracker.rb +17 -0
- data/lib/tapping_device/trackers/initialization_tracker.rb +27 -0
- data/lib/tapping_device/trackers/method_call_tracker.rb +9 -0
- data/lib/tapping_device/trackers/mutation_tracker.rb +132 -0
- data/lib/tapping_device/trackers/passed_tracker.rb +16 -0
- data/lib/tapping_device/version.rb +1 -1
- data/tapping_device.gemspec +4 -5
- metadata +21 -22
- data/lib/tapping_device/sql_tapping_methods.rb +0 -89
@@ -1,89 +0,0 @@
|
|
1
|
-
class TappingDevice
|
2
|
-
module SqlTappingMethods
|
3
|
-
CALL_STACK_SKIPPABLE_METHODS = [:transaction, :tap]
|
4
|
-
|
5
|
-
# SQLListener acts like an interface for us to intercept activerecord query instrumentations
|
6
|
-
# this means we only need to register one subscriber no matter how many objects we want to tap on
|
7
|
-
class SQLListener
|
8
|
-
def call(name, start, finish, message_id, values);end
|
9
|
-
end
|
10
|
-
@@sql_listener = SQLListener.new
|
11
|
-
|
12
|
-
ActiveSupport::Notifications.subscribe("sql.active_record", @@sql_listener)
|
13
|
-
|
14
|
-
def tap_sql!(object)
|
15
|
-
@call_stack = []
|
16
|
-
@target ||= object
|
17
|
-
@trace_point = with_trace_point_on_target(object, event: [:call, :c_call]) do |start_tp|
|
18
|
-
########## Check if the call is worth recording ##########
|
19
|
-
filepath, line_number = get_call_location(start_tp, padding: 1) # we need extra padding because of `with_trace_point_on_target`
|
20
|
-
method = start_tp.callee_id
|
21
|
-
next if already_recording?(method)
|
22
|
-
|
23
|
-
########## Start the recording ##########
|
24
|
-
# 1. Mark recording state by pushing method into @call_stack
|
25
|
-
# 2. Subscribe sql instrumentations generated by activerecord
|
26
|
-
# 3. Record those sqls and run device callbacks
|
27
|
-
# 4. Start waiting for current call's return callback
|
28
|
-
@call_stack.push(method)
|
29
|
-
payload = build_payload(tp: start_tp, filepath: filepath, line_number: line_number)
|
30
|
-
device = tap_on_sql_instrumentation!(payload)
|
31
|
-
|
32
|
-
with_trace_point_on_target(object, event: :return) do |return_tp|
|
33
|
-
next unless return_tp.callee_id == method
|
34
|
-
|
35
|
-
########## End recording ##########
|
36
|
-
# 1. Close itself
|
37
|
-
# 2. Stop our subscription on SQLListener
|
38
|
-
# 3. Remove current method from @call_stack
|
39
|
-
# 4. Stop the device if stop condition is fulfilled
|
40
|
-
return_tp.disable
|
41
|
-
device.stop!
|
42
|
-
@call_stack.pop
|
43
|
-
stop_if_condition_fulfilled(payload)
|
44
|
-
|
45
|
-
########## Track descendant objects ##########
|
46
|
-
# if the method creates another Relation object
|
47
|
-
if return_tp.defined_class == ActiveRecord::QueryMethods
|
48
|
-
create_child_device.tap_sql!(return_tp.return_value)
|
49
|
-
end
|
50
|
-
end.enable
|
51
|
-
end
|
52
|
-
|
53
|
-
@trace_point.enable unless self.class.suspend_new
|
54
|
-
|
55
|
-
self
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def tap_on_sql_instrumentation!(payload)
|
62
|
-
device = TappingDevice.new do |sql_listener_payload|
|
63
|
-
values = sql_listener_payload.arguments[:values]
|
64
|
-
|
65
|
-
next if should_be_skipped_by_paths?(payload.filepath) ||
|
66
|
-
["SCHEMA", "TRANSACTION"].include?(values[:name]) ||
|
67
|
-
values[:sql].match?(/SAVEPOINT/)
|
68
|
-
|
69
|
-
payload[:sql] = values[:sql]
|
70
|
-
record_call!(payload)
|
71
|
-
end
|
72
|
-
device.tap_on!(@@sql_listener)
|
73
|
-
end
|
74
|
-
|
75
|
-
# usually, AR's query methods (like `first`) will end up calling `find_by_sql`
|
76
|
-
# then to TappingDevice, both `first` and `find_by_sql` generates the sql
|
77
|
-
# but the results are duplicated, we should only consider the `first` call
|
78
|
-
def already_recording?(method)
|
79
|
-
!@call_stack.empty? || CALL_STACK_SKIPPABLE_METHODS.include?(method)
|
80
|
-
end
|
81
|
-
|
82
|
-
def with_trace_point_on_target(object, event:)
|
83
|
-
TracePoint.new(*event) do |tp|
|
84
|
-
if is_from_target?(object, tp)
|
85
|
-
yield(tp)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|