Module: Alplus::NotificationsSubscriber

Defined in:
lib/alplus/notifications_subscriber.rb

Overview

Auto-breadcrumbs from ActiveSupport::Notifications (Rails only): pushes a small, bounded set of framework events into the current request's Scope as breadcrumbs, so a captured exception's timeline shows "what happened just before this" without the host app calling Alplus.add_breadcrumb by hand (mirrors Sentry's/AppSignal's instrumentation breadcrumbs).

Wired from Railtie#install! (guarded there too) -- a total no-op outside Rails/ActiveSupport, and never subscribed twice even if a host process boots more than one Rails::Application (test suites do this routinely).

Deliberately a SMALL, fixed event list: more events means more noise per request and a bigger cut of the Scope's bounded breadcrumb ring buffer (Scope::MAX_BREADCRUMBS) spent on framework chatter instead of the host app's own add_breadcrumb calls.

Constant Summary collapse

EVENTS =
%w[sql.active_record process_action.action_controller start_processing.action_controller].freeze

Class Method Summary collapse

Class Method Details

.handle(event_name, notification) ⇒ Object

Fail-safe: instrumentation must never break the request it is observing. Any error here (a payload shape this Rails version doesn't produce, a Scope write racing shutdown, etc.) is swallowed.



49
50
51
52
53
54
55
56
57
58
# File 'lib/alplus/notifications_subscriber.rb', line 49

def handle(event_name, notification)
  return unless breadcrumbs_enabled?

  breadcrumb = build_breadcrumb(event_name, notification)
  return unless breadcrumb

  Scope.current.add_breadcrumb(**breadcrumb)
rescue StandardError
  nil
end

.install!Object

Idempotent: a second call (e.g. a second Rails::Application boot in the same process, common in test suites) is a no-op.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/alplus/notifications_subscriber.rb', line 26

def install!
  return if @installed
  return unless defined?(::ActiveSupport::Notifications)

  EVENTS.each do |event_name|
    ::ActiveSupport::Notifications.subscribe(event_name) do |*args|
      handle(event_name, ::ActiveSupport::Notifications::Event.new(*args))
    end
  end
  @installed = true
end

.reset!Object

Test-only: lets a spec re-install (e.g. against a stubbed Scope/Configuration) without carrying state from a previous example. Not called by production code.



41
42
43
# File 'lib/alplus/notifications_subscriber.rb', line 41

def reset!
  @installed = false
end