Skip to content

Commit

Permalink
Merge pull request dejan#162 from dejan/source_path
Browse files Browse the repository at this point in the history
Introduce source_path configuration
  • Loading branch information
dejan authored Aug 3, 2019
2 parents cf6f566 + 541bdc2 commit 70be339
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 37 deletions.
12 changes: 12 additions & 0 deletions meta_request/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ end

List of available attributes and defaults can be found in [lib/meta_request/config.rb](lib/meta_request/config.rb).

## Docker

Apps runing in Docker container will have filepaths of the container so links to editor would not work. To fix this, you need to propagate working directory through enviroment variable `SOURCE_PATH`. With docker-compose it can be done like this:

```yaml
services:
app:
environment:
- SOURCE_PATH=$PWD
# ...
```

## Development

Run all tests:
Expand Down
26 changes: 14 additions & 12 deletions meta_request/lib/meta_request/app_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ class AppNotifications
payload[:options][k] = payload.delete(k) unless k.in? CACHE_KEY_COLUMNS
end

dev_callsite = Utils.dev_callsite(caller)

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

Event.new(name, start, ending, transaction_id, payload)
}
Expand All @@ -43,23 +40,28 @@ class AppNotifications

SQL_BLOCK = Proc.new {|*args|
name, start, ending, transaction_id, payload = args
dev_callsite = Utils.dev_callsite(caller)

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

Event.new(SQL_EVENT_NAME, start, ending, transaction_id, payload)
}

VIEW_BLOCK = Proc.new {|*args|
name, start, ending, transaction_id, payload = args
payload[:identifier] = MetaRequest::Utils.sub_source_path(payload[:identifier])

Event.new(name, start, ending, transaction_id, payload)
}

# Subscribe to all events relevant to RailsPanel
#
def self.subscribe
new.
subscribe("meta_request.log").
subscribe("sql.active_record", &SQL_BLOCK).
subscribe("sql.sequel", &SQL_BLOCK).
subscribe("render_partial.action_view").
subscribe("render_template.action_view").
subscribe("render_partial.action_view", &VIEW_BLOCK).
subscribe("render_template.action_view", &VIEW_BLOCK).
subscribe("process_action.action_controller.exception").
subscribe("process_action.action_controller") do |*args|
name, start, ending, transaction_id, payload = args
Expand Down
6 changes: 5 additions & 1 deletion meta_request/lib/meta_request/config.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module MetaRequest
class Config
attr_writer :logger, :storage_pool_size
attr_writer :logger, :storage_pool_size, :source_path

# logger used for reporting gem's fatal errors
def logger
Expand All @@ -12,5 +12,9 @@ def logger
def storage_pool_size
@storage_pool_size ||= 20
end

def source_path
@source_path ||= ENV['SOURCE_PATH'] || Rails.root.to_s
end
end
end
7 changes: 3 additions & 4 deletions meta_request/lib/meta_request/log_interceptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ def unknown(message=nil, *args)
super
end


private
def push_event(level, message)
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}
callsite = AppRequest.current && Utils.dev_callsite(caller.drop(1))
if callsite
payload = callsite.merge(message: message, level: level)
AppRequest.current.events << Event.new('meta_request.log', 0, 0, 0, payload)
end
rescue Exception => e
Expand Down
22 changes: 17 additions & 5 deletions meta_request/lib/meta_request/utils.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
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
app_line = caller.detect { |c| c.start_with? MetaRequest.rails_root }
return nil unless app_line

_, filename, _, line, _, method = app_line.split(/^(.*?)(:(\d+))(:in `(.*)')?$/)

{
filename: sub_source_path(filename),
line: line.to_i,
method: method
}
end

def sub_source_path(path)
rails_root = MetaRequest.rails_root
source_path = MetaRequest.config.source_path
return path if rails_root == source_path
path.sub(rails_root, source_path)
end
end
end
1 change: 0 additions & 1 deletion meta_request/meta_request.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Gem::Specification.new do |gem|

gem.add_dependency 'railties', '>= 3.0.0', '< 7'
gem.add_dependency 'rack-contrib', '>= 1.1', '< 3'
gem.add_dependency 'callsite', '~> 0.0', '>= 0.0.11'

gem.files = Dir['README.md', 'lib/**/*.rb']
end
38 changes: 24 additions & 14 deletions meta_request/test/unit/meta_request/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,45 @@

describe MetaRequest::Utils do
describe '.dev_callsite' do
it 'returns line parsed with Callsite if rails root is found in multi-line trace' do
it 'returns first call site from the app iself' do
filename = File.join(MetaRequest.rails_root, "test_file.rb")
line = 87
method = 'app_func'

stacktrace = [
"/gem/gem_file.rb:1:in `func'`",
"#{File.join(MetaRequest.rails_root, "test_file.rb")}:87:in `app_func'",
"#{filename}:#{line}:in `#{method}'",
"/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)

expected = { filename: filename, line: line, method: method }
assert_equal expected, MetaRequest::Utils.dev_callsite(stacktrace)
end

it "returns nil if multi-line trace doesn't match app root" do
it 'returns nil if the stacktrace contains only call sites outside of the app' 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 'replaces filename with the proovided source path configuration' do
MetaRequest.config.source_path = '/Users/foo/bar'

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

assert_nil MetaRequest::Utils.dev_callsite(stacktrace)
expected = { filename: '/Users/foo/bar/test_file.rb', line: line, method: method }
assert_equal expected, MetaRequest::Utils.dev_callsite(stacktrace)

# revert configuration
MetaRequest.config.source_path = MetaRequest.rails_root
end
end
end

0 comments on commit 70be339

Please sign in to comment.