Share
Embed
TracePointを活用してモデル名変更の負債解消をした話
2023-10-27 Kaigi on Rails 発表資料
https://kaigionrails.org/2023/talks/alpaca-tc/
誰もが開発時に直面する「技術的負債」。 あなたもRailsの世界でこの大敵との戦いで手をこまねいていませんか? 特にRubyのような動的言語では、一見シンプルな置換作業でも予期しない障壁が待ち受けています。
私たちのチームは、命名規則の誤りから生じたモデル名の技術的負債と向き合い、60,000行以上の変更を成功させました。 その成功のカギとなったのは「TracePoint」。 さまざまなイベント(メソッド呼び出しなど)をトレースすることができるRubyの強力な標準ライブラリでした。
このセッションでは、TracePointの基本的な使い方や、さらには技術的負債の解消のノウハウを実例とともに紹介します。 この機会に、巨大な技術的負債との戦いに悩むあなたの手札にTracePointという武器を加えてみませんか。
alpaca-tc
October 27, 2023
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
61 slides
-
ͳͥࢲࠓൃදΛ͢Δͷ͔ w ѱ໋໊͍نଇͩͬͨͨΊɺͯ͢ͷϞσϧ໊Λมߋ͢Δෛ࠴ղফ w DPNNJU ߦ w ӨڹൣғΞϓϦέʔγϣϯશମ
w 5SBDF1PJOU ΠϕϯτΛτϨʔε͢Δඪ४ϥΠϒϥϦ Λ׆༻ͯ͠ w ࣄྫ͕গͳ͍ͷͰɺօ༷ʹཱͭώϯτΛൃ৴Ͱ͖Δ͔ -
1SPKFDU4IFFU4FDUJPO*UFN*OQVU/VNCFS w ϞσϧΛBQQNPEFMTԼʹஔ w มߋ͕ඞཁͳͷ w ఆ Ϟσϧ໊ ɺϑΝΠϧύε w
ؔ࿈໊ͷϝλϓϩDSFBUF@YYY CVJME@YYY w ΧϥϜ໊YYY@JE w 'BDUPSZ#PUɺ9YY%FDPSBUPSɺ"3ϝιου܈XIFSFKPJOTʜ -
5SBDF1PJOU ର ར༻ՕॴΛಛఆ ஔॲཧ w ఆ໊ w ؔ࿈໊ w ΧϥϜ໊
T0ME/FXH GJMFQBUIUPBSC- GJMFQBUIUPCSC- GJMFQBUIUPD@TQFDSC- -
5SBDF1PJOUͷجຊ w ඪ४ϥΠϒϥϦ w 3VCZ্ͷ༷ʑͳΠϕϯτΛϑοΫͰ͖Δ w DBMM3VCZͰهड़͞Εͨϝιουͷݺͼग़͠ w MJOFࣜͷධՁ w
SFUVSO3VCZͰهड़͞Εͨϝιουݺͼग़͔͠ΒͷϦλʔϯ w SBJTFྫ֎ͷൃੜ w ʜଞଟ -
trace_point = TracePoint.new(:call) do |tp| pp([tp.event, tp.method_id, tp.path, tp.defined_class]) end
def hello "hello" end def say_hello hello end trace_point.enable say_hello -
trace_point = TracePoint.new(:call) do |tp| pp([tp.event, tp.method_id, tp.path, tp.defined_class]) end
def hello "hello" end def say_hello hello end trace_point.enable say_hello ΠϕϯτͷτϨʔεΛ։࢝ -
trace_point = TracePoint.new(:call) do |tp| pp([tp.event, tp.method_id, tp.path, tp.defined_class]) end
def hello "hello" end def say_hello hello end trace_point.enable say_hello ϝιουݺͼग़͠ -
trace_point = TracePoint.new(:call) do |tp| pp([tp.event, tp.method_id, tp.path, tp.defined_class]) end
def hello "hello" end def say_hello hello end trace_point.enable say_hello ϒϩοΫ͕࣮ߦ #=> [:call, :say_hello, "/private/tmp/xxx.rb", Object] -
trace_point = TracePoint.new(:call) do |tp| pp([tp.event, tp.method_id, tp.path, tp.defined_class]) end
def hello "hello" end def say_hello hello end trace_point.enable say_hello #=> [:call, :say_hello, "/private/tmp/xxx.rb", Object] ϝιουݺͼग़͠ -
trace_point = TracePoint.new(:call) do |tp| pp([tp.event, tp.method_id, tp.path, tp.defined_class]) end
def hello "hello" end def say_hello hello end trace_point.enable say_hello ϒϩοΫ͕࣮ߦ #=> [:call, :say_hello, "/private/tmp/xxx.rb", Object] #=> [:call, :hello, "/private/tmp/xxx.rb", Object] -
trace_point = TracePoint.new(:call) do |tp| pp([tp.event, tp.method_id, tp.path, tp.defined_class]) end
def hello "hello" end def say_hello hello end trace_point.enable say_hello w EF fi OFE@DMBTTϝιουΛఆٛͨ͠Ϋϥε͔Ϟδϡʔϧ w TFMGΠϕϯτΛൃੜͤͨ͞ΦϒδΣΫτ w NFUIPE@JEϝιουͷఆٛ࣌ͷ໊લ w QBSNFUFSTϝιουͷύϥϝʔλఆٛ w CJOEJOHϝιουͷCJOEJOHɻFWBMΛ͑ͳΜͰͰ͖Δ -
# 今回の置換では、appかspec配下にあるファイルが対象 TARGETS = [ Rails.root.join('app').to_s, Rails.root.join('spec').to_s ].freeze # @return
[Thread::Backtrace::Location] ファイルパスや行数情報 def find_caller_location Thread.each_caller_location do |location| path = location.path next if path == __FILE__ return location if TARGETS.any? { path.start_with?(_1) } end end #=> "app/models/input_number.rb:11:in `tenant'" location = find_caller_location -
# 今回の置換では、appかspec配下にあるファイルが対象 TARGETS = [ Rails.root.join('app').to_s, Rails.root.join('spec').to_s ].freeze # @return
[Thread::Backtrace::Location] ファイルパスや行数情報 def find_caller_location Thread.each_caller_location do |location| path = location.path next if path == __FILE__ return location if TARGETS.any? { path.start_with?(_1) } end end #=> "app/models/input_number.rb:11:in `tenant'" location = find_caller_location ஔରΛࢦఆ -
# 今回の置換では、appかspec配下にあるファイルが対象 TARGETS = [ Rails.root.join('app').to_s, Rails.root.join('spec').to_s ].freeze # @return
[Thread::Backtrace::Location] ファイルパスや行数情報 def find_caller_location Thread.each_caller_location do |location| path = location.path next if path == __FILE__ return location if TARGETS.any? { path.start_with?(_1) } end end #=> "app/models/input_number.rb:11:in `tenant'" location = find_caller_location ελοΫΛॱʹḪΔ -
# 今回の置換では、appかspec配下にあるファイルが対象 TARGETS = [ Rails.root.join('app').to_s, Rails.root.join('spec').to_s ].freeze # @return
[Thread::Backtrace::Location] ファイルパスや行数情報 def find_caller_location Thread.each_caller_location do |location| path = location.path next if path == __FILE__ return location if TARGETS.any? { path.start_with?(_1) } end end #=> "app/models/input_number.rb:11:in `tenant'" location = find_caller_location ஔରͳΒΛฦ͢ -
# 今回の置換では、appかspec配下にあるファイルが対象 TARGETS = [ Rails.root.join('app').to_s, Rails.root.join('spec').to_s ].freeze # @return
[Thread::Backtrace::Location] ファイルパスや行数情報 def find_caller_location Thread.each_caller_location do |location| path = location.path next if path == __FILE__ return location if TARGETS.any? { path.start_with?(_1) } end end #=> "app/models/input_number.rb:11:in `tenant'" location = find_caller_location ύεͱߦ͕Θ͔Δ -
class Hello def hello puts "hello" end end 3VCZ7."CTUSBDU4ZOUBY5SFFQBSTF SCOPE@#13
CLASS@#12 COLON2(:Hello)@#0 SCOPE@#11 BLOCK@#9 BEGIN@#1 DEFN(:hello)@#3 SCOPE@#8 ARGS@#4 FCALL(puts)@#5 LIST@#7 STR(hello)@#6 ιʔείʔυ நߏจ "45 -
class Hello def hello puts "hello" end end SCOPE@#13 CLASS@#12
COLON2(:Hello)@#0 SCOPE@#11 BLOCK@#9 BEGIN@#1 DEFN(:hello)@#3 SCOPE@#8 ARGS@#4 FCALL(puts)@#5 LIST@#7 STR(hello)@#6 3VCZ7."CTUSBDU4ZOUBY5SFFQBSTF ֤ϊʔυϢχʔΫͳOPEF@JEΛ࣋ͭ ιʔείʔυ நߏจ "45 -
# 呼び出し元のThread::Backtrace::Locationを取得 caller_location = find_caller_location # node_idを取得 node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(
caller_location ) # ファイルパス + node_idで正確なソースコードを取得できる ast = RubyVM::AbstractSyntaxTree.parse_file( caller_location.path, keep_script_lines: true ) node = find_node_id(ast, node_id) #=> 正確な位置情報やソースコードを取得できる [node.first_lineno, node.first_column, node.last_lineno, node.last_column] node.source -
# 呼び出し元のThread::Backtrace::Locationを取得 caller_location = find_caller_location # node_idを取得 node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(
caller_location ) # ファイルパス + node_idで正確なソースコードを取得できる ast = RubyVM::AbstractSyntaxTree.parse_file( caller_location.path, keep_script_lines: true ) node = find_node_id(ast, node_id) #=> 正確な位置情報やソースコードを取得できる [node.first_lineno, node.first_column, node.last_lineno, node.last_column] node.source ݺͼग़͠ݩͷใΛऔಘ -
# 呼び出し元のThread::Backtrace::Locationを取得 caller_location = find_caller_location # node_idを取得 node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(
caller_location ) # ファイルパス + node_idで正確なソースコードを取得できる ast = RubyVM::AbstractSyntaxTree.parse_file( caller_location.path, keep_script_lines: true ) node = find_node_id(ast, node_id) #=> 正確な位置情報やソースコードを取得できる [node.first_lineno, node.first_column, node.last_lineno, node.last_column] node.source OPEF@JEʹม -
# 呼び出し元のThread::Backtrace::Locationを取得 caller_location = find_caller_location # node_idを取得 node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(
caller_location ) # ファイルパス + node_idで正確なソースコードを取得できる ast = RubyVM::AbstractSyntaxTree.parse_file( caller_location.path, keep_script_lines: true ) node = find_node_id(ast, node_id) #=> 正確な位置情報やソースコードを取得できる [node.first_lineno, node.first_column, node.last_lineno, node.last_column] node.source "45͔ΒҰக͢ΔOPEFΛऔಘ -
# 呼び出し元のThread::Backtrace::Locationを取得 caller_location = find_caller_location # node_idを取得 node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(
caller_location ) # ファイルパス + node_idで正確なソースコードを取得できる ast = RubyVM::AbstractSyntaxTree.parse_file( caller_location.path, keep_script_lines: true ) node = find_node_id(ast, node_id) #=> 正確な位置情報やソースコードを取得できる [node.first_lineno, node.first_column, node.last_lineno, node.last_column] node.source ιʔείʔυͷ༰ -
count = 0 TracePoint.new(:call) do count += 1 end.enable #
この1行で、内部のメソッド呼び出しは何回起こる? User.where(id: 1).load puts count #=> ??? DPVOU͍ͭ͘ʹͳΔͰ͠ΐ͏͔ -
DPVOU͍ͭ͘ʹͳΔͰ͠ΐ͏͔ count = 0 TracePoint.new(:call) do count += 1 end.enable
# この1行で、内部のメソッド呼び出しは何回起こる? User.where(id: 1).load puts count #=> 約5,000回 -
TARGET_METHODS = [ ActiveRecord::Base.method(:has_one), ActiveRecord::Base.method(:has_many), ActiveRecord::Base.method(:belongs_to), ActiveRecord::Base.method(:has_and_belongs_to_many), ].map { [_1.owner,
_1.name] }.to_set trace_point = TracePoint.new(:call) do |tp| next unless TARGET_METHODS.include?([tp.defined_class, tp_method_id]]) ... end ඞཁͳϝιουҎ֎ૣظϦλʔϯ -
TARGET_METHODS = [ ActiveRecord::Base.method(:has_one), ActiveRecord::Base.method(:has_many), ActiveRecord::Base.method(:belongs_to), ActiveRecord::Base.method(:has_and_belongs_to_many), ].map { [_1.owner,
_1.name] }.to_set trace_point = TracePoint.new(:call) do |tp| next unless TARGET_METHODS.include?([tp.defined_class, tp_method_id]]) ... end ඞཁͳϝιουҎ֎ૣظϦλʔϯ ؔ࿈ఆٛͷϝιουΛࢦఆ -
TARGET_METHODS = [ ActiveRecord::Base.method(:has_one), ActiveRecord::Base.method(:has_many), ActiveRecord::Base.method(:belongs_to), ActiveRecord::Base.method(:has_and_belongs_to_many), ].map { [_1.owner,
_1.name] }.to_set trace_point = TracePoint.new(:call) do |tp| next unless TARGET_METHODS.include?([tp.defined_class, tp_method_id]]) ... end ඞཁͳϝιουҎ֎ૣظϦλʔϯ τϨʔεର֎ૣظϦλʔϯ -
trace_point = TracePoint.new(:call) do |tp| names = tp.parameters.map { _2
} parameters = names.to_h do [_1, tp.binding.local_variable_get(_1)] end pp(parameters) end -
trace_point = TracePoint.new(:call) do |tp| names = tp.parameters.map { _2
} parameters = names.to_h do [_1, tp.binding.local_variable_get(_1)] end pp(parameters) end ύϥϝʔλ໊Λऔಘ -
trace_point = TracePoint.new(:call) do |tp| names = tp.parameters.map { _2
} parameters = names.to_h do [_1, tp.binding.local_variable_get(_1)] end pp(parameters) end Λऔಘ -
def extract_rest_parameters(*args, **options, &block) { :* => args, :** =>
options, :"..." => [args, options, block], :& => block } end trace_point = TracePoint.new(:call) do |tp| rest_names = tp.parameters.filter_map { _2 if [:*, :**, :&].include?(_2) } rest_variables = begin tp.binding.eval("extract_rest_parameters(#{rest_names.join(', ')})").slice(*rest_names) rescue SyntaxError tp.binding.eval("extract_rest_parameters(...)").slice(:"...") end pp rest_variables end trace_point.enable -
def extract_rest_parameters(*args, **options, &block) { :* => args, :** =>
options, :"..." => [args, options, block], :& => block } end trace_point = TracePoint.new(:call) do |tp| rest_names = tp.parameters.filter_map { _2 if [:*, :**, :&].include?(_2) } rest_variables = begin tp.binding.eval("extract_rest_parameters(#{rest_names.join(', ')})").slice(*rest_names) rescue SyntaxError tp.binding.eval("extract_rest_parameters(...)").slice(:"...") end pp rest_variables end trace_point.enable ແ໊ͳSFTUҾ໊ΛऔΓग़͢ -
def extract_rest_parameters(*args, **options, &block) { :* => args, :** =>
options, :"..." => [args, options, block], :& => block } end trace_point = TracePoint.new(:call) do |tp| rest_names = tp.parameters.filter_map { _2 if [:*, :**, :&].include?(_2) } rest_variables = begin tp.binding.eval("extract_rest_parameters(#{rest_names.join(', ')})").slice(*rest_names) rescue SyntaxError tp.binding.eval("extract_rest_parameters(...)").slice(:"...") end pp rest_variables end trace_point.enable FYUSBDU@SFTU@QBSBNFUFST -
def extract_rest_parameters(*args, **options, &block) { :* => args, :** =>
options, :"..." => [args, options, block], :& => block } end trace_point = TracePoint.new(:call) do |tp| rest_names = tp.parameters.filter_map { _2 if [:*, :**, :&].include?(_2) } rest_variables = begin tp.binding.eval("extract_rest_parameters(#{rest_names.join(', ')})").slice(*rest_names) rescue SyntaxError tp.binding.eval("extract_rest_parameters(...)").slice(:"...") end pp rest_variables end trace_point.enable มʹଋറ͞ΕΔ -
def extract_rest_parameters(*args, **options, &block) { :* => args, :** =>
options, :"..." => [args, options, block], :& => block } end trace_point = TracePoint.new(:call) do |tp| rest_names = tp.parameters.filter_map { _2 if [:*, :**, :&].include?(_2) } rest_variables = begin tp.binding.eval("extract_rest_parameters(#{rest_names.join(', ')})").slice(*rest_names) rescue SyntaxError tp.binding.eval("extract_rest_parameters(...)").slice(:"...") end pp rest_variables end trace_point.enable ύϥϝʔλ໊Λऔಘ -
def extract_rest_parameters(*args, **options, &block) { :* => args, :** =>
options, :"..." => [args, options, block], :& => block } end trace_point = TracePoint.new(:call) do |tp| rest_names = tp.parameters.filter_map { _2 if [:*, :**, :&].include?(_2) } rest_variables = begin tp.binding.eval("extract_rest_parameters(#{rest_names.join(', ')})").slice(*rest_names) rescue SyntaxError tp.binding.eval("extract_rest_parameters(...)").slice(:"...") end pp rest_variables end trace_point.enable ͷ߹ͪ͜Β͕ॲཧ͞ΕΔ -
·ͱΊ w 5SBDF1PJOUΛ׆༻͢Δͱϝιουݺͼग़͠ͷใΛऔಘՄೳ w Өڹൣғௐࠪஔॲཧʹ׆͔ͤΔ w ஔॲཧτϥϒϧͳ͘Ͱ͖ͨͷ͔ʁ w ʮػցతͳஔ มͳͲҰ෦ͷखಈஔʯͰ
w ςετΛॻ͍͍ͯͳ͔ͬͨՕॴͰΤϥʔ w ʮ4NBSU)3Ϟσϧ໊ʯͰݕࡧ͢ΔͱςοΫϒϩά͕ݟΕ·͢ w 5SBDF1PJOUͷ໘ന͞ɺෛ࠴ղফʹ׆͔ͤͦ͏ͳώϯτΘΓ·͔ͨ͠ʁ