Class: Findbug::Performance::Instrumentation
- Inherits:
-
Object
- Object
- Findbug::Performance::Instrumentation
- Defined in:
- lib/findbug/performance/instrumentation.rb
Overview
Instrumentation subscribes to Rails’ ActiveSupport::Notifications.
WHAT IS ActiveSupport::Notifications?
Rails has a built-in pub/sub system for internal events. Every time something interesting happens, Rails publishes a notification:
- sql.active_record → Database queries
- process_action.action_controller → HTTP requests
- render_template.action_view → View rendering
- cache_read.active_support → Cache operations
Any code can subscribe to these events:
ActiveSupport::Notifications.subscribe("sql.active_record") do |event|
puts "Query took #{event.duration}ms"
end
This is how Rails’ request logs, performance gems, and APM tools work. We subscribe to capture timing data for our dashboard.
WHY NOT MIDDLEWARE FOR PERFORMANCE?
Middleware only sees the request start and end. It can’t see:
-
Individual SQL queries
-
Which view took how long
-
Cache hits/misses
Notifications give us granular visibility into the request lifecycle.
Constant Summary collapse
- SUBSCRIPTIONS =
[ "process_action.action_controller", "sql.active_record", "render_template.action_view", "render_partial.action_view", "cache_read.active_support", "cache_write.active_support" ].freeze
Class Method Summary collapse
-
.setup! ⇒ Object
Set up all instrumentation subscriptions.
-
.teardown! ⇒ Object
Tear down subscriptions (for testing).
Class Method Details
.setup! ⇒ Object
Set up all instrumentation subscriptions
Called once during Rails initialization (via Railtie).
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/findbug/performance/instrumentation.rb', line 54 def setup! return if @setup_complete return unless Findbug.config.performance_enabled subscribe_to_requests subscribe_to_queries subscribe_to_views subscribe_to_cache @setup_complete = true Findbug.logger.debug("[Findbug] Performance instrumentation enabled") end |
.teardown! ⇒ Object
Tear down subscriptions (for testing)
68 69 70 71 72 73 74 |
# File 'lib/findbug/performance/instrumentation.rb', line 68 def teardown! @subscriptions&.each do |subscriber| ActiveSupport::Notifications.unsubscribe(subscriber) end @subscriptions = [] @setup_complete = false end |