diff --git a/lib/semantic/instrumentalizer.rb b/lib/semantic/instrumentalizer.rb index 2bbb853..6f008bc 100644 --- a/lib/semantic/instrumentalizer.rb +++ b/lib/semantic/instrumentalizer.rb @@ -11,11 +11,26 @@ module Semantic class << self def activate(*event_groups) reset + runtime_registry_subscriber event_groups.each { |event_group| enable(event_group) } end private + def runtime_registry_subscriber + global_subscribers << ActiveSupport::Notifications.monotonic_subscribe('sql.active_record') do |name, start, finish, id, payload| + unless %w[SCHEMA TRANSACTION].include?(payload[:name]) + ActiveRecord::RuntimeRegistry.queries_count += 1 + ActiveRecord::RuntimeRegistry.cached_queries_count += 1 if payload[:cached] + end + + runtime = (finish - start) * 1_000.0 + + ActiveRecord::RuntimeRegistry.async_sql_runtime += (runtime - payload[:lock_wait]) if payload[:async] + ActiveRecord::RuntimeRegistry.sql_runtime += runtime + end + end + def global_subscribers @global_subscribers ||= [] end