Ruby rails: notifications

From wikinotes
Revision as of 16:05, 6 April 2021 by Will (talk | contribs) (Created page with " = Documentation = <blockquote> {| class="wikitable" |- | instrumentation docs || https://guides.rubyonrails.org/active_support_instrumentation.html#introduction-to-instrumen...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Documentation

instrumentation docs https://guides.rubyonrails.org/active_support_instrumentation.html#introduction-to-instrumentation

Basics

ActiveSupport::Notifications.subscribe("") do |name, started, finished, unique_id, data|
  puts name, started, finished, unique_id, data
end

# name:      name of event (ex: 'process_action.action_controller')
# started:   Time
# finished:  Time
# unique_id:
# data:


ActiveSupport::Notifications.subscribe("process_action.action_controller")  # explicit event-name
ActiveSupport::Notifications.subscribe(/process_action/)                    # regex match event-name
ActiveSupport::Notifications.monotonic_subscribe(/process_action/)          # incl. leap/skipped seconds in start/end

There are several builtin events. See rails framework hooks.

Custom Events

ActiveSupport::Notifications.instrument("my.custom.event", param1: 1, param2: 2)
# all keys are recorded as data