Class: Datadog::Tracing::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/event.rb

Overview

A simple pub-sub event model for components to exchange messages through.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Event

Returns a new instance of Event.



43
44
45
46
# File 'lib/datadog/tracing/event.rb', line 43

def initialize(name)
  @name = name
  @subscriptions = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/datadog/tracing/event.rb', line 39

def name
  @name
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



39
40
41
# File 'lib/datadog/tracing/event.rb', line 39

def subscriptions
  @subscriptions
end

Instance Method Details

#publish(*args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/datadog/tracing/event.rb', line 60

def publish(*args)
  subscriptions.each do |block|
    begin
      block.call(*args)
    rescue StandardError => e
      Datadog.logger.debug do
        "Error while handling '#{name}' event with '#{block}': #{e.class.name} #{e.message} " \
        "at #{Array(e.backtrace).first}"
      end
    end
  end

  true
end

#subscribe(&block) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
# File 'lib/datadog/tracing/event.rb', line 48

def subscribe(&block)
  raise ArgumentError, 'Must give a block to subscribe!' unless block

  subscriptions << block
end

#unsubscribe_all!Object



54
55
56
57
58
# File 'lib/datadog/tracing/event.rb', line 54

def unsubscribe_all!
  subscriptions.clear

  true
end