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
34
35
36
37
# 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
  cache_options = notifier.respond_to?(:cache_integration_options) ? notifier.cache_integration_options : {}
  @cache_normalizer = Core::CacheNormalizer.new(
    cache_options[:project_id].to_s, cache_options[:key_mode] || :none
  )
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



53
54
55
56
57
58
# File 'lib/chronos/rails/notifications_subscriber.rb', line 53

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

#installObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chronos/rails/notifications_subscriber.rb', line 39

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