My main idea is to count the number of ActiveRecord queries for every API hit in rails. I was looking into ActiveSupport instrumentation API. And it seems it already provides a couple of useful data.
view_runtimedb_runtime
I also found a couple of gems which would count the number of queries. But they add query counts in the log data.
- https://github.com/rubysamurai/query_count
- https://github.com/comboy/sql_queries_count
- https://github.com/makandra/query_diet
Ex: Below is a sample log line when I used the query_count gem.
Completed 200 OK in 140ms (Views: 12.7ms | ActiveRecord: 54.4ms | SQL Queries: 36 (0 cached) | Allocations: 20449)
But instead of a log line, is it possible to expose query_count via ActiveSupport events, maybe as part of the payload of process_action.action_controller?
The easiest way to do rails endpoint instrumentation, based on my experience is to use rails-panel.
It shows how long the rendering took, how long the query took and how many queries were executed.