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

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/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(span_name, span_options, on_start: nil, on_finish: nil, trace: nil) ⇒ Subscription

Returns a new instance of Subscription.

Parameters:

  • span_name (String)

    the operation name for the span

  • span_options (Hash)

    span_options to pass during span creation

  • on_start (Proc) (defaults to: nil)

    a block to run when the event is fired, might not include all required information in the ‘payload` argument.

  • on_finish (Proc) (defaults to: nil)

    a block to run when the event has finished processing, possibly including more information in the ‘payload` argument.

  • trace (Proc) (defaults to: nil)

    whether to trace the event. Defaults to returning ‘true`.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 21

def initialize(span_name, span_options, on_start: nil, on_finish: nil, trace: nil)
  raise ArgumentError, 'Must be given either on_start or on_finish' unless on_start || on_finish

  @span_name = span_name
  @span_options = span_options
  @on_start = Handler.new(on_start)
  @on_finish = Handler.new(on_finish)
  @trace = trace
  @callbacks = Callbacks.new
end

Instance Attribute Details

#span_nameObject

Returns the value of attribute span_name.



10
11
12
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 10

def span_name
  @span_name
end

#span_optionsObject

Returns the value of attribute span_options.



10
11
12
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 10

def span_options
  @span_options
end

Instance Method Details

#after_trace(&block) ⇒ Object



46
47
48
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 46

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

#before_trace(&block) ⇒ Object



42
43
44
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 42

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

#finish(name, id, payload) ⇒ Object

Called by ActiveSupport on event finish



38
39
40
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 38

def finish(name, id, payload)
  finish_span(name, id, payload) if payload[:datadog_span]
end

#start(name, id, payload) ⇒ Object

Called by ActiveSupport on event start



33
34
35
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 33

def start(name, id, payload)
  start_span(name, id, payload) if @trace&.call(name, payload)
end

#subscribe(pattern) ⇒ Object



50
51
52
53
54
55
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 50

def subscribe(pattern)
  return false if subscribers.key?(pattern)

  subscribers[pattern] = ::ActiveSupport::Notifications.subscribe(pattern, self)
  true
end

#unsubscribe(pattern) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 57

def unsubscribe(pattern)
  return false unless subscribers.key?(pattern)

  ::ActiveSupport::Notifications.unsubscribe(subscribers[pattern])
  subscribers.delete(pattern)
  true
end

#unsubscribe_allObject



65
66
67
68
69
70
# File 'lib/datadog/tracing/contrib/active_support/notifications/subscription.rb', line 65

def unsubscribe_all
  return false if subscribers.empty?

  subscribers.each_key { |pattern| unsubscribe(pattern) }
  true
end