Browse Source

instrumentalizer OK

main
pvincent 4 weeks ago
parent
commit
609ca764b3
  1. 49
      config/initializers/instrumentalizing.rb
  2. 29
      lib/semantic/instrumentalizer.rb

49
config/initializers/instrumentalizing.rb

@ -15,51 +15,18 @@ def build_instrumentation_config
end.compact end.compact
end end
Rails.autoloaders.main.on_load('ApplicationController') do
puts '--- Zeitwerk RELOAD ---'
Semantic::Instrumentalizer.activate(*build_instrumentation_config)
end
Rails.configuration.after_initialize do Rails.configuration.after_initialize do
puts '---- AFTER INITIALIZE ---------'
Rails.logger.name = 'rails' Rails.logger.name = 'rails'
ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event| 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 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 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

29
lib/semantic/instrumentalizer.rb

@ -1,12 +1,13 @@
module Semantic 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 # enable/disable instrumentation callbacks
class Instrumentalizer 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 class << self
def activate(*event_groups) def activate(*event_groups)
reset reset
@ -15,24 +16,28 @@ module Semantic
private private
def logger = @logger ||= SemanticLogger[:instrumentation]
def global_subscribers = $global_subscribers ||= [] # rubocop:disable Style/GlobalVars
def reset def reset
global_subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
global_subscribers.clear
NOTIFICATIONS.each do |event_group, hooks| 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
end end
def enable(event_group) def enable(event_group)
logger.debug { "enabling: #{event_group}" }
log_subscriber = build_log_subscriber_from(event_group) log_subscriber = build_log_subscriber_from(event_group)
NOTIFICATIONS[event_group].each do |hook| 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}") # logger.debug("SEND #{log_subscriber} hook=#{hook}")
log_subscriber.send(hook, event) log_subscriber.send(hook, event)
end end
global_subscribers << subscriber
end end
end end

Loading…
Cancel
Save