pvincent
3 months ago
6 changed files with 90 additions and 24 deletions
-
22config/initializers/semantic_logger.rb
-
2lib/live/definable.rb
-
3lib/semantic/fancy_formatter.rb
-
68lib/semantic/instrumentation_manager.rb
-
18lib/semantic/subscribers/action_controller.rb
-
1lib/semantic/subscribers/action_view.rb
@ -1,22 +1,18 @@ |
|||
RailsSemanticLogger::Rack::Logger.logger.level = :info # useful for remaining log like "[Rack::Log] Started..." |
|||
SemanticLogger.clear_appenders! |
|||
|
|||
im = nil |
|||
# Zeitwerk reload message |
|||
Rails.autoloaders.main.on_load('ApplicationController') { SemanticLogger[:Zeitwerk].debug('reload!') } |
|||
|
|||
all_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] |
|||
} |
|||
Rails.autoloaders.main.on_load('ApplicationController') do |
|||
SemanticLogger[:zeitwerk].debug('reload!') |
|||
im = Semantic::InstrumentationManager.new |
|||
end |
|||
|
|||
Rails.configuration.after_initialize do |
|||
all_notifications.each do |event_group, hooks| |
|||
hooks.each { |hook| ActiveSupport::Notifications.unsubscribe("#{hook}.#{event_group}") } |
|||
end |
|||
|
|||
Semantic::InstrumentationManager.clear |
|||
ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event| |
|||
SemanticLogger[:live_notifications].warn('rolling.live_constant', event.payload) |
|||
# FIXME: to be continued... |
|||
im.process(event.payload[:changes]) |
|||
rescue StandardError => e |
|||
puts e |
|||
end |
|||
end |
@ -0,0 +1,68 @@ |
|||
module Semantic |
|||
class InstrumentationManager |
|||
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 |
|||
|
|||
def logger = @logger ||= SemanticLogger[:instrumentation] |
|||
|
|||
def initialize |
|||
logger.debug 're-initialized' |
|||
end |
|||
|
|||
def self.clear |
|||
NOTIFICATIONS.each do |event_group, hooks| |
|||
hooks.each { |hook| ActiveSupport::Notifications.unsubscribe("#{hook}.#{event_group}") } |
|||
end |
|||
end |
|||
|
|||
def process(changes) |
|||
logger.info "process called with #{changes.size} change(s)" |
|||
changes.each do |change| |
|||
constant = change[:constant] |
|||
value = change[:new_value] |
|||
case constant |
|||
when 'ACTION_VIEW', 'ACTION_CONTROLLER', 'ACTIVE_RECORD' |
|||
value ? enable(constant) : disable(constant) |
|||
end |
|||
end |
|||
logger.info 'DONE' |
|||
end |
|||
|
|||
def enable(constant) |
|||
logger.info("enable: #{constant}") |
|||
event_group = constant.underscore.to_sym |
|||
log_subscriber = build_log_subscriber_from(event_group.to_s.camelize) |
|||
logger.warn(log_subscriber) |
|||
|
|||
NOTIFICATIONS[event_group].each do |hook| |
|||
logger.info("subscribe to #{hook}.#{event_group}") |
|||
ActiveSupport::Notifications.subscribe("#{hook}.#{event_group}") do |event| |
|||
# logger.debug("SEND #{log_subscriber} hook=#{hook}") |
|||
log_subscriber.send(hook, event) |
|||
end |
|||
end |
|||
end |
|||
|
|||
def disable(constant) |
|||
logger.info("disable: #{constant}") |
|||
event_group = constant.underscore.to_sym |
|||
|
|||
logger.debug(NOTIFICATIONS[event_group].size) |
|||
|
|||
NOTIFICATIONS[event_group].each do |hook| |
|||
logger.info("unsubscribe to #{hook}.#{event_group}") |
|||
ActiveSupport::Notifications.unsubscribe("#{hook}.#{event_group}") |
|||
end |
|||
end |
|||
|
|||
def build_log_subscriber_from(classname) |
|||
case classname |
|||
when 'ActionController' then Semantic::Subscribers.const_get(classname).new('toto1') |
|||
else Semantic::Subscribers.const_get(classname).new |
|||
end |
|||
end |
|||
end |
|||
end |
Write
Preview
Loading…
Cancel
Save
Reference in new issue