Class: Datadog::Contrib::ActiveSupport::Notifications::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/ddtrace/contrib/active_support/notifications/subscription.rb

Overview

An ActiveSupport::Notification subscription that wraps events with tracing.

Defined Under Namespace

Classes: Callbacks, Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracer, span_name, options, &block) ⇒ Subscription

Returns a new instance of Subscription.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 11

def initialize(tracer, span_name, options, &block)
  raise ArgumentError, 'Must be given a block!' unless block_given?
  @tracer = tracer
  @span_name = span_name
  @options = options
  @handler = Handler.new(&block)
  @callbacks = Callbacks.new
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 7

def options
  @options
end

#span_nameObject

Returns the value of attribute span_name.



7
8
9
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 7

def span_name
  @span_name
end

Instance Method Details

#after_trace(&block) ⇒ Object



44
45
46
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 44

def after_trace(&block)
  callbacks.add(:after_trace, &block) if block_given?
end

#before_trace(&block) ⇒ Object



40
41
42
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 40

def before_trace(&block)
  callbacks.add(:before_trace, &block) if block_given?
end

#call(name, start, finish, id, payload) ⇒ Object

ActiveSupport 3.x calls this



25
26
27
28
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 25

def call(name, start, finish, id, payload)
  start_span(name, id, payload, start)
  finish_span(name, id, payload, finish)
end

#finish(name, id, payload) ⇒ Object

ActiveSupport 4+ calls this on finish



36
37
38
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 36

def finish(name, id, payload)
  finish_span(name, id, payload)
end

#start(name, id, payload) ⇒ Object

ActiveSupport 4+ calls this on start



31
32
33
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 31

def start(name, id, payload)
  start_span(name, id, payload)
end

#subscribe(pattern) ⇒ Object



48
49
50
51
52
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 48

def subscribe(pattern)
  return false if subscribers.key?(pattern)
  subscribers[pattern] = ::ActiveSupport::Notifications.subscribe(pattern, self)
  true
end

#tracerObject



20
21
22
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 20

def tracer
  @tracer.is_a?(Proc) ? @tracer.call : @tracer
end

#unsubscribe(pattern) ⇒ Object



54
55
56
57
58
59
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 54

def unsubscribe(pattern)
  return false unless subscribers.key?(pattern)
  ::ActiveSupport::Notifications.unsubscribe(subscribers[pattern])
  subscribers.delete(pattern)
  true
end

#unsubscribe_allObject



61
62
63
64
65
# File 'lib/ddtrace/contrib/active_support/notifications/subscription.rb', line 61

def unsubscribe_all
  return false if subscribers.empty?
  subscribers.keys.each { |pattern| unsubscribe(pattern) }
  true
end