You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

  1. module Semantic
  2. class InstrumentationManager
  3. NOTIFICATIONS = {
  4. action_controller: %i[start_processing process_action redirect_to],
  5. action_view: %i[render_partial render_template render_collection render_layout],
  6. active_record: %i[sql strict_loading instantiation start_transaction transaction]
  7. }.freeze
  8. def logger = @logger ||= SemanticLogger[:instrumentation]
  9. def initialize
  10. logger.debug 're-initialized'
  11. end
  12. def self.clear
  13. NOTIFICATIONS.each do |event_group, hooks|
  14. hooks.each { |hook| ActiveSupport::Notifications.unsubscribe("#{hook}.#{event_group}") }
  15. end
  16. end
  17. def process(changes)
  18. logger.info "process called with #{changes.size} change(s)"
  19. changes.each do |change|
  20. constant = change[:constant]
  21. value = change[:new_value]
  22. case constant
  23. when 'ACTION_VIEW', 'ACTION_CONTROLLER', 'ACTIVE_RECORD'
  24. value ? enable(constant) : disable(constant)
  25. end
  26. end
  27. logger.info 'DONE'
  28. end
  29. def enable(constant)
  30. logger.info("enable: #{constant}")
  31. event_group = constant.underscore.to_sym
  32. log_subscriber = build_log_subscriber_from(event_group.to_s.camelize)
  33. logger.warn(log_subscriber)
  34. NOTIFICATIONS[event_group].each do |hook|
  35. logger.info("subscribe to #{hook}.#{event_group}")
  36. ActiveSupport::Notifications.subscribe("#{hook}.#{event_group}") do |event|
  37. # logger.debug("SEND #{log_subscriber} hook=#{hook}")
  38. log_subscriber.send(hook, event)
  39. end
  40. end
  41. end
  42. def disable(constant)
  43. logger.info("disable: #{constant}")
  44. event_group = constant.underscore.to_sym
  45. logger.debug(NOTIFICATIONS[event_group].size)
  46. NOTIFICATIONS[event_group].each do |hook|
  47. logger.info("unsubscribe to #{hook}.#{event_group}")
  48. ActiveSupport::Notifications.unsubscribe("#{hook}.#{event_group}")
  49. end
  50. end
  51. def build_log_subscriber_from(classname)
  52. case classname
  53. when 'ActionController' then Semantic::Subscribers.const_get(classname).new('toto1')
  54. else Semantic::Subscribers.const_get(classname).new
  55. end
  56. end
  57. end
  58. end