Skip to content

sparsh9/rails-api-profiler

Repository files navigation

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


About

A gem to profile rails apis

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published