Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dev caller location #146

Merged
merged 6 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meta_request/lib/meta_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module MetaRequest
autoload :Middlewares, "meta_request/middlewares"
autoload :LogInterceptor, "meta_request/log_interceptor"
autoload :AppNotifications, "meta_request/app_notifications"
autoload :Utils, "meta_request/utils"

def self.config
@config ||= Config.new
Expand Down
17 changes: 9 additions & 8 deletions meta_request/lib/meta_request/app_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class AppNotifications
payload[:options][k] = payload.delete(k) unless k.in? CACHE_KEY_COLUMNS
end

dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
if dev_caller
c = Callsite.parse(dev_caller)
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
dev_callsite = Utils.dev_callsite(caller)

if dev_callsite
payload.merge!(:line => dev_callsite.line, :filename => dev_callsite.filename, :method => dev_callsite.method)
end

Event.new(name, start, ending, transaction_id, payload)
Expand All @@ -43,11 +43,12 @@ class AppNotifications

SQL_BLOCK = Proc.new {|*args|
name, start, ending, transaction_id, payload = args
dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
if dev_caller
c = Callsite.parse(dev_caller)
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
dev_callsite = Utils.dev_callsite(caller)

if dev_callsite
payload.merge!(:line => dev_callsite.line, :filename => dev_callsite.filename, :method => dev_callsite.method)
end

Event.new(SQL_EVENT_NAME, start, ending, transaction_id, payload)
}
# Subscribe to all events relevant to RailsPanel
Expand Down
9 changes: 3 additions & 6 deletions meta_request/lib/meta_request/log_interceptor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'callsite'

module MetaRequest
module LogInterceptor

Expand Down Expand Up @@ -36,10 +34,9 @@ def unknown(message=nil, *args)

private
def push_event(level, message)
dev_log = AppRequest.current && caller[1].include?(MetaRequest.rails_root)
if dev_log
c = Callsite.parse(caller[1])
payload = {:message => message, :level => level, :line => c.line, :filename => c.filename, :method => c.method}
dev_callsite = AppRequest.current && Utils.dev_callsite(caller[1])
if dev_callsite
payload = {:message => message, :level => level, :line => dev_callsite.line, :filename => dev_callsite.filename, :method => dev_callsite.method}
AppRequest.current.events << Event.new('meta_request.log', 0, 0, 0, payload)
end
rescue Exception => e
Expand Down
13 changes: 13 additions & 0 deletions meta_request/lib/meta_request/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'callsite'

module MetaRequest
module Utils
extend self

# @return [Callsite::Line, nil]
def dev_callsite(caller)
app_line = Array(caller).detect { |c| c.start_with? MetaRequest.rails_root }
Callsite.parse(app_line) if app_line
end
end
end
36 changes: 36 additions & 0 deletions meta_request/test/unit/meta_request/utils_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'test_helper'

describe MetaRequest::Utils do
describe '.dev_callsite' do
it 'returns line parsed with Callsite if rails root is found in multi-line trace' do
stacktrace = [
"/gem/gem_file.rb:1:in `func'`",
"#{File.join(MetaRequest.rails_root, "test_file.rb")}:87:in `app_func'",
"/gem/gem_file.rb:1:in `func2'`",
]
expected_callsite_line = Callsite::Line.new("#{Rails.root}/test_file.rb", 87, 'app_func')
assert_equal expected_callsite_line, MetaRequest::Utils.dev_callsite(stacktrace)
end

it "returns nil if multi-line trace doesn't match app root" do
stacktrace = [
"/gem/gem_file.rb:1:in `func'`",
"/prefix/#{File.join(MetaRequest.rails_root, "test_file.rb")}:87:in `app_func'",
"/gem/gem_file.rb:1:in `func2'`",
]
assert_nil MetaRequest::Utils.dev_callsite(stacktrace)
end

it 'returns line parsed with Callsite if rails root is found in a single line trace' do
stacktrace = "#{File.join(MetaRequest.rails_root, "test_file.rb")}:87:in `app_func'"
expected_callsite_line = Callsite::Line.new("#{Rails.root}/test_file.rb", 87, "app_func")
assert_equal expected_callsite_line, MetaRequest::Utils.dev_callsite(stacktrace)
end

it "returns nil if single-line trace doesn't match app root" do
stacktrace = "/gem/gem_file.rb:1:in `func'`"

assert_nil MetaRequest::Utils.dev_callsite(stacktrace)
end
end
end