Module: OpenTrace::LocalVars

Defined in:
lib/opentrace/local_vars.rb

Constant Summary collapse

MAX_VARS =
10
MAX_VALUE_LENGTH =
500

Class Method Summary collapse

Class Method Details

.capture(binding_obj) ⇒ Object

Capture local variables from an explicit binding. Called by the user in their rescue blocks:

rescue => e
  OpenTrace.capture_binding(e, binding)
  raise
end

Returns: Array of { name:, value:, type: } or nil



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/opentrace/local_vars.rb', line 19

def capture(binding_obj)
  return nil unless binding_obj.is_a?(Binding)

  vars = binding_obj.local_variables.first(MAX_VARS)
  vars.filter_map do |name|
    # Skip internal variables (_, _1, etc.)
    next if name.to_s.start_with?("_")

    value = binding_obj.local_variable_get(name)
    {
      name: name.to_s,
      value: safe_inspect(value),
      type: value.class.name
    }
  end
rescue StandardError
  nil
end

.safe_inspect(value) ⇒ Object



38
39
40
41
42
43
# File 'lib/opentrace/local_vars.rb', line 38

def safe_inspect(value)
  str = value.inspect
  str.length > MAX_VALUE_LENGTH ? str[0, MAX_VALUE_LENGTH] + "..." : str
rescue StandardError
  "#<uninspectable>"
end