Skip to content

Commit

Permalink
Added method source analysis. Utilized for code refactoring suggestio…
Browse files Browse the repository at this point in the history
…ns in Call Stacking SaaS app. Off by default.

Can be enabled by editing ~/.callstacking and changing the analyze_source flag from false to true.
  • Loading branch information
aantix committed Nov 27, 2023
1 parent 1737f9e commit 2e14609
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 25 deletions.
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PATH
callstacking-rails (0.1.38)
faraday (>= 1.10.3)
faraday-follow_redirects
method_source
rails (>= 4)

GEM
Expand Down
1 change: 1 addition & 0 deletions callstacking-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "rails", ">= 4"
spec.add_dependency "faraday", '>= 1.10.3'
spec.add_dependency 'faraday-follow_redirects'
spec.add_dependency 'method_source'
spec.add_development_dependency 'mocha'
spec.add_development_dependency 'minitest-silence'
end
14 changes: 10 additions & 4 deletions lib/callstacking/rails/instrument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def instrument_method(klass, method_name, application_level: true)
path = method(__method__).super_method.source_location&.first || ''
line_no = method(__method__).super_method.source_location&.last || ''

method_source = ''
method_source = method(__method__).super_method.source if settings.analyze_source?

p, l = caller.find { |c| c.to_s =~ /#{::Rails.root.to_s}/}&.split(':')

spans = tmp_module.instance_variable_get(:@spans)
Expand All @@ -50,9 +53,9 @@ def instrument_method(klass, method_name, application_level: true)

arguments = Callstacking::Rails::Instrument.arguments_for(method(__method__).super_method, args)

span.call_entry(klass, method_name, arguments, p || path, l || line_no)
span.call_entry(klass, method_name, arguments, p || path, l || line_no, method_source)
return_val = super(*args, &block)
span.call_return(klass, method_name, p || path, l || line_no, return_val)
span.call_return(klass, method_name, p || path, l || line_no, return_val, method_source)

return_val
end
Expand All @@ -67,6 +70,9 @@ def instrument_method(klass, method_name, application_level: true)
path = method(__method__).super_method.source_location&.first || ''
line_no = method(__method__).super_method.source_location&.last || ''

method_source = ''
method_source = method(__method__).super_method.source if settings.analyze_source?

p, l = caller.find { |c| c.to_s =~ /#{::Rails.root.to_s}/}&.split(':')

spans = tmp_module.instance_variable_get(:@spans)
Expand All @@ -75,9 +81,9 @@ def instrument_method(klass, method_name, application_level: true)

arguments = Callstacking::Rails::Instrument.arguments_for(method(__method__).super_method, args)

span.call_entry(klass, method_name, arguments, p || path, l || line_no)
span.call_entry(klass, method_name, arguments, p || path, l || line_no, method_source)
return_val = super(*args, **kwargs, &block)
span.call_return(klass, method_name, p || path, l || line_no, return_val)
span.call_return(klass, method_name, p || path, l || line_no, return_val, method_source)

return_val
end
Expand Down
7 changes: 6 additions & 1 deletion lib/callstacking/rails/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ def disabled?
!enabled?
end

def analyze_source?
settings[:analyze_source] || false
end

def save(email, password, url)
props = { auth_token: '',
url: url,
enabled: true
enabled: true,
analyze_source: false,
}

props = { Callstacking::Rails::Env.environment => {
Expand Down
8 changes: 4 additions & 4 deletions lib/callstacking/rails/spans.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def increment_nesting_level
@nesting_level
end

def call_entry(klass, method_name, arguments, path, line_no)
def call_entry(klass, method_name, arguments, path, line_no, method_source)
@nesting_level+=1
@previous_entry = previous_event(klass, method_name)
@call_entry_callback.call(@nesting_level, increment_order_num, klass, method_name, arguments, path, line_no)
@call_entry_callback.call(@nesting_level, increment_order_num, klass, method_name, arguments, path, line_no, method_source)
end

def call_return(klass, method_name, path, line_no, return_val)
def call_return(klass, method_name, path, line_no, return_val, method_source)
@call_return_callback.call(coupled_callee(klass, method_name), @nesting_level,
increment_order_num, klass, method_name, path, line_no, return_val)
increment_order_num, klass, method_name, path, line_no, return_val, method_source)
@nesting_level-=1
end

Expand Down
15 changes: 9 additions & 6 deletions lib/callstacking/rails/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ def self.trace_log_clear
private

def init_callbacks(tuid)
@spans.on_call_entry do |nesting_level, order_num, klass, method_name, arguments, path, line_no|
create_call_entry(tuid, nesting_level, order_num, klass, method_name, arguments, path, line_no, @traces)
@spans.on_call_entry do |nesting_level, order_num, klass, method_name, arguments, path, line_no, method_source|
create_call_entry(tuid, nesting_level, order_num, klass, method_name, arguments, path, line_no, @traces, method_source)
end

@spans.on_call_return do |coupled_callee, nesting_level, order_num, klass, method_name, path, line_no, return_val|
create_call_return(tuid, coupled_callee, nesting_level, order_num, klass, method_name, path, line_no, return_val, @traces)
@spans.on_call_return do |coupled_callee, nesting_level, order_num, klass, method_name, path, line_no, return_val, method_source|
create_call_return(tuid, coupled_callee, nesting_level, order_num, klass, method_name, path, line_no, return_val, @traces, method_source)
end
end

Expand All @@ -97,7 +97,7 @@ def exception_message(exception, method, controller, action, format)
"#{exception.backtrace[0]}".html_safe
end

def create_call_return(tuid, coupled_callee, nesting_level, order_num, klass, method_name, path, line_no, return_val, traces)
def create_call_return(tuid, coupled_callee, nesting_level, order_num, klass, method_name, path, line_no, return_val, traces, method_source)
lock.synchronize do
traces << { tuid: tuid,
type: TRACE_CALL_RETURN,
Expand All @@ -112,11 +112,12 @@ def create_call_return(tuid, coupled_callee, nesting_level, order_num, klass, me
return_value: return_value(return_val),
coupled_callee: coupled_callee,
message: nil,
method_source: method_source,
}
end
end

def create_call_entry(tuid, nesting_level, order_num, klass, method_name, arguments, path, line_no, traces)
def create_call_entry(tuid, nesting_level, order_num, klass, method_name, arguments, path, line_no, traces, method_source)
lock.synchronize do
traces << { tuid: tuid,
type: TRACE_CALL_ENTRY,
Expand All @@ -131,6 +132,7 @@ def create_call_entry(tuid, nesting_level, order_num, klass, method_name, argume
coupled_callee: nil,
local_variables: {},
message: nil,
method_source: method_source,
}
end
end
Expand All @@ -150,6 +152,7 @@ def create_message(tuid, message, order_num, traces, type = TRACE_MESSAGE)
return_value: nil,
coupled_callee: false,
local_variables: {},
method_source: '',
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/callstacking/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Callstacking
module Rails
VERSION = "0.1.38"
VERSION = "0.1.39"
end
end

0 comments on commit 2e14609

Please sign in to comment.