DEV Community

Sparsh Garg
Sparsh Garg

Posted on

I Built and Published My First Ruby Gem: Rails API Profiler πŸš€

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
}
Enter fullscreen mode Exit fullscreen mode

βš™οΈ 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

Feedback and contributions are welcome πŸ™Œ

Top comments (0)