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 installThatβ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
endAvailable 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!
- Fork the repository
- Create a feature branch
- Add tests
- 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