Class: Chronos::Rails::NotificationsSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/rails/notifications_subscriber.rb

Overview

Converts public ActiveSupport notifications into bounded Chronos telemetry.

Examples:

Chronos::Rails::NotificationsSubscriber.new.install

Constant Summary collapse

EVENTS =
%w(
  process_action.action_controller render_template.action_view sql.active_record
  deliver.action_mailer perform.active_job cache_read.active_support
  cache_write.active_support cache_fetch_hit.active_support
).freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notifier = Chronos, notifications = nil) ⇒ NotificationsSubscriber

Returns a new instance of NotificationsSubscriber.



29
30
31
32
33
# File 'lib/chronos/rails/notifications_subscriber.rb', line 29

def initialize(notifier = Chronos, notifications = nil)
  @notifier = notifier
  @notifications = notifications || active_support_notifications
  @sql_normalizer = Core::SqlNormalizer.new
end

Class Attribute Details

.installed_busesObject (readonly)

Returns the value of attribute installed_buses.



26
27
28
# File 'lib/chronos/rails/notifications_subscriber.rb', line 26

def installed_buses
  @installed_buses
end

.mutexObject (readonly)

Returns the value of attribute mutex.



26
27
28
# File 'lib/chronos/rails/notifications_subscriber.rb', line 26

def mutex
  @mutex
end

Instance Method Details

#handle(name, arguments) ⇒ Object



49
50
51
52
53
54
# File 'lib/chronos/rails/notifications_subscriber.rb', line 49

def handle(name, arguments)
  event = notification_event(name, arguments)
  dispatch(name, event[:payload], event[:duration_ms])
rescue StandardError
  false
end

#installObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chronos/rails/notifications_subscriber.rb', line 35

def install
  return false unless @notifications && @notifications.respond_to?(:subscribe)

  self.class.mutex.synchronize do
    return false if self.class.installed_buses[@notifications.object_id]

    event_names.each { |name| subscribe(name) }
    self.class.installed_buses[@notifications.object_id] = true
  end
  true
rescue StandardError
  false
end