Skip to content

Commit

Permalink
Merge branch 'master' into update-action-view-instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jrothrock committed Jan 26, 2024
2 parents 4bbfe82 + a124643 commit fc3414e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/scout_apm/app_server_load.rb
Expand Up @@ -51,7 +51,7 @@ def data
ensure
# Sometimes :database_engine and :database_adapter can cause a reference to an AR connection.
# Make sure we release all AR connections held by this thread.
ActiveRecord::Base.clear_active_connections! if Utils::KlassHelper.defined?("ActiveRecord::Base")
ActiveRecord::Base.connection_handler.clear_active_connections! if Utils::KlassHelper.defined?("ActiveRecord::Base")
end

# Calls `.to_s` on the object passed in.
Expand Down
21 changes: 21 additions & 0 deletions lib/scout_apm/auto_instrument/rails.rb
Expand Up @@ -135,6 +135,27 @@ def on_send(node)
wrap(node.location.expression, *instrument(node.location.expression.source, file_name, line))
end

def on_hash(node)
node.children.each do |pair|
# Skip `pair` if we're sure it's not using the hash shorthand syntax
next if pair.type != :pair
key_node, value_node = pair.children
next unless key_node.type == :sym && value_node.type == :send
key = key_node.children[0]
next unless value_node.children[0].nil? && key == value_node.children[1]

# Extract useful metadata for instrumentation:
line = pair.location.line || 'line?'
# column = pair.location.column || 'column?' # not used
# method_name = key || '*unknown*' # not used
file_name = @source_rewriter.source_buffer.name

instrument_before, instrument_after = instrument(pair.location.expression.source, file_name, line)
replace(pair.loc.expression, "#{key}: #{instrument_before}#{key}#{instrument_after}")
end
super
end

# def on_class(node)
# class_name = node.children[1]
#
Expand Down
4 changes: 2 additions & 2 deletions scout_apm.gemspec
Expand Up @@ -28,10 +28,10 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake-compiler"
s.add_development_dependency "addressable"
s.add_development_dependency "activesupport"
s.add_runtime_dependency "parser"
s.add_runtime_dependency "parser", Gem::Version.new(RUBY_VERSION).approximate_recommendation

# These are general development dependencies which are used in instrumentation
# tests. Specific versions are pulled in using specific gemfiles, e.g.
# tests. Specific versions are pulled in using specific gemfiles, e.g.
# `gems/rails3.gemfile`.
s.add_development_dependency "activerecord"
s.add_development_dependency "sqlite3"
Expand Down
@@ -0,0 +1,36 @@

class HashShorthandController < ApplicationController
def hash
json = {
static: "static",
shorthand: ::ScoutApm::AutoInstrument("shorthand:",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:6:in `hash'"]){shorthand},
longhand: ::ScoutApm::AutoInstrument("longhand: longhand",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:7:in `hash'"]){longhand},
longhand_different_key: ::ScoutApm::AutoInstrument("longhand",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:8:in `hash'"]){longhand},
hash_rocket: ::ScoutApm::AutoInstrument(":hash_rocket => hash_rocket",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:9:in `hash'"]){hash_rocket},
:hash_rocket_different_key => ::ScoutApm::AutoInstrument("hash_rocket",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:10:in `hash'"]){hash_rocket},
non_nil_receiver: ::ScoutApm::AutoInstrument("non_nil_receiver.value",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:11:in `hash'"]){non_nil_receiver.value},
nested: {
shorthand: ::ScoutApm::AutoInstrument("shorthand:",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:13:in `hash'"]){shorthand},
}
}
::ScoutApm::AutoInstrument("render json:",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:16:in `hash'"]){render json:}
end

private

def shorthand
"shorthand"
end

def longhand
"longhand"
end

def hash_rocket
"hash_rocket"
end

def non_nil_receiver
::ScoutApm::AutoInstrument("OpenStruct.new(value: \"value\")",["ROOT/test/unit/auto_instrument/hash_shorthand_controller.rb:34:in `non_nil_receiver'"]){OpenStruct.new(value: "value")}
end
end
36 changes: 36 additions & 0 deletions test/unit/auto_instrument/hash_shorthand_controller.rb
@@ -0,0 +1,36 @@

class HashShorthandController < ApplicationController
def hash
json = {
static: "static",
shorthand:,
longhand: longhand,
longhand_different_key: longhand,
:hash_rocket => hash_rocket,
:hash_rocket_different_key => hash_rocket,
non_nil_receiver: non_nil_receiver.value,
nested: {
shorthand:,
}
}
render json:
end

private

def shorthand
"shorthand"
end

def longhand
"longhand"
end

def hash_rocket
"hash_rocket"
end

def non_nil_receiver
OpenStruct.new(value: "value")
end
end
8 changes: 7 additions & 1 deletion test/unit/auto_instrument_test.rb
Expand Up @@ -4,7 +4,7 @@

class AutoInstrumentTest < Minitest::Test
ROOT = File.expand_path("../../", __dir__)

def source_path(name)
File.expand_path("auto_instrument/#{name}.rb", __dir__)
end
Expand Down Expand Up @@ -38,6 +38,12 @@ def test_controller_rewrite
normalize_backtrace(::ScoutApm::AutoInstrument::Rails.rewrite(source_path("controller")))
end

def test_controller_rewrite_hash_shorthand
skip if RUBY_VERSION < "3.1"
assert_equal instrumented_source("hash_shorthand_controller"),
normalize_backtrace(::ScoutApm::AutoInstrument::Rails.rewrite(source_path("hash_shorthand_controller")))
end

def test_rescue_from_rewrite
# update_instrumented_source("rescue_from")

Expand Down

0 comments on commit fc3414e

Please sign in to comment.