From 609ca764b3d0907b1f6a536a8a2aababa3f8af75 Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 19 Sep 2024 21:34:15 +0400 Subject: [PATCH] instrumentalizer OK --- config/initializers/instrumentalizing.rb | 49 ++++-------------------- lib/semantic/instrumentalizer.rb | 29 ++++++++------ 2 files changed, 25 insertions(+), 53 deletions(-) diff --git a/config/initializers/instrumentalizing.rb b/config/initializers/instrumentalizing.rb index 54a0480..d9c8852 100644 --- a/config/initializers/instrumentalizing.rb +++ b/config/initializers/instrumentalizing.rb @@ -15,51 +15,18 @@ def build_instrumentation_config end.compact end -Rails.autoloaders.main.on_load('ApplicationController') do - puts '--- Zeitwerk RELOAD ---' - Semantic::Instrumentalizer.activate(*build_instrumentation_config) -end - Rails.configuration.after_initialize do - puts '---- AFTER INITIALIZE ---------' Rails.logger.name = 'rails' ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event| - puts event.payload[:changes].inspect - # TODO: if key includes ACTIVE_RECORD ACTION_VIEW - Semantic::Instrumentalizer.activate(*build_instrumentation_config) + constants = event.payload[:changes].map { |change| change[:constant] } + if constants.intersection(%w[ACTIVE_RECORD ACTION_VIEW]).any? + Semantic::Instrumentalizer.activate(*build_instrumentation_config) + end end - - # im = nil - # Rails.autoloaders.main.on_load('ApplicationController') do - # SemanticLogger[:zeitwerk].debug('reload!') - # im = Semantic::InstrumentationManager.new - # end - - # Semantic::InstrumentationManager.clear - - # bootstrap = [{ kind: :restored, constant: 'ACTION_VIEW', type: :boolean, old_value: false, new_value: true }, - # { kind: :restored, constant: 'ACTION_CONTROLLER', type: :boolean, old_value: false, new_value: true }] - # im ||= Semantic::InstrumentationManager.new - # im.process(bootstrap) end -# RAILS_HOOKS = %i[ -# action_controller -# action_controller_api -# action_controller_base -# action_dispatch_request -# action_dispatch_response -# action_view -# active_job -# active_model -# active_record -# active_record_fixture_set -# after_initialize -# after_routes_loaded -# before_configuration -# before_eager_load -# before_initialize -# i18n -# message_pack -# ].freeze +Rails.autoloaders.main.on_load('ApplicationController') do + Rails.logger.debug 'Zeitwerk RELOAD!' + Semantic::Instrumentalizer.activate(*build_instrumentation_config) +end diff --git a/lib/semantic/instrumentalizer.rb b/lib/semantic/instrumentalizer.rb index 405eb79..cd98ee0 100644 --- a/lib/semantic/instrumentalizer.rb +++ b/lib/semantic/instrumentalizer.rb @@ -1,12 +1,13 @@ module Semantic + NOTIFICATIONS = { + action_controller: %i[start_processing process_action redirect_to], + action_view: %i[render_partial render_template render_collection render_layout], + active_record: %i[sql strict_loading instantiation start_transaction transaction] + }.freeze + # enable/disable instrumentation callbacks class Instrumentalizer - NOTIFICATIONS = { - action_controller: %i[start_processing process_action redirect_to], - action_view: %i[render_partial render_template render_collection render_layout], - active_record: %i[sql strict_loading instantiation start_transaction transaction] - }.freeze - + include SemanticLogger::Loggable class << self def activate(*event_groups) reset @@ -15,24 +16,28 @@ module Semantic private - def logger = @logger ||= SemanticLogger[:instrumentation] + def global_subscribers = $global_subscribers ||= [] # rubocop:disable Style/GlobalVars def reset + global_subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) } + global_subscribers.clear + NOTIFICATIONS.each do |event_group, hooks| - hooks.each { |hook| ActiveSupport::Notifications.unsubscribe("#{hook}.#{event_group}") } + hooks.each do |hook| + hook_full_name = "#{hook}.#{event_group}" + ActiveSupport::Notifications.unsubscribe(hook_full_name) + end end end def enable(event_group) - logger.debug { "enabling: #{event_group}" } - log_subscriber = build_log_subscriber_from(event_group) NOTIFICATIONS[event_group].each do |hook| - logger.info("subscribe to #{hook}.#{event_group}") - ActiveSupport::Notifications.subscribe("#{hook}.#{event_group}") do |event| + subscriber = ActiveSupport::Notifications.subscribe("#{hook}.#{event_group}") do |event| # logger.debug("SEND #{log_subscriber} hook=#{hook}") log_subscriber.send(hook, event) end + global_subscribers << subscriber end end