Auto-instrumentation
The MiniAPM gem automatically instruments popular Ruby libraries using ActiveSupport::Notifications and targeted monkey-patching.
How It Works
Section titled “How It Works”When Rails boots, MiniAPM’s Railtie:
- Inserts middleware into the request stack
- Subscribes to ActiveSupport::Notifications events
- Applies instrumentation to detected libraries
- Creates spans automatically with contextual attributes
Rails Instrumentation
Section titled “Rails Instrumentation”ActionController
Section titled “ActionController”Automatically traced:
- Request method and path
- Controller and action names
- Response status
- Total request duration
Captured attributes:
http.method- GET, POST, etc.http.route- Controller#actionhttp.status_code- Response statushttp.request_id- Rails request ID
ActionView
Section titled “ActionView”Automatically traced:
- Template rendering
- Partial rendering
- Layout rendering
Captured attributes:
view.template- Template pathview.layout- Layout name
ActiveRecord
Section titled “ActiveRecord”Automatically traced:
- All SQL queries
- Query execution time
- Database operations
Captured attributes:
db.system- Database type (postgresql, mysql, sqlite)db.statement- SQL query (sanitized)db.operation- SELECT, INSERT, UPDATE, DELETE
Enable full SQL logging:
config.instrument :activerecord, log_sql: trueBackground Jobs
Section titled “Background Jobs”ActiveJob
Section titled “ActiveJob”Automatically traced:
- Job execution
- Queue time
- Job failures
Captured attributes:
job.class- Job class namejob.queue- Queue namejob.id- Job ID
Sidekiq
Section titled “Sidekiq”Automatically traced via middleware:
- Job processing
- Retry attempts
- Failures
Captured attributes:
sidekiq.class- Worker classsidekiq.queue- Queue namesidekiq.jid- Job IDsidekiq.retry_count- Retry attempt
SolidQueue
Section titled “SolidQueue”Automatically traced:
- Job claiming
- Job execution
- Concurrency controls
HTTP Clients
Section titled “HTTP Clients”Net::HTTP
Section titled “Net::HTTP”Automatically traced:
- All HTTP requests
- Request/response details
- Timing
Captured attributes:
http.method- Request methodhttp.url- Request URLhttp.status_code- Response status
W3C Trace Context headers are automatically injected.
Faraday
Section titled “Faraday”Instrumented via middleware:
# Automatically added when Faraday is detectedconn = Faraday.new do |f| # MiniAPM middleware is auto-insertedendHTTParty
Section titled “HTTParty”Automatically traced for all requests.
redis-client (modern)
Section titled “redis-client (modern)”Instrumented via middleware:
Captured attributes:
db.system- “redis”db.operation- Command namenet.peer.name- Redis host
redis gem (legacy)
Section titled “redis gem (legacy)”Instrumented via monkey-patching.
Search Engines
Section titled “Search Engines”Elasticsearch / OpenSearch
Section titled “Elasticsearch / OpenSearch”Automatically traced:
- Search queries
- Index operations
- Bulk operations
Searchkick
Section titled “Searchkick”Traced via ActiveSupport::Notifications.
Cache Operations
Section titled “Cache Operations”Rails cache operations are traced:
cache.readcache.writecache.deletecache.fetch
Manual Span Creation
Section titled “Manual Span Creation”Create custom spans for operations not automatically instrumented:
MiniAPM.span("process_order", category: :internal) do |span| span.add_attribute("order.id", order.id) span.add_attribute("order.total", order.total)
process_order(order)endSpan categories:
:http_server- Incoming HTTP requests:http_client- Outgoing HTTP requests:db- Database operations:cache- Cache operations:job- Background jobs:internal- Custom internal operations
Accessing Current Context
Section titled “Accessing Current Context”# Get current trace ID (useful for logging)MiniAPM.current_trace_id
# Get current spanMiniAPM.current_span
# Get current span IDMiniAPM.current_span_idDisabling Specific Instrumentation
Section titled “Disabling Specific Instrumentation”MiniAPM.configure do |config| # Disable Redis instrumentation config.instrument :redis, enabled: false
# Disable SQL logging config.instrument :activerecord, log_sql: falseendParameter Filtering
Section titled “Parameter Filtering”Sensitive parameters are automatically filtered:
MiniAPM.configure do |config| config.filter_parameters = [ :password, :token, :api_key, :secret, :credit_card ]endError Capture
Section titled “Error Capture”Exceptions are automatically captured with:
- Full stack trace
- Request context
- User ID (if available)
- Custom parameters
Ignore specific exceptions:
MiniAPM.configure do |config| config.ignored_exceptions = [ 'ActionController::RoutingError', 'ActionController::InvalidAuthenticityToken', 'ActionController::UnknownFormat', 'ActiveRecord::RecordNotFound' ]end