I had been thinking about building a Ruby gem for a long time, but like many developers, I kept postponing it.
Eventually, I decided to stop overthinking and just ship.
That decision led to my first Ruby gem: rails_api_profiler.
β The Problem
While working on Rails API applications, I often wanted quick answers to simple questions:
- Which API endpoints are slow?
- How many database queries does a request trigger?
- Is the bottleneck in application code or the database?
Full APM tools are powerful, but sometimes you just need lightweight, local insight.
π§© What the Gem Does
rails_api_profiler is a middleware-based performance profiler for Rails APIs.
It logs, per request:
- Request duration
- ActiveRecord query count
- Total database execution time
No controller changes. No monkey patching. Minimal setup.
π Example Output
[RailsApiProfiler] {
:method=>"GET",
:path=>"/api/v1/users",
:status=>200,
:duration_ms=>183.21,
:db_queries=>7,
:db_time_ms=>41.8
}
βοΈ How It Works (Briefly)
Under the hood, the gem uses:
- Rack middleware to measure request time
- ActiveSupport::Notifications to track SQL queries
- Thread-local storage for request-scoped stats
- A Railtie for automatic Rails integration
π§ What I Learned
Building this gem helped me understand:
- Rails middleware and instrumentation
- How to structure Rails-compatible gems
- Why shipping early beats waiting for perfection
π Try It Out
- RubyGems: https://rubygems.org/gems/rails_api_profiler
- GitHub: https://github.com/sparsh9/rails-api-profiler
Feedback and contributions are welcome π
Top comments (0)