Class: AppMap::Event::MethodEvent

Inherits:
MethodEventStruct show all
Extended by:
ValueInspector
Defined in:
lib/appmap/event.rb

Constant Summary collapse

MAX_ARRAY_ENUMERATION =
10
MAX_HASH_ENUMERATION =
10
MAX_STRING_LENGTH =
100

Constants included from ValueInspector

ValueInspector::MAX_ARRAY_ELEMENTS, ValueInspector::MAX_DEPTH

Instance Attribute Summary

Attributes inherited from MethodEventStruct

#event, #id, #thread_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValueInspector

best_class_name, detect_schema, detect_size

Class Method Details

.add_schema(param, value) ⇒ Object



71
72
73
# File 'lib/appmap/event.rb', line 71

def add_schema(param, value)
  ValueInspector.detect_schema(value, type_info: param)
end

.add_size(param, value) ⇒ Object



66
67
68
69
# File 'lib/appmap/event.rb', line 66

def add_size(param, value)
  size = ValueInspector.detect_size(value)
  param[:size] = size if size
end

.build_from_invocation(event_type, event:) ⇒ Object



31
32
33
34
35
# File 'lib/appmap/event.rb', line 31

def build_from_invocation(event_type, event:)
  event.id = AppMap::Event.next_id_counter
  event.event = event_type
  event.thread_id = Thread.current.object_id
end

.custom_display_string(value) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/appmap/event.rb', line 79

def custom_display_string(value)
  case value
  when NilClass, TrueClass, FalseClass, Numeric, Time, Date
    [ value.to_s, true ]
  when Symbol
    [ ":#{value}", true ]
  when String
    result = value[0...MAX_STRING_LENGTH].encode('utf-8', invalid: :replace, undef: :replace, replace: '_')
    result << " (...#{value.length - MAX_STRING_LENGTH} more characters)" if value.length > MAX_STRING_LENGTH
    [ result, true ]
  when Array
    result = value[0...MAX_ARRAY_ENUMERATION].map{|v| display_string(v)}.join(', ')
    result << " (...#{value.length - MAX_ARRAY_ENUMERATION} more items)" if value.length > MAX_ARRAY_ENUMERATION
    [ [ '[', result, ']' ].join, true ]
  when Hash
    result = value.keys[0...MAX_HASH_ENUMERATION].map{|key| "#{display_string(key)}=>#{display_string(value[key])}"}.join(', ')
    result << " (...#{value.size - MAX_HASH_ENUMERATION} more entries)" if value.size > MAX_HASH_ENUMERATION
    [ [ '{', result, '}' ].join, true ]
  when File
    [ "#{value.class}[path=#{value.path}]", true ]
  when Net::HTTP
    [ "#{value.class}[#{value.address}:#{value.port}]", true ]
  when Net::HTTPGenericRequest
    [ "#{value.class}[#{value.method} #{value.path}]", true ]
  end
rescue StandardError
  nil
end

.default_display_string(value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/appmap/event.rb', line 108

def default_display_string(value)
  return nil if ENV['APPMAP_OBJECT_STRING'] == 'false'

  last_resort_string = lambda do
    warn "AppMap encountered an error inspecting a #{value.class.name}: #{$!.message}"
    '*Error inspecting variable*'
  end

  begin
    value.to_s
  rescue NoMethodError
    begin
      value.inspect
    rescue
      last_resort_string.call
    end
  rescue WeakRef::RefError
    nil
  rescue
    last_resort_string.call
  end
end

.display_string(value) ⇒ Object

Gets a display string for a value. This is not meant to be a machine deserializable value.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/appmap/event.rb', line 38

def display_string(value)
  return nil if value.equal?(nil)

  # With setting APPMAP_PROFILE_DISPLAY_STRING, stringifying this class is shown to take 9 seconds(!) of a 17 second test run.
  return nil if best_class_name(value) == 'ActiveSupport::Callbacks::Filters::Environment'

  if @times.nil? && ENV['APPMAP_PROFILE_DISPLAY_STRING'] == 'true'
    @times = Hash.new {|memo,key| memo[key] = 0}
    Thread.new do
      sleep 0.5
      while true
        warn @times.to_a.sort{|a,b| b[1] <=> a[1]}[0..9].join("\n")
        sleep 3
      end
    end
  end

  start = Time.now
  value_string, final = custom_display_string(value) || default_display_string(value)

  if @times
    elapsed = Time.now - start
    @times[best_class_name(value)] += elapsed
  end

  final ? value_string : encode_display_string(value_string)
end

.encode_display_string(value) ⇒ Object



75
76
77
# File 'lib/appmap/event.rb', line 75

def encode_display_string(value)
  (value||'')[0...MAX_STRING_LENGTH].encode('utf-8', invalid: :replace, undef: :replace, replace: '_')
end

Instance Method Details

#ready?Boolean

An event may be partially constructed, and then completed at a later time. When the event is only partially constructed, it’s not ready for serialization to the AppMap file.

Returns:

  • (Boolean)

    false until the event is fully constructed and available.



136
137
138
# File 'lib/appmap/event.rb', line 136

def ready?
  true
end