Skip to content

Commit 3f5daa8

Browse files
committed
feat: APPMAP_PROFILE_DISPLAY_STRING and APPMAP_OBJECT_STRING
APPMAP_PROFILE_DISPLAY_STRING=true enables periodic printouts of top 10 most expensive classes to stringify APPMAP_OBJECT_STRING=false disables stringification of generic objects
1 parent f30ed9f commit 3f5daa8

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/appmap/event.rb

+32-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,31 @@ def build_from_invocation(event_type, event:)
2929

3030
# Gets a display string for a value. This is not meant to be a machine deserializable value.
3131
def display_string(value)
32-
return nil unless value
32+
return nil if value.equal?(nil)
3333

34+
# With setting APPMAP_PROFILE_DISPLAY_STRING, stringifying this class is shown to take 9 seconds(!) of a 17 second test run.
35+
return nil if best_class_name(value) == 'ActiveSupport::Callbacks::Filters::Environment'
36+
37+
if @times.nil? && ENV['APPMAP_PROFILE_DISPLAY_STRING'] == 'true'
38+
@times = Hash.new {|memo,key| memo[key] = 0}
39+
Thread.new do
40+
sleep 0.5
41+
while true
42+
warn @times.to_a.sort{|a,b| b[1] <=> a[1]}[0..9].join("\n")
43+
sleep 3
44+
end
45+
end
46+
end
47+
48+
start = Time.now
3449
value_string = custom_display_string(value) || default_display_string(value)
3550

36-
(value_string||'')[0...LIMIT].encode('utf-8', invalid: :replace, undef: :replace, replace: '_')
51+
if @times
52+
elapsed = Time.now - start
53+
@times[best_class_name(value)] += elapsed
54+
end
55+
56+
encode_dislay_string(value_string)
3757
end
3858

3959
def object_properties(hash_like)
@@ -57,8 +77,16 @@ def best_class_name(value)
5777
value_cls.name
5878
end
5979

80+
def encode_dislay_string(value)
81+
(value||'')[0...LIMIT].encode('utf-8', invalid: :replace, undef: :replace, replace: '_')
82+
end
83+
6084
def custom_display_string(value)
6185
case value
86+
when NilClass, TrueClass, FalseClass, Numeric, Time, Date
87+
value.to_s
88+
when String
89+
value[0...LIMIT].encode('utf-8', invalid: :replace, undef: :replace, replace: '_')
6290
when File
6391
"#{value.class}[path=#{value.path}]"
6492
when Net::HTTP
@@ -71,6 +99,8 @@ def custom_display_string(value)
7199
end
72100

73101
def default_display_string(value)
102+
return nil if ENV['APPMAP_OBJECT_STRING'] == 'false'
103+
74104
last_resort_string = lambda do
75105
warn "AppMap encountered an error inspecting a #{value.class.name}: #{$!.message}"
76106
'*Error inspecting variable*'

spec/hook_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ def secure_compare(a, b)
970970
tz = ENV['TZ']
971971
ENV['TZ'] = 'UTC'
972972
Timecop.freeze(Time.utc('2020-01-01')) do
973-
expect(Time).to receive(:now).exactly(3).times.and_call_original
973+
expect(Time).to receive(:now).at_least(3).times.and_call_original
974974
expect(InstanceMethod.new.say_the_time).to be
975975
end
976976
ensure

0 commit comments

Comments
 (0)