The project is in a healthy, maintained state
Rails API Profiler is a lightweight, middleware-based tool for profiling API requests in Rails applications. It measures request duration, database query count, and database execution time with zero code changes, supporting Rails 5 and above.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 5.0, < 9.0
 Project Readme

Perfect πŸ‘ Here’s a clean, professional, RubyGems-ready README that fits the quality of the gem you’re building. You can copy-paste this directly into README.md.


Rails API Profiler

Rails API Profiler is a lightweight, middleware-based performance profiling tool for Rails API applications. It provides request-level insights such as response time, database query count, and database execution time β€” with zero changes to your controllers or business logic.

Designed to be simple, fast, and production-safe, it supports Rails 5 and above.


✨ Features

  • πŸš€ Measures API request duration
  • πŸ—„οΈ Tracks ActiveRecord query count
  • ⏱️ Tracks total database execution time
  • βš™οΈ Middleware-based (no controller code required)
  • πŸ”• Can be enabled/disabled via configuration
  • πŸ§ͺ Safe for development, staging, and debugging
  • 🧩 Works with Rails 5+

πŸ“¦ Installation

Add the gem to your Gemfile:

gem "rails_api_profiler", group: [:development, :staging]

Then install:

bundle install

That’s it β€” no further setup required.


βš™οΈ Configuration

You can optionally configure the profiler in an initializer:

# config/initializers/rails_api_profiler.rb

RailsApiProfiler.configure do |config|
  config.enabled = true
  config.slow_request_threshold = 500 # milliseconds
end

Available Options

Option Description Default
enabled Enable or disable profiling true
slow_request_threshold Threshold for slow request warnings (ms) 500
logger Custom logger instance Rails.logger

πŸ“Š Example Log Output

[RailsApiProfiler] {:method=>"GET",
:path=>"/api/v1/users",
:status=>200,
:duration_ms=>183.21,
:db_queries=>7,
:db_time_ms=>41.8}

For slow requests:

[RailsApiProfiler] SLOW REQUEST {:method=>"POST",
:path=>"/api/v1/orders",
:status=>201,
:duration_ms=>812.44,
:db_queries=>18,
:db_time_ms=>220.3}

🧠 How It Works

  • Uses a Rack middleware to measure request duration
  • Hooks into ActiveSupport::Notifications to track SQL queries
  • Stores per-request stats using thread-local storage
  • Automatically attaches via a Rails Railtie

No monkey-patching. No controller hooks. No overhead outside requests.


πŸ§ͺ Supported Versions

  • Rails: 5.0+
  • Ruby: 2.4+

🚧 When Should I Use This?

This gem is ideal when you want to:

  • Identify slow API endpoints
  • Detect excessive database queries
  • Debug performance regressions
  • Profile APIs in development or staging
  • Keep instrumentation lightweight

πŸ›‘ When NOT to Use This

  • As a full APM replacement (Datadog, New Relic, etc.)
  • For deep flamegraph analysis
  • For long-running background jobs

This gem focuses on request-level clarity, not heavy analytics.


πŸ›£οΈ Roadmap

Planned improvements:

  • JSON / structured log formatter
  • N+1 query detection
  • Prometheus exporter
  • Memory usage tracking
  • Optional endpoint filtering

🀝 Contributing

Contributions are welcome!

  1. Fork the repository
  2. Create a feature branch
  3. Add tests
  4. Submit a pull request

Please ensure all specs pass before submitting.


πŸ“„ License

This project is licensed under the MIT License.


πŸ‘€ Author

Sparsh Garg GitHub: https://github.com/sparsh9