charkost / prosopite

:mag: Rails N+1 queries auto-detection with zero false positives / false negatives
Apache License 2.0
1.53k stars 46 forks source link

Allow cleaning backtraces used to identify N+1 queries #79

Open tf opened 9 months ago

tf commented 9 months ago

Some gems cache missing methods after the first call. This can cause Prosopite to miss duplicate queries. For example, the N+1 query for author in the following builder template goes unnoticed if there are two posts since xml.post is a missing method that gets cached for the second call (see [1]).


@posts.each do |post|
  xml.post do
    xml.author post.author.name
  end
end

Add location_backtrace_cleaner config option to modify backtraces that are used to identify queries. This allows to either silence problematic lines or even just use the backtrace inside the application itself:

Prosopite.location_backtrace_cleaner = Rails.backtrace_silencer

[1] https://github.com/jimweirich/builder/blob/c80100f8205b2e918dbff605682b01ab0fabb866/lib/builder/xmlbase.rb#L91

tf commented 9 months ago

Alternatively, one could always create associations with at least three records in tests. This bloats test setup, though, and is easy to forget. Only using application backtraces means erring on the side of caution.

Happy to take suggestions regarding the name of the option. Having both a backtrace_silencer and a location_backtrace_silencer option might not be ideal.

One alternative I considered is to instead add an option to allow replacing the whole location_key generation. This would also let me plug in my own backtrace silencer before creating the hash. One would need to replicate the SHA256.hexdigest call in one's config then, though. The suggestion solution seemed more specific to the problem.

I also considered adding an option like use_app_backtrace_only which causes Rails.backtrace_silencer to be applied directly. This would not work in Rails engine tests, tough, since there Rails.backtrace_silencer removes everything but dummy app locations.

tf commented 9 months ago

Also happy to add a README section if this is not considered too much of an edge case.