Class: ActiveSupport::StructuredEventSubscriber
- Inherits:
-
Subscriber
- Object
- Subscriber
- ActiveSupport::StructuredEventSubscriber
- Defined in:
- lib/active_support/structured_event_subscriber.rb
Overview
Active Support Structured Event Subscriber
ActiveSupport::StructuredEventSubscriber consumes ActiveSupport::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 (ActiveSupport::Notifications::Event) to the start_processing method. The subscriber can then emit a structured event via the emit_event method.
Constant Summary collapse
- DEBUG_CHECK =
proc { !ActiveSupport.event_reporter.debug_mode? }
Instance Attribute Summary collapse
-
#silenced_events ⇒ Object
writeonly
:nodoc:.
Attributes inherited from Subscriber
Class Method Summary collapse
-
.attach_to ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #call(event) ⇒ Object
-
#emit_debug_event(name, payload = nil, caller_depth: 1, **kwargs) ⇒ Object
Like
emit_event, but only emits when the event reporter is in debug mode. -
#emit_event(name, payload = nil, caller_depth: 1, **kwargs) ⇒ Object
Emit a structured event via Rails.event.notify.
-
#initialize ⇒ StructuredEventSubscriber
constructor
A new instance of StructuredEventSubscriber.
- #silenced?(event) ⇒ Boolean
Methods inherited from Subscriber
detach_from, method_added, subscribers
Constructor Details
#initialize ⇒ StructuredEventSubscriber
Returns a new instance of StructuredEventSubscriber.
56 57 58 59 |
# File 'lib/active_support/structured_event_subscriber.rb', line 56 def initialize super @silenced_events = {} end |
Instance Attribute Details
#silenced_events=(value) ⇒ Object (writeonly)
:nodoc:
65 66 67 |
# File 'lib/active_support/structured_event_subscriber.rb', line 65 def silenced_events=(value) @silenced_events = value end |
Class Method Details
.attach_to ⇒ Object
:nodoc:
37 38 39 40 41 |
# File 'lib/active_support/structured_event_subscriber.rb', line 37 def attach_to(...) # :nodoc: result = super set_silenced_events result end |
Instance Method Details
#call(event) ⇒ Object
88 89 90 91 92 |
# File '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) ⇒ Object
Like emit_event, but only emits when the event reporter is in debug mode
82 83 84 85 86 |
# File '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) ⇒ Object
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
75 76 77 78 79 |
# File '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 |
#silenced?(event) ⇒ Boolean
61 62 63 |
# File 'lib/active_support/structured_event_subscriber.rb', line 61 def silenced?(event) ActiveSupport.event_reporter.subscribers.none? || @silenced_events[event]&.call end |