Minimal APM for Rails apps. Automatically measures each controller action's total time, tracks SQL queries, monitors view rendering performance, tracks memory usage and detects leaks, monitors background jobs, and posts metrics to a remote endpoint with an API key read from your app's settings/credentials/env.
To use the gem you need to have a free account with DeadBro - Rails APM
Add to your Gemfile:
gem "apm_bro", git: "https://github.com/rubydevro/apm_bro.git"By default, if Rails is present, ApmBro auto-subscribes to process_action.action_controller and posts metrics asynchronously.
You can set via an initializer:
ApmBro.configure do |cfg|
cfg.api_key = ENV["APM_BRO_API_KEY"]
cfg.enabled = true
endApmBro can track the email of the user making requests, which is useful for debugging user-specific issues and understanding user behavior patterns.
Enable user email tracking in your Rails configuration:
# In config/application.rb or environments/*.rb
ApmBro.configure do |config|
config.user_email_tracking_enabled = true
endBy default, ApmBro will try to extract user email from these sources (in order of priority):
current_user.email- Most common in Rails apps with authentication- Request parameters -
user_emailoremailin params - HTTP headers -
X-User-EmailorHTTP_X_USER_EMAIL - Session data -
user_emailin session
In progress
- User email tracking is disabled by default for privacy
- Only enable when necessary for debugging or analytics
- Consider your data privacy requirements and regulations
- The email is included in all request payloads sent to our APM endpoint
ApmBro supports configurable request sampling to reduce the volume of metrics sent to your APM endpoint, which is useful for high-traffic applications.
Set the sample rate as a percentage (1-100):
# Track 50% of requests
ApmBro.configure do |config|
config.sample_rate = 50
end
# Track 10% of requests (useful for high-traffic apps)
ApmBro.configure do |config|
config.sample_rate = 10
end
# Track all requests (default)
ApmBro.configure do |config|
config.sample_rate = 100
end- Random Sampling: Each request has a random chance of being tracked based on the sample rate
- Consistent Per-Request: The sampling decision is made once per request and applies to all metrics for that request
- Debug Logging: Skipped requests do not count towards the montly limit
- Error Tracking: Errors are still tracked regardless of sampling
- High-Traffic Applications: Reduce APM data volume and costs
- Development/Staging: Sample fewer requests to reduce noise
- Performance Testing: Track a subset of requests during load testing
- Cost Optimization: Balance monitoring coverage with data costs
You can exclude specific controllers and jobs from APM tracking.
ApmBro.configure do |config|
config.excluded_controllers = [
"HealthChecksController",
"Admin::*" # wildcard supported
]
config.excluded_controller_actions = [
"UsersController#show",
"Admin::ReportsController#index",
"Admin::*#*" # wildcard supported for controller and action
]
config.excluded_jobs = [
"ActiveStorage::AnalyzeJob",
"Admin::*"
]
endNotes:
- Wildcards
*are supported for controller and action (e.g.,Admin::*#*). - Matching is done against full names like
UsersController,Admin::ReportsController#index,MyJob.
ApmBro automatically tracks SQL queries executed during each request and job. Each request will include a sql_queries array containing:
sql- The SQL query (always sanitized)name- Query name (e.g., "User Load", "User Update")duration_ms- Query execution time in millisecondscached- Whether the query was cachedconnection_id- Database connection IDtrace- Call stack showing where the query was executed
ApmBro automatically tracks view rendering performance for each request. This includes:
- Individual view events: Templates, partials, and collections rendered
- Performance metrics: Rendering times for each view component
- Cache analysis: Cache hit rates for partials and collections
- Slow view detection: Identification of the slowest rendering views
- Frequency analysis: Most frequently rendered views
ApmBro automatically tracks memory usage and detects memory leaks with minimal performance impact. This includes:
By default, ApmBro uses lightweight memory tracking that has minimal performance impact:
- Memory Usage Monitoring: Track memory consumption per request (using GC stats, not system calls)
- Memory Leak Detection: Detect growing memory patterns over time
- GC Efficiency Analysis: Monitor garbage collection effectiveness
- Zero Allocation Tracking: No object allocation tracking by default (can be enabled)
# In your Rails configuration
ApmBro.configure do |config|
config.memory_tracking_enabled = true # Enable lightweight memory tracking (default: true)
config.allocation_tracking_enabled = false # Enable detailed allocation tracking (default: false)
# Sampling configuration
config.sample_rate = 100 # Percentage of requests to track (1-100, default: 100)
endPerformance Impact:
- Lightweight mode: ~0.1ms overhead per request
- Allocation tracking: ~2-5ms overhead per request (only enable when needed)
ApmBro automatically tracks ActiveJob background jobs when ActiveJob is available. Each job execution is tracked with:
job_class- The job class name (e.g., "UserMailer::WelcomeEmail")job_id- Unique job identifierqueue_name- The queue the job was processed fromarguments- Sanitized job arguments (sensitive data filtered)duration_ms- Job execution time in millisecondsstatus- "completed" or "failed"sql_queries- Array of SQL queries executed during the jobexception_class- Exception class name (for failed jobs)message- Exception message (for failed jobs)backtrace- Exception backtrace (for failed jobs)
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release.
Bug reports and pull requests are welcome on GitHub at https://github.com/rubydevro/apm_bro.