Skip to content

Commit

Permalink
Instrument existing klasses at initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
aantix committed May 15, 2023
1 parent 44fc729 commit 1b1eb60
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/callstacking/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class Engine < ::Rails::Engine
config.after_initialize do
Logger.log "Call Stacking loading (#{Callstacking::Rails::Env.environment})"

Logger.log("English defined? #{Object.const_defined?('English')}")

spans[Thread.current.object_id]||=Spans.new
instrumenter.add_span(spans[Thread.current.object_id])

@@loader = Callstacking::Rails::Loader.new(instrumenter, excluded: settings.excluded + EXCLUDED_TEST_CLASSES)
@@loader.instrument_existing

loader.on_load
# loader.reset!
end
Expand Down
27 changes: 19 additions & 8 deletions lib/callstacking/rails/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def initialize(instrumenter, excluded: [])
@klasses = Set.new
end

def instrument_existing
objs = []
ObjectSpace.each_object(Module){|ob| objs << [ob, ((Object.const_source_location(ob.to_s) rescue nil))].flatten}
objs.each{|o| filter_klass(o.first, o.last)}
end

def on_load
trace = TracePoint.new(:end) do |tp|
klass = tp.self
Expand All @@ -19,14 +25,7 @@ def on_load
Logger.log("Callstacking::Rails::Loader.on_load #{klass} #{path}")
Logger.log("English defined? #{Object.const_defined?('English')}")

excluded_klass = excluded.any? { |ex| path =~ /#{ex}/ }

if path =~ /#{::Rails.root.to_s}/ &&
!klasses.include?(klass) &&
!excluded_klass
instrumenter.instrument_klass(klass)
klasses << klass
end
filter_klass(klass, path)
end

trace.enable
Expand All @@ -36,6 +35,18 @@ def reset!
instrumenter.instrument_method(ActionView::PartialRenderer, :render, application_level: false)
instrumenter.instrument_method(ActionView::TemplateRenderer, :render, application_level: false)
end

private
def filter_klass(klass, path)
excluded_klass = excluded.any? { |ex| path =~ /#{ex}/ }

if path =~ /#{::Rails.root.to_s}/ &&
!klasses.include?(klass) &&
!excluded_klass
instrumenter.instrument_klass(klass)
klasses << klass
end
end
end
end
end

0 comments on commit 1b1eb60

Please sign in to comment.