Class: ActiveSupport::StructuredEventSubscriber
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Subscriber
|
|
Instance Chain:
self,
Subscriber
|
|
Inherits: |
ActiveSupport::Subscriber
|
Defined in: | activesupport/lib/active_support/structured_event_subscriber.rb |
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.
Constant Summary
-
DEBUG_CHECK =
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 34proc { !ActiveSupport.event_reporter.debug_mode? }
Class Attribute Summary
Subscriber
- Inherited
Class Method Summary
- .attach_to
- .new ⇒ StructuredEventSubscriber constructor
- .debug_only(method) private
- .set_silenced_events private
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
- #call(event)
-
#emit_debug_event(name, payload = nil, caller_depth: 1, **kwargs)
Like #emit_event, but only emits when the event reporter is in debug mode.
-
#emit_event(name, payload = nil, caller_depth: 1, **kwargs)
Emit a structured event via Rails.event.notify.
- #silenced?(event) ⇒ Boolean
- #handle_event_error(name, error) private
Subscriber
- Inherited
Constructor Details
.new ⇒ StructuredEventSubscriber
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 56
def initialize super @silenced_events = {} end
Class Attribute Details
.debug_methods (rw)
[ GitHub ]# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 32
class_attribute :debug_methods, instance_accessor: false, default: [] # :nodoc:
.debug_methods? ⇒ Boolean
(rw)
[ GitHub ]
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 32
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 37
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 50
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 44
def set_silenced_events if subscriber subscriber.silenced_events = debug_methods.to_h { |method| ["#{method}.#{namespace}", DEBUG_CHECK] } end end
Instance Attribute Details
#silenced_events=(value) (writeonly)
[ GitHub ]# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 65
attr_writer :silenced_events # :nodoc:
Instance Method Details
#call(event)
[ GitHub ]# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 88
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
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 82
def emit_debug_event(name, payload = nil, caller_depth: 1, **kwargs) ActiveSupport.event_reporter.debug(name, payload, caller_depth: caller_depth + 1, **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
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 75
def emit_event(name, payload = nil, caller_depth: 1, **kwargs) ActiveSupport.event_reporter.notify(name, payload, caller_depth: caller_depth + 1, **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 95
def handle_event_error(name, error) ActiveSupport.error_reporter.report(error, source: name) end
#silenced?(event) ⇒ Boolean
# File 'activesupport/lib/active_support/structured_event_subscriber.rb', line 61
def silenced?(event) ActiveSupport.event_reporter.subscribers.none? || @silenced_events[event]&.call end