123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::StructuredEventSubscriber

Do not use. This class is for internal use only.

Overview

===========================================

StructuredEventSubscriber consumes Notifications in order to emit structured events via Rails.event.

An example would be the Action Controller structured event subscriber, responsible for emitting request processing events:

module ActionController
  class StructuredEventSubscriber < ActiveSupport::StructuredEventSubscriber
    attach_to :action_controller

    def start_processing(event)
      emit_event("controller.request_started",
        controller: event.payload[:controller],
        action: event.payload[:action],
        format: event.payload[:format]
      )
    end
  end
end

After configured, whenever a "start_processing.action_controller" notification is published, it will properly dispatch the event (Notifications::Event) to the start_processing method. The subscriber can then emit a structured event via the #emit_event method.

Class Attribute Summary

Class Method Summary

Subscriber - Inherited

.attach_to

Attach the subscriber to a namespace.

.detach_from

Detach the subscriber from a namespace.

.method_added

Adds event subscribers for all new methods added to the class.

.new, .subscribers, .add_event_subscriber, .fetch_public_methods, .find_attached_subscriber, .invalid_event?, .pattern_subscribed?, .prepare_pattern, .remove_event_subscriber

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newStructuredEventSubscriber

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 59

def initialize
  super
  @silenced_events = {}
end

Class Attribute Details

.debug_methods (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 37

class_attribute :debug_methods, instance_accessor: false, default: [] # :nodoc:

.debug_methods?Boolean (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 37

class_attribute :debug_methods, instance_accessor: false, default: [] # :nodoc:

Class Method Details

.attach_to

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 40

def attach_to(...) # :nodoc:
  result = super
  set_silenced_events
  result
end

.debug_only(method) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 53

def debug_only(method)
  self.debug_methods += [method]
  set_silenced_events
end

.set_silenced_events (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 47

def set_silenced_events
  if subscriber
    subscriber.silenced_events = debug_methods.to_h { |method| ["#{method}.#{namespace}", true] }
  end
end

Instance Attribute Details

#silenced_events=(value) (writeonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 68

attr_writer :silenced_events # :nodoc:

Instance Method Details

#call(event)

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 91

def call(event)
  super
rescue => e
  handle_event_error(event.name, e)
end

#emit_debug_event(name, payload = nil, caller_depth: 1, **kwargs)

Like #emit_event, but only emits when the event reporter is in debug mode

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 85

def emit_debug_event(name, payload = nil, caller_depth: 1, **kwargs)
  ActiveSupport.event_reporter.debug(name, payload, caller_depth: caller_depth + 1, filter_payload: false, **kwargs)
rescue => e
  handle_event_error(name, e)
end

#emit_event(name, payload = nil, caller_depth: 1, **kwargs)

Emit a structured event via Rails.event.notify.

Arguments

  • name - The event name as a string or symbol
  • payload - The event payload as a hash or object
  • caller_depth - Stack depth for source location (default: 1)
  • kwargs - Additional payload data merged with the payload hash
[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 78

def emit_event(name, payload = nil, caller_depth: 1, **kwargs)
  ActiveSupport.event_reporter.notify(name, payload, caller_depth: caller_depth + 1, filter_payload: false, **kwargs)
rescue => e
  handle_event_error(name, e)
end

#handle_event_error(name, error) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 98

def handle_event_error(name, error)
  ActiveSupport.error_reporter.report(error, source: name)
end

#silenced?(event) ⇒ Boolean

[ GitHub ]

  
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 64

def silenced?(event)
  ActiveSupport.event_reporter.subscribers.none? || (@silenced_events.key?(event) && !ActiveSupport.event_reporter.debug_mode?)
end