Skip to content
Open
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
45 changes: 45 additions & 0 deletions lib/observable/argument_extractor.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lib/observable/caller_information.rb
Original file line number Diff line number Diff line change
@@ -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
49 changes: 2 additions & 47 deletions lib/observable/instrumenter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "opentelemetry/sdk"
require_relative "configuration"
require_relative "caller_information"
require_relative "argument_extractor"

module Observable
class Instrumenter
Expand Down Expand Up @@ -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