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.

59 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
  17. @global_subscribers ||= []
  18. end
  19. def reset
  20. global_subscribers.each do |sub|
  21. ActiveSupport::Notifications.unsubscribe(sub)
  22. end
  23. global_subscribers.clear
  24. NOTIFICATIONS.each do |event_group, hooks|
  25. hooks.each do |hook|
  26. hook_full_name = "#{hook}.#{event_group}"
  27. ActiveSupport::Notifications.unsubscribe(hook_full_name)
  28. end
  29. end
  30. end
  31. def enable(event_group)
  32. log_subscriber = build_log_subscriber_from(event_group)
  33. NOTIFICATIONS[event_group].each do |hook|
  34. subscriber = ActiveSupport::Notifications.subscribe("#{hook}.#{event_group}") do |event|
  35. # logger.debug("SEND #{log_subscriber} hook=#{hook}")
  36. log_subscriber.send(hook, event)
  37. end
  38. global_subscribers << subscriber
  39. end
  40. end
  41. def build_log_subscriber_from(event_group)
  42. classname = event_group.to_s.camelize
  43. case classname
  44. when 'ActionController'
  45. Semantic::Subscribers.const_get(classname).new('toto1')
  46. else
  47. Semantic::Subscribers.const_get(classname).new
  48. end
  49. end
  50. end
  51. end
  52. end