Class: Karafka::Instrumentation::Monitor

Inherits:
Dry::Monitor::Notifications
  • Object
show all
Defined in:
lib/karafka/instrumentation/monitor.rb

Overview

Note:

This class acts as a singleton because we are only permitted to have single monitor per running process (just as logger)

Monitor is used to hookup external monitoring services to monitor how Karafka works It provides a standardized API for checking incoming messages/enqueueing etc Since it is a pub-sub based on dry-monitor, you can use as many subscribers/loggers at the same time, which means that you might have for example file logging and NewRelic at the same time

Instance Method Summary collapse

Constructor Details

#initializeKarafka::Instrumentation::Monitor

Returns monitor instance for system instrumentation.



49
50
51
52
# File 'lib/karafka/instrumentation/monitor.rb', line 49

def initialize
  super(:karafka)
  BASE_EVENTS.each(&method(:register_event))
end

Instance Method Details

#available_eventsArray<String>

Returns names of available events to which we can subscribe.

Returns:

  • (Array<String>)

    names of available events to which we can subscribe



65
66
67
# File 'lib/karafka/instrumentation/monitor.rb', line 65

def available_events
  __bus__.events.keys
end

#subscribe(event_name_or_listener) ⇒ Object

Allows us to subscribe to events with a code that will be yielded upon events

Parameters:

  • event_name_or_listener (String, Object)

    name of the event we want to subscribe to or a listener if we decide to go with object listener

Raises:



57
58
59
60
61
62
# File 'lib/karafka/instrumentation/monitor.rb', line 57

def subscribe(event_name_or_listener)
  return super unless event_name_or_listener.is_a?(String)
  return super if available_events.include?(event_name_or_listener)

  raise Errors::UnregisteredMonitorEventError, event_name_or_listener
end