Class: StandardHealth::Subscribers Private

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_health/subscribers.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Registers internal and user-supplied subscribers against whichever event bus is live (Rails.event on 8.1+, ActiveSupport::Notifications elsewhere).

Each subscriber must respond to call(event_name, payload). Internal subscribers (Logger / Sentry / Metrics) are built from the live config so changes to metric_prefix / logger propagate when setup! is re-run.

Subscriptions cover the namespace prefix standard_health., so a single registration on each backend listens for every event the gem emits.

Defined Under Namespace

Classes: RailsEventAdapter

Constant Summary collapse

EVENT_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"standard_health."
EVENT_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A#{Regexp.escape(EVENT_PATTERN)}/

Instance Method Summary collapse

Constructor Details

#initializeSubscribers

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Subscribers.



19
20
21
22
# File 'lib/standard_health/subscribers.rb', line 19

def initialize
  @rails_event_subscribers = []
  @as_subscribers = []
end

Instance Method Details

#setup!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
27
28
29
# File 'lib/standard_health/subscribers.rb', line 24

def setup!
  teardown!
  return unless StandardHealth.config.instrumentation_enabled

  register(internal_subscribers + extra_subscribers)
end

#teardown!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Tear down both backends. Rails.event subscribers are unsubscribed unconditionally — we recorded them at registration time, so we must remove them even if Rails.event has since become unavailable (e.g. test hide_const("Rails")). Otherwise the wrappers would remain live in the bus while we believe they are gone.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/standard_health/subscribers.rb', line 36

def teardown!
  @rails_event_subscribers.each do |subscriber|
    ::Rails.event.unsubscribe(subscriber) if EventEmitter.rails_event_available?
  rescue StandardError
    # If Rails.event is gone (test isolation), we can do nothing more —
    # clearing the array still releases our reference.
  end
  @rails_event_subscribers.clear

  @as_subscribers.each do |subscriber|
    ::ActiveSupport::Notifications.unsubscribe(subscriber)
  end
  @as_subscribers.clear
end