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.

61 lines
1.9 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. rescue StandardError => e
  38. logger.error('Error during instrumentation handling', e)
  39. end
  40. global_subscribers << subscriber
  41. end
  42. end
  43. def build_log_subscriber_from(event_group)
  44. classname = event_group.to_s.camelize
  45. case classname
  46. when 'ActionController'
  47. Semantic::Subscribers.const_get(classname).new('toto1') # FIXME: externalize session_key
  48. else
  49. Semantic::Subscribers.const_get(classname).new
  50. end
  51. end
  52. end
  53. end
  54. end