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.



41
42
43
44
# File 'lib/datadog/tracing/event.rb', line 41

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/datadog/tracing/event.rb', line 37

def name
  @name
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



37
38
39
# File 'lib/datadog/tracing/event.rb', line 37

def subscriptions
  @subscriptions
end

Instance Method Details

#publish(*args) ⇒ Object



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

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)


46
47
48
49
50
# File 'lib/datadog/tracing/event.rb', line 46

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

  subscriptions << block
end

#unsubscribe_all!Object



52
53
54
55
56
# File 'lib/datadog/tracing/event.rb', line 52

def unsubscribe_all!
  subscriptions.clear

  true
end