From 995c6698d069749dbd87e1a19bba1efbe607bdf7 Mon Sep 17 00:00:00 2001 From: pvincent Date: Sat, 21 Sep 2024 16:42:38 +0400 Subject: [PATCH] runtime_registry_subscriber --- lib/semantic/instrumentalizer.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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