Module: OpenTrace::LocalVars

Defined in:
lib/opentrace/local_vars.rb

Constant Summary collapse

MAX_VARS =
10
MAX_VALUE_LENGTH =
500
SENSITIVE_PATTERNS =
%w[
  password passwd secret token api_key apikey
  authorization auth_token access_token refresh_token
  credit_card card_number cvv ssn private_key
  session_id cookie credential
].freeze

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/opentrace/local_vars.rb', line 26

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?("_")

    name_s = name.to_s.downcase
    if sensitive_name?(name_s)
      { name: name.to_s, value: "[FILTERED]", type: "filtered" }
    else
      value = binding_obj.local_variable_get(name)
      { name: name.to_s, value: safe_inspect(value), type: value.class.name }
    end
  end
rescue StandardError
  nil
end

.safe_inspect(value) ⇒ Object



50
51
52
53
54
55
# File 'lib/opentrace/local_vars.rb', line 50

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

.sensitive_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/opentrace/local_vars.rb', line 46

def sensitive_name?(name)
  SENSITIVE_PATTERNS.any? { |pattern| name.include?(pattern) }
end