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.

55 lines
1.7 KiB

  1. module Semantic
  2. NOTIFICATIONS = {
  3. action_controller: %i[start_processing process_action redirect_to],
  4. action_view: %i[render_partial render_template render_collection render_layout],
  5. active_record: %i[sql strict_loading instantiation start_transaction transaction]
  6. }.freeze
  7. # enable/disable instrumentation callbacks
  8. class Instrumentalizer
  9. include SemanticLogger::Loggable
  10. class << self
  11. def activate(*event_groups)
  12. reset
  13. event_groups.each { |event_group| enable(event_group) }
  14. end
  15. private
  16. def global_subscribers = $global_subscribers ||= [] # rubocop:disable Style/GlobalVars
  17. def reset
  18. global_subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
  19. global_subscribers.clear
  20. NOTIFICATIONS.each do |event_group, hooks|
  21. hooks.each do |hook|
  22. hook_full_name = "#{hook}.#{event_group}"
  23. ActiveSupport::Notifications.unsubscribe(hook_full_name)
  24. end
  25. end
  26. end
  27. def enable(event_group)
  28. log_subscriber = build_log_subscriber_from(event_group)
  29. NOTIFICATIONS[event_group].each do |hook|
  30. subscriber = ActiveSupport::Notifications.subscribe("#{hook}.#{event_group}") do |event|
  31. # logger.debug("SEND #{log_subscriber} hook=#{hook}")
  32. log_subscriber.send(hook, event)
  33. end
  34. global_subscribers << subscriber
  35. end
  36. end
  37. def build_log_subscriber_from(event_group)
  38. classname = event_group.to_s.camelize
  39. case classname
  40. when 'ActionController'
  41. Semantic::Subscribers.const_get(classname).new('toto1')
  42. else
  43. Semantic::Subscribers.const_get(classname).new
  44. end
  45. end
  46. end
  47. end
  48. end