Class: Karafka::Core::Monitoring::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/core/monitoring/monitor.rb

Overview

Karafka monitor that can be used to pass through instrumentation calls to selected notifications bus.

It provides abstraction layer that allows us to use both our internal notifications as well as ActiveSupport::Notifications.

Instance Method Summary collapse

Constructor Details

#initialize(notifications_bus, namespace = nil) ⇒ Monitor

Returns a new instance of Monitor.

Parameters:

  • notifications_bus (Object)

    either our internal notifications bus or ActiveSupport::Notifications

  • namespace (String, nil) (defaults to: nil)

    namespace for events or nil if no namespace



20
21
22
23
24
# File 'lib/karafka/core/monitoring/monitor.rb', line 20

def initialize(notifications_bus, namespace = nil)
  @notifications_bus = notifications_bus
  @namespace = namespace
  @mapped_events = {}
end

Instance Method Details

#instrument(event_id, payload = EMPTY_HASH, &block) ⇒ Object

Passes the instrumentation block (if any) into the notifications bus

Parameters:

  • event_id (String, Symbol)

    event id

  • payload (Hash) (defaults to: EMPTY_HASH)
  • block (Proc)

    block we want to instrument (if any)



31
32
33
34
35
# File 'lib/karafka/core/monitoring/monitor.rb', line 31

def instrument(event_id, payload = EMPTY_HASH, &block)
  full_event_name = @mapped_events[event_id] ||= [event_id, @namespace].compact.join('.')

  @notifications_bus.instrument(full_event_name, payload, &block)
end

#subscribe(*args, &block) ⇒ Object

Allows us to subscribe to the notification bus

Parameters:

  • args (Array)

    any arguments that the notification bus subscription layer accepts

  • block (Proc)

    optional block for subscription



41
42
43
# File 'lib/karafka/core/monitoring/monitor.rb', line 41

def subscribe(*args, &block)
  @notifications_bus.subscribe(*args, &block)
end