module Hallmonitor::Monitored

Public Class Methods

included(base) click to toggle source
# File lib/hallmonitor/monitored.rb, line 57
def self.included(base)
  base.extend(ClassMethods)
  base.extend(self)
end

Public Instance Methods

emit(event = nil, tags: {}) { |to_emit| ... } click to toggle source

Emits an event: self if the event param is nil, the passed in event if it's an {Event}, or constructs a {Event} from the passed in param.

If the parameter is a {Hallmonitor::Event}, it will be emitted as is. Otherwise, a new {Hallmonitor::Event} will be created with the parameter and emitted.

@param event [Mixed] The thing to emit, see method description @yield [to_emit] The thing that's going to be emitted

# File lib/hallmonitor/monitored.rb, line 72
def emit(event = nil, tags: {})
  to_emit = self
  unless event.nil?
    to_emit = event.is_a?(Hallmonitor::Event) ? event : Hallmonitor::Event.new(event, tags: tags)
  end

  # If we were given a block, then we want to execute that
  yield(to_emit) if block_given?

  Dispatcher.output(to_emit)
  nil
end
watch(name, tags: {}) { |event| ... } click to toggle source

Executes and times a block of code and emits a {TimedEvent} @note Will emit the timed event even if the block raises an error @param name [String] The name of the event to emit @yield [event] the event object that will be emitted @return Whatever the block's return value is

# File lib/hallmonitor/monitored.rb, line 90
def watch(name, tags: {})
  event = Hallmonitor::TimedEvent.new(name, tags: tags)
  event.start = Time.now
  begin
    yield(event)
  ensure
    event.stop = Time.now
    emit(event)
  end
end