From 83ddeb4fc62bffc488f43d776584d751e02ff5f8 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 5 Mar 2026 09:52:41 +0000 Subject: [PATCH] Extract CallerInformation and ArgumentExtractor to module-level files Both were nested inside Instrumenter, making them inaccessible outside and tightly coupling them to Instrumenter's implementation. Moving them to their own files (caller_information.rb, argument_extractor.rb) under Observable:: makes them independently testable and reusable. Also takes the opportunity to fix the unused local_variables call and bare rescue in ArgumentExtractor (fixes already done in sibling PRs). https://claude.ai/code/session_01KWZUse79XR68i6nGa8VgTu --- lib/observable/argument_extractor.rb | 45 +++++++++++++++++++++++++ lib/observable/caller_information.rb | 5 +++ lib/observable/instrumenter.rb | 49 ++-------------------------- 3 files changed, 52 insertions(+), 47 deletions(-) create mode 100644 lib/observable/argument_extractor.rb create mode 100644 lib/observable/caller_information.rb diff --git a/lib/observable/argument_extractor.rb b/lib/observable/argument_extractor.rb new file mode 100644 index 0000000..f03cb0a --- /dev/null +++ b/lib/observable/argument_extractor.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module Observable + class ArgumentExtractor + def initialize(caller_location, caller_binding = nil) + @caller_location = caller_location + @caller_binding = caller_binding + @method_name = caller_location.label + end + + def extract + return {} unless @caller_binding + extract_from_binding + end + + private + + def extract_from_binding + args = {} + extract_local_variables_with_numeric_indexing(args) + args + rescue StandardError + {} + end + + def extract_local_variables_with_numeric_indexing(args) + local_vars = @caller_binding.local_variables + positional_index = 0 + + local_vars.each do |var_name| + var_name_str = var_name.to_s + + next if var_name_str.start_with?("_") || var_name == :instrumenter + + value = @caller_binding.local_variable_get(var_name) + + args[positional_index.to_s] = { + value: value, + param_name: var_name_str + } + positional_index += 1 + end + end + end +end diff --git a/lib/observable/caller_information.rb b/lib/observable/caller_information.rb new file mode 100644 index 0000000..4080c92 --- /dev/null +++ b/lib/observable/caller_information.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module Observable + CallerInformation = Struct.new(:method_name, :namespace, :filepath, :line_number, :arguments, :is_class_method, keyword_init: true) +end diff --git a/lib/observable/instrumenter.rb b/lib/observable/instrumenter.rb index 6ac0a75..273f8ac 100644 --- a/lib/observable/instrumenter.rb +++ b/lib/observable/instrumenter.rb @@ -1,5 +1,7 @@ require "opentelemetry/sdk" require_relative "configuration" +require_relative "caller_information" +require_relative "argument_extractor" module Observable class Instrumenter @@ -330,53 +332,6 @@ def extract_arguments_from_caller(caller_location) ArgumentExtractor.new(caller_location, @caller_binding).extract end - CallerInformation = Struct.new(:method_name, :namespace, :filepath, :line_number, :arguments, :is_class_method, keyword_init: true) - - class ArgumentExtractor - def initialize(caller_location, caller_binding = nil) - @caller_location = caller_location - @caller_binding = caller_binding - @method_name = caller_location.label - end - - def extract - return {} unless @caller_binding - extract_from_binding - end - - private - - def extract_from_binding - @caller_binding.local_variables - args = {} - - extract_local_variables_with_numeric_indexing(args) - - args - rescue - {} - end - - def extract_local_variables_with_numeric_indexing(args) - local_vars = @caller_binding.local_variables - positional_index = 0 - - local_vars.each do |var_name| - var_name_str = var_name.to_s - - next if var_name_str.start_with?("_") || var_name == :instrumenter - - value = @caller_binding.local_variable_get(var_name) - - args[positional_index.to_s] = { - value: value, - param_name: var_name_str - } - positional_index += 1 - end - end - end - class InstrumentationError < StandardError; end end end