Class: RequireProfiler::Plugins::RailsPlugin

Inherits:
Base
  • Object
show all
Defined in:
lib/require_profiler/plugins/rails_plugin.rb

Overview

Track Rails initialization: railtie initializers, to_prepare callbacks, and load hooks

Defined Under Namespace

Modules: InitializerPatch, LoadHookPatch, ToPreparePatch

Class Attribute Summary collapse

Attributes inherited from Base

#reporter

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from RequireProfiler::Plugins::Base

Class Attribute Details

.reporterObject

Returns the value of attribute reporter.



34
35
36
# File 'lib/require_profiler/plugins/rails_plugin.rb', line 34

def reporter
  @reporter
end

Class Method Details

.label(kind, name, block) ⇒ Object



46
47
48
# File 'lib/require_profiler/plugins/rails_plugin.rb', line 46

def label(kind, name, block)
  ["rails:#{kind}", name, block&.source_location&.join(":")].compact.join(":")
end

.track(kind, name, block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/require_profiler/plugins/rails_plugin.rb', line 36

def track(kind, name, block)
  path = label(kind, name, block)
  reporter.handle_event(Reporter::Event.new(type: :start, kind: :rails, path:))
  start = Time.now
  yield
ensure
  time = Time.now - start
  reporter.handle_event(Reporter::Event.new(type: :end, path:, time:))
end

Instance Method Details

#activate!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/require_profiler/plugins/rails_plugin.rb', line 51

def activate!
  RailsPlugin.reporter = reporter

  Patcher.on_load("Rails::Initializable::Initializer") do
    ::Rails::Initializable::Initializer.prepend(InitializerPatch)
  end
  Patcher.on_load("ActiveSupport::Reloader") do
    ::ActiveSupport::Reloader.singleton_class.prepend(ToPreparePatch)
  end
  Patcher.on_load("ActiveSupport::LazyLoadHooks") do
    ::ActiveSupport.singleton_class.prepend(LoadHookPatch)
  end
end