hschne / rails-mini-profiler

Performance profiling for Rails, made simple 🦔
MIT License
410 stars 22 forks source link

Add plugin system to allow users to add their own tracing #96

Open hschne opened 2 years ago

hschne commented 2 years ago

Right now, which traces are profiled, and how they are rendered, is hardcoded.

Add some sort of plugin architecture that allows users to add custom profilers

topherfangio commented 3 weeks ago

On this note, it would be great if we could profile ViewComponents! I would be interested in taking a stab at this PR if you could provide some guidance on your thoughts for a plugin system.

hschne commented 2 weeks ago

Hey @topherfangio, that's a great idea. I'd love to see some support for ViewComponents (or other view libraries such as Phlex), and I'll take any help I can get :grin:

Now, as you've probably seen I haven't actively worked on RMP for a while, but this seems like a good opportunity. As to guidelines, I'll do my best to recollect what I was thinking when I created this issue originally.


'Pluginsin RMP are basically whatTracersare now. Users can (theoretically even now) create newTracerclasses and register them withRegistry(see alsoConfigurationfor how built-in tracers are registered). Tracers specify which ActiveSupport events RMP subscribes to and which data from those events gets stored in the database. They also specify aTracePresenterclass that governs how the event will be rendered in the UI (see, e.g.,SequelTracePresenter`).

So, in theory (I haven't tested any of this) you could already create a plugin with the existing code by adding something like this to your app:

class CustomTracer < RailsMiniProfiler::Tracers::Tracer 
   class << self
      def subscribes_to
        `render.viewcomponent`
      end

      def presents
        ViewComponentTracePresenter
      end
   end
end

class ViewComponentTracePresenter < RailsMiniProfiler::Presenters::TracePresenter
  def label 
   'ViewComponentTrace'
   end
end

# RMP Config
RailsMiniProfiler.configure do |config|
  # Customize when Rails Mini Profiler should run
  config.tracers = %i[controller instantiation sequel view rmp view_component] 
end 

Luckily, ViewComponent already provides instrumentation; for other libraries, RMP may have to add instrumentation itself.


Apart from checking if what I wrote above actually works there are two things that need doing (that I can think of on top of my head):

  1. The UI allows filtering traces by type, and there currently is no mechanism to add new tracers/events there (see _trace_list_header.erb). Shouldn't bee to rough adding this though.
            <% trace_names = %w[process_action.action_controller sql.active_record instantiation.active_record render_template.action_view render_partial.action_view] %>
  2. The whole interface of Tracers and registries and stuff isn't user friendly I feel. Might require some naming changes, even if its just aliasing some classes. I'm very open to suggestions! :smile:

Let me know your thoughts! I think it's seriously awesome if you were to take a stab at this, be happy to support in any way I can :bow:

topherfangio commented 5 hours ago

@hschne Thanks! I've been on vacation but will take a look at this this week and see how far I can get!