ViewComponent / view_component

A framework for building reusable, testable & encapsulated view components in Ruby on Rails.
https://viewcomponent.org
MIT License
3.17k stars 404 forks source link

Support for rack-mini-profiler #1982

Closed htcarr3 closed 3 months ago

htcarr3 commented 4 months ago

Feature request

Currently only views and partials are broken out in the speed badge report that gets created by rack-mini-profiler. I assume this requires some tie-in on instrumentation. My apologies if this is already supported, I just couldn't find any examples or documentation for it, and maybe this is just a documentation issue (or maybe this is some configuration that makes sense to live on rack-mini-profiler)!

Motivation

rack-mini-profiler seems to be the ubiquitous first stop for analyzing performance in rails/rack apps. Since ViewComponent (essentially) replaces partials in many instances, it seems natural to expect the rendering timings to show up there as well. Thank you for your consideration!

Update/Example

After diving deeper into the instrumentation documentation for ViewComponent, and looking at the source code for Rack::MiniProfilerRails (which is essentially the official railtie), something like this seems to work:

# somewhere in the boot path for a rails app, like development.rb

Rack::MiniProfilerRails.subscribe('render.view_component') do |_name, start, finish, _id, payload|
  Rack::MiniProfilerRails.render_notification_handler(
    Rack::MiniProfilerRails.shorten_identifier(payload[:identifier]),
    finish,
    start
  )
end

If this seems correct, maybe a small addition to the instrumentation docs could be helpful? I'm happy to put together a PR.

htcarr3 commented 4 months ago

Related: #1490

joelhawksley commented 3 months ago

@htcarr3 sure, please write a PR!

htcarr3 commented 3 months ago

@htcarr3 sure, please write a PR!

I will put together a pr soon, thank you!

htcarr3 commented 3 months ago

@joelhawksley, there is certainly no rush on this, but I do have a pr up whenever you find some time to review

joelhawksley commented 3 months ago

@htcarr3 thanks for the PR! Just merged it ❤